From 7327d5cc95d57d7561e563a94230cffae7f64bd5 Mon Sep 17 00:00:00 2001 From: robsliwi Date: Thu, 26 Apr 2018 17:06:02 +0200 Subject: [PATCH 1/7] Add categories to actions --- lib/oat/adapters/siren.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oat/adapters/siren.rb b/lib/oat/adapters/siren.rb index be4cc03..4fac6b7 100644 --- a/lib/oat/adapters/siren.rb +++ b/lib/oat/adapters/siren.rb @@ -81,7 +81,7 @@ def field(name, &block) data[:fields] << field.data end - %w(href method title type).each do |attribute| + %w(categories href method title type).each do |attribute| define_method(attribute) do |value| data[attribute.to_sym] = value end From d432a99087d052fce81f1bbc5e57a00919146528 Mon Sep 17 00:00:00 2001 From: Daniel Kuhn Date: Fri, 27 Apr 2018 10:50:10 +0200 Subject: [PATCH 2/7] Add category, required and class to form fields --- .gitignore | 1 + lib/oat/adapters/siren.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 162ed15..c279e7a 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ spec/reports test/tmp test/version_tmp tmp +.idea/ \ No newline at end of file diff --git a/lib/oat/adapters/siren.rb b/lib/oat/adapters/siren.rb index 4fac6b7..c389146 100644 --- a/lib/oat/adapters/siren.rb +++ b/lib/oat/adapters/siren.rb @@ -98,7 +98,7 @@ def klass(value) data[:class] << value end - %w(type value title).each do |attribute| + %w(category required class type value title).each do |attribute| define_method(attribute) do |value| data[attribute.to_sym] = value end From 58b04dd0e2b8701ecab94f9fcf0f7b64a24cbddc Mon Sep 17 00:00:00 2001 From: Robert Sliwinski Date: Thu, 3 May 2018 16:41:58 +0200 Subject: [PATCH 3/7] Remove class attribute It's already included in v0.6.0 --- lib/oat/adapters/siren.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oat/adapters/siren.rb b/lib/oat/adapters/siren.rb index c389146..7a54a53 100644 --- a/lib/oat/adapters/siren.rb +++ b/lib/oat/adapters/siren.rb @@ -98,7 +98,7 @@ def klass(value) data[:class] << value end - %w(category required class type value title).each do |attribute| + %w(category required type value title).each do |attribute| define_method(attribute) do |value| data[attribute.to_sym] = value end From 2e7699f6f72ae26b7eb4f0fece505909d2a2cc9f Mon Sep 17 00:00:00 2001 From: Daniel Kuhn Date: Thu, 3 May 2018 17:07:17 +0200 Subject: [PATCH 4/7] Remove required attribute for fields Required is moved to a class on the frontend side. See: https://github.com/evopark/product-backend/pull/20 --- lib/oat/adapters/siren.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oat/adapters/siren.rb b/lib/oat/adapters/siren.rb index 7a54a53..8130a2f 100644 --- a/lib/oat/adapters/siren.rb +++ b/lib/oat/adapters/siren.rb @@ -98,7 +98,7 @@ def klass(value) data[:class] << value end - %w(category required type value title).each do |attribute| + %w(category type value title).each do |attribute| define_method(attribute) do |value| data[attribute.to_sym] = value end From f2808b98e590246cd9a85f66cb75e61c1d3a5f3e Mon Sep 17 00:00:00 2001 From: Marcus Ilgner Date: Wed, 30 May 2018 16:39:13 +0200 Subject: [PATCH 5/7] Support arrays in #klass, Siren title --- lib/oat/adapters/siren.rb | 8 ++++++-- spec/adapters/siren_spec.rb | 2 ++ spec/fixtures.rb | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/oat/adapters/siren.rb b/lib/oat/adapters/siren.rb index 8130a2f..3145f75 100644 --- a/lib/oat/adapters/siren.rb +++ b/lib/oat/adapters/siren.rb @@ -21,6 +21,10 @@ def type(*types) data[:class] = types end + def title(title) + data[:title] = title + end + def link(rel, opts = {}) data[:links] << {:rel => [rel].flatten}.merge(opts) end @@ -71,7 +75,7 @@ def initialize(name) end def klass(value) - data[:class] << value + data[:class].concat(Array(value)) end def field(name, &block) @@ -95,7 +99,7 @@ def initialize(name) end def klass(value) - data[:class] << value + data[:class].concat(Array(value)) end %w(category type value title).each do |attribute| diff --git a/spec/adapters/siren_spec.rb b/spec/adapters/siren_spec.rb index 3000aaf..38f6a34 100644 --- a/spec/adapters/siren_spec.rb +++ b/spec/adapters/siren_spec.rb @@ -12,6 +12,8 @@ it 'produces a Siren-compliant hash' do expect(hash.fetch(:class)).to match_array(['user']) + expect(hash.fetch(:title)).to eq('This guy') + expect(hash.fetch(:properties)).to include( :id => user.id, :name => user.name, diff --git a/spec/fixtures.rb b/spec/fixtures.rb index 1c6bbad..f9879a7 100644 --- a/spec/fixtures.rb +++ b/spec/fixtures.rb @@ -38,6 +38,10 @@ def self.included(base) entities [:friends, 'http://example.org/rels/person'], item.friends, klass, :message => "Merged into parent's context" end + if adapter.respond_to?(:title) + title 'This guy' + end + if adapter.respond_to?(:action) action :close_account do |action| action.href "http://foo.bar.com/#{item.id}/close_account" From d836ac217b2929c0f2dd91bdfde643201b4a3a1b Mon Sep 17 00:00:00 2001 From: Robert Sliwinski Date: Mon, 4 Jun 2018 12:13:21 +0200 Subject: [PATCH 6/7] Remove uniqueness for entities When adding multiple entities with the same attributes the Siren-Adapter just ignored the new entity if there was already one with the same values. --- lib/oat/adapters/siren.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/oat/adapters/siren.rb b/lib/oat/adapters/siren.rb index 3145f75..63e3f8c 100644 --- a/lib/oat/adapters/siren.rb +++ b/lib/oat/adapters/siren.rb @@ -46,9 +46,7 @@ def entity(name, obj, serializer_class = nil, context_options = {}, &block) ent.rel(name) ent_hash = ent.to_hash - unless data[:entities].include? ent_hash - data[:entities] << ent_hash - end + data[:entities] << ent_hash end end From 4324215c8c5633752503038f7097621c840cccba Mon Sep 17 00:00:00 2001 From: Christian Rolle Date: Tue, 19 Jun 2018 15:42:52 +0200 Subject: [PATCH 7/7] Make type consistent regarding argument list It should be possible to assign 1-n types to an entity. Therefore the type accessor is expected to deal with array as list as well. --- lib/oat/adapters/siren.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/oat/adapters/siren.rb b/lib/oat/adapters/siren.rb index 63e3f8c..697ca0d 100644 --- a/lib/oat/adapters/siren.rb +++ b/lib/oat/adapters/siren.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + # https://github.com/kevinswiber/siren module Oat module Adapters class Siren < Oat::Adapter - def initialize(*args) super data[:links] = [] @@ -12,13 +13,13 @@ def initialize(*args) # Sub-Entities have a required rel attribute # https://github.com/kevinswiber/siren#rel - def rel(rels) + def rel(*rels) # rel must be an array. data[:rel] = Array(rels) end def type(*types) - data[:class] = types + data[:class] = Array(types) end def title(title) @@ -26,7 +27,7 @@ def title(title) end def link(rel, opts = {}) - data[:links] << {:rel => [rel].flatten}.merge(opts) + data[:links] << { rel: Array(rel) }.merge(opts) end def properties(&block) @@ -37,7 +38,7 @@ def property(key, value) data[:properties][key] = value end - alias_method :meta, :property + alias meta property def entity(name, obj, serializer_class = nil, context_options = {}, &block) ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block) @@ -56,11 +57,11 @@ def entities(name, collection, serializer_class = nil, context_options = {}, &bl end end - alias_method :collection, :entities + alias collection entities - def action(name, &block) + def action(name) action = Action.new(name) - block.call(action) + yield(action) data[:actions] << action.data end @@ -69,21 +70,21 @@ class Action attr_reader :data def initialize(name) - @data = { :name => name, :class => [], :fields => [] } + @data = { name: name, class: [], fields: [] } end def klass(value) data[:class].concat(Array(value)) end - def field(name, &block) + def field(name) field = Field.new(name) - block.call(field) + yield(field) data[:fields] << field.data end - %w(categories href method title type).each do |attribute| + %w[categories href method title type].each do |attribute| define_method(attribute) do |value| data[attribute.to_sym] = value end @@ -93,21 +94,20 @@ class Field attr_reader :data def initialize(name) - @data = { :name => name, :class => []} + @data = { name: name, class: [] } end def klass(value) data[:class].concat(Array(value)) end - %w(category type value title).each do |attribute| + %w[category type value title].each do |attribute| define_method(attribute) do |value| data[attribute.to_sym] = value end end end end - end end end