From 3ba4386bdac4c1933d1125c2fbb629de745a1fd0 Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Wed, 18 Mar 2015 15:01:50 -0400 Subject: [PATCH 01/13] Root is always "data" for jsonapi --- lib/active_model/serializer/adapter/json_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index f604b67f..b74daaf8 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -16,7 +16,7 @@ module ActiveModel end def serializable_hash(options = {}) - @root = (@options[:root] || serializer.json_key.to_s.pluralize).to_sym + @root = 'data' if serializer.respond_to?(:each) @hash[@root] = serializer.map do |s| From da86747a3e0ac4cc314867c2d4f234a97b921e1b Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Thu, 19 Mar 2015 14:14:27 -0400 Subject: [PATCH 02/13] Use symbol for root in jsonapi, fix tests --- lib/active_model/serializer/adapter/json_api.rb | 2 +- test/action_controller/adapter_selector_test.rb | 2 +- test/action_controller/serialization_scope_name_test.rb | 4 ++-- test/action_controller/serialization_test.rb | 6 +++--- test/adapter/json_api/belongs_to_test.rb | 6 +++--- test/adapter/json_api/collection_test.rb | 4 ++-- test/adapter/json_api/has_many_embed_ids_test.rb | 2 +- .../adapter/json_api/has_many_explicit_serializer_test.rb | 8 ++++---- test/adapter/json_api/has_many_test.rb | 4 ++-- test/adapter/json_api/has_one_test.rb | 2 +- test/adapter/json_api/linked_test.rb | 2 +- 11 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index b74daaf8..18905c30 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -16,7 +16,7 @@ module ActiveModel end def serializable_hash(options = {}) - @root = 'data' + @root = :data if serializer.respond_to?(:each) @hash[@root] = serializer.map do |s| diff --git a/test/action_controller/adapter_selector_test.rb b/test/action_controller/adapter_selector_test.rb index 96d7dd52..ea266881 100644 --- a/test/action_controller/adapter_selector_test.rb +++ b/test/action_controller/adapter_selector_test.rb @@ -29,7 +29,7 @@ module ActionController def test_render_using_adapter_override get :render_using_adapter_override - assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', response.body + assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', response.body end def test_render_skipping_adapter diff --git a/test/action_controller/serialization_scope_name_test.rb b/test/action_controller/serialization_scope_name_test.rb index f1e81761..df77231a 100644 --- a/test/action_controller/serialization_scope_name_test.rb +++ b/test/action_controller/serialization_scope_name_test.rb @@ -29,7 +29,7 @@ class DefaultScopeNameTest < ActionController::TestCase def test_default_scope_name get :render_new_user - assert_equal '{"users":{"admin?":false}}', @response.body + assert_equal '{"data":{"admin?":false}}', @response.body end end @@ -62,6 +62,6 @@ class SerializationScopeNameTest < ActionController::TestCase def test_override_scope_name_with_controller get :render_new_user - assert_equal '{"admin_users":{"admin?":true}}', @response.body + assert_equal '{"data":{"admin?":true}}', @response.body end end \ No newline at end of file diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index a10cb039..28325ede 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -111,14 +111,14 @@ module ActionController get :render_using_default_adapter_root assert_equal 'application/json', @response.content_type - assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', @response.body + assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body end def test_render_using_custom_root_in_adapter_with_a_default get :render_using_custom_root_in_adapter_with_a_default assert_equal 'application/json', @response.content_type - assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body + assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body end def test_render_array_using_implicit_serializer @@ -143,7 +143,7 @@ module ActionController get :render_array_using_implicit_serializer_and_meta assert_equal 'application/json', @response.content_type - assert_equal '{"profiles":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body + assert_equal '{"data":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body end def test_render_with_cache_enable diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index 206c4167..e5073d65 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -32,7 +32,7 @@ module ActiveModel end def test_includes_post_id - assert_equal("42", @adapter.serializable_hash[:comments][:links][:post]) + assert_equal("42", @adapter.serializable_hash[:data][:links][:post]) end def test_includes_linked_post @@ -67,13 +67,13 @@ module ActiveModel serializer = PostSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - assert_equal({comments: [], blog: "999", author: nil}, adapter.serializable_hash[:posts][:links]) + assert_equal({comments: [], blog: "999", author: nil}, adapter.serializable_hash[:data][:links]) end def test_include_type_for_association_when_different_than_name serializer = BlogSerializer.new(@blog) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - links = adapter.serializable_hash[:blogs][:links] + links = adapter.serializable_hash[:data][:links] expected = { writer: { type: "author", diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index 3f207065..48b3952d 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -28,7 +28,7 @@ module ActiveModel assert_equal([ { title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } }, { title: "New Post", body: "Body", id: "2", links: { comments: [], blog: "999", author: "1" } } - ], @adapter.serializable_hash[:posts]) + ], @adapter.serializable_hash[:data]) end def test_limiting_fields @@ -36,7 +36,7 @@ module ActiveModel assert_equal([ { title: "Hello!!", links: { comments: [], blog: "999", author: "1" } }, { title: "New Post", links: { comments: [], blog: "999", author: "1" } } - ], @adapter.serializable_hash[:posts]) + ], @adapter.serializable_hash[:data]) end end diff --git a/test/adapter/json_api/has_many_embed_ids_test.rb b/test/adapter/json_api/has_many_embed_ids_test.rb index 0082e585..1f68006d 100644 --- a/test/adapter/json_api/has_many_embed_ids_test.rb +++ b/test/adapter/json_api/has_many_embed_ids_test.rb @@ -25,7 +25,7 @@ module ActiveModel end def test_includes_comment_ids - assert_equal(["1", "2"], @adapter.serializable_hash[:authors][:links][:posts]) + assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:posts]) end def test_no_includes_linked_comments diff --git a/test/adapter/json_api/has_many_explicit_serializer_test.rb b/test/adapter/json_api/has_many_explicit_serializer_test.rb index 0f99f0ec..e1df69f0 100644 --- a/test/adapter/json_api/has_many_explicit_serializer_test.rb +++ b/test/adapter/json_api/has_many_explicit_serializer_test.rb @@ -30,7 +30,7 @@ module ActiveModel def test_includes_comment_ids assert_equal(['1', '2'], - @adapter.serializable_hash[:posts][:links][:comments]) + @adapter.serializable_hash[:data][:links][:comments]) end def test_includes_linked_comments @@ -42,7 +42,7 @@ module ActiveModel def test_includes_author_id assert_equal(@author.id.to_s, - @adapter.serializable_hash[:posts][:links][:author]) + @adapter.serializable_hash[:data][:links][:author]) end def test_includes_linked_authors @@ -53,13 +53,13 @@ module ActiveModel def test_explicit_serializer_with_null_resource @post.author = nil assert_equal(nil, - @adapter.serializable_hash[:posts][:links][:author]) + @adapter.serializable_hash[:data][:links][:author]) end def test_explicit_serializer_with_null_collection @post.comments = [] assert_equal([], - @adapter.serializable_hash[:posts][:links][:comments]) + @adapter.serializable_hash[:data][:links][:comments]) end end end diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index 87ef36f3..a4ec8efd 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -33,7 +33,7 @@ module ActiveModel end def test_includes_comment_ids - assert_equal(["1", "2"], @adapter.serializable_hash[:posts][:links][:comments]) + assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:comments]) end def test_includes_linked_comments @@ -84,7 +84,7 @@ module ActiveModel def test_include_type_for_association_when_different_than_name serializer = BlogSerializer.new(@blog) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - actual = adapter.serializable_hash[:blogs][:links][:articles] + actual = adapter.serializable_hash[:data][:links][:articles] expected = { type: "posts", ids: ["1"] diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb index 247bb2f9..42a76156 100644 --- a/test/adapter/json_api/has_one_test.rb +++ b/test/adapter/json_api/has_one_test.rb @@ -30,7 +30,7 @@ module ActiveModel end def test_includes_bio_id - assert_equal("43", @adapter.serializable_hash[:authors][:links][:bio]) + assert_equal("43", @adapter.serializable_hash[:data][:links][:bio]) end def test_includes_linked_bio diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index 72897239..dd28e74d 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -185,7 +185,7 @@ module ActiveModel spammy_post.related = [Spam::UnrelatedLink.new(id: 456)] serializer = SpammyPostSerializer.new(spammy_post) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - links = adapter.serializable_hash[:posts][:links] + links = adapter.serializable_hash[:data][:links] expected = { related: { type: 'unrelated_links', From 83c28540946500009f3515e4e45adc4fd5aa900c Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Fri, 20 Mar 2015 10:49:33 -0400 Subject: [PATCH 03/13] Rename `add_linked` to `add_included` Better reflect generated output --- lib/active_model/serializer/adapter/json_api.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 18905c30..461d28f4 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -65,7 +65,7 @@ module ActiveModel end end - def add_linked(resource_name, serializers, parent = nil) + def add_included(resource_name, serializers, parent = nil) serializers = Array(serializers) unless serializers.respond_to?(:each) resource_path = [parent, resource_name].compact.join('.') @@ -78,7 +78,7 @@ module ActiveModel serializers.each do |serializer| attrs = attributes_for_serializer(serializer, @options) - add_resource_links(attrs, serializer, add_linked: false) + add_resource_links(attrs, serializer, add_included: false) @top[:linked][plural_name].push(attrs) unless @top[:linked][plural_name].include?(attrs) end @@ -86,7 +86,7 @@ module ActiveModel serializers.each do |serializer| serializer.each_association do |name, association, opts| - add_linked(name, association, resource_path) if association + add_included(name, association, resource_path) if association end if include_nested_assoc? resource_path end end @@ -139,7 +139,7 @@ module ActiveModel end def add_resource_links(attrs, serializer, options = {}) - options[:add_linked] = options.fetch(:add_linked, true) + options[:add_included] = options.fetch(:add_included, true) serializer.each_association do |name, association, opts| attrs[:links] ||= {} @@ -150,9 +150,9 @@ module ActiveModel add_link(attrs, name, association) end - if @options[:embed] != :ids && options[:add_linked] + if @options[:embed] != :ids && options[:add_included] Array(association).each do |association| - add_linked(name, association) + add_included(name, association) end end end From 0f55f212667bcd8babc1d36a1d33d0a83e463b48 Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Fri, 20 Mar 2015 12:04:22 -0400 Subject: [PATCH 04/13] Update format of links --- .../serializer/adapter/json_api.rb | 22 ++----- .../action_controller/json_api_linked_test.rb | 10 ++-- test/adapter/json_api/belongs_to_test.rb | 54 ++++++++++------- test/adapter/json_api/collection_test.rb | 53 +++++++++++++--- .../json_api/has_many_embed_ids_test.rb | 4 +- .../has_many_explicit_serializer_test.rb | 57 ++++++++++++++---- test/adapter/json_api/has_many_test.rb | 26 ++++---- test/adapter/json_api/has_one_test.rb | 17 +++++- test/adapter/json_api/linked_test.rb | 60 ++++++++++--------- test/test_helper.rb | 2 +- 10 files changed, 198 insertions(+), 107 deletions(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 461d28f4..0c5aaf97 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -38,30 +38,18 @@ module ActiveModel type = serialized_object_type(serializers) resource[:links] ||= {} - if name.to_s == type || !type - resource[:links][name] ||= [] - resource[:links][name] += serializers.map{|serializer| serializer.id.to_s } - else - resource[:links][name] ||= {} - resource[:links][name][:type] = type - resource[:links][name][:ids] ||= [] - resource[:links][name][:ids] += serializers.map{|serializer| serializer.id.to_s } - end + resource[:links][name] ||= { linkage: [] } + resource[:links][name][:linkage] += serializers.map { |serializer| { type: type, id: serializer.id.to_s } } end def add_link(resource, name, serializer) resource[:links] ||= {} - resource[:links][name] = nil + resource[:links][name] = { linkage: nil } if serializer && serializer.object type = serialized_object_type(serializer) - if name.to_s == type || !type - resource[:links][name] = serializer.id.to_s - else - resource[:links][name] ||= {} - resource[:links][name][:type] = type - resource[:links][name][:id] = serializer.id.to_s - end + + resource[:links][name][:linkage] = { type: type, id: serializer.id.to_s } end end diff --git a/test/action_controller/json_api_linked_test.rb b/test/action_controller/json_api_linked_test.rb index 7d4dcbf8..e24f695b 100644 --- a/test/action_controller/json_api_linked_test.rb +++ b/test/action_controller/json_api_linked_test.rb @@ -102,22 +102,22 @@ module ActionController "id" => "1", "name" => "Steve K.", "links" => { - "posts" => [], - "roles" => ["1", "2"], - "bio" => nil + "posts" => { "linkage" => [] }, + "roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] }, + "bio" => { "linkage" => nil } } }], "roles"=>[{ "id" => "1", "name" => "admin", "links" => { - "author" => "1" + "author" => { "linkage" => { "type" =>"author", "id" => "1" } } } }, { "id" => "2", "name" => "colab", "links" => { - "author" => "1" + "author" => { "linkage" => { "type" =>"author", "id" => "1" } } } }] } diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index e5073d65..a6b1cd55 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -32,7 +32,9 @@ module ActiveModel end def test_includes_post_id - assert_equal("42", @adapter.serializable_hash[:data][:links][:post]) + expected = { linkage: { type: "post", id: "42" } } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:post]) end def test_includes_linked_post @@ -42,9 +44,9 @@ module ActiveModel title: 'New Post', body: 'Body', links: { - comments: ["1"], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: "1" } ] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }] assert_equal expected, @adapter.serializable_hash[:linked][:posts] @@ -55,9 +57,9 @@ module ActiveModel expected = [{ title: 'New Post', links: { - comments: ["1"], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: "1" } ] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }] assert_equal expected, @adapter.serializable_hash[:linked][:posts] @@ -67,7 +69,7 @@ module ActiveModel serializer = PostSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - assert_equal({comments: [], blog: "999", author: nil}, adapter.serializable_hash[:data][:links]) + assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blog", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links]) end def test_include_type_for_association_when_different_than_name @@ -76,12 +78,22 @@ module ActiveModel links = adapter.serializable_hash[:data][:links] expected = { writer: { - type: "author", - id: "1" + linkage: { + type: "author", + id: "1" + } }, articles: { - type: "posts", - ids: ["42", "43"] + linkage: [ + { + type: "posts", + id: "42" + }, + { + type: "posts", + id: "43" + } + ] } } assert_equal expected, links @@ -96,9 +108,9 @@ module ActiveModel id: "1", name: "Steve K.", links: { - posts: [], - roles: [], - bio: nil + posts: { linkage: [] }, + roles: { linkage: [] }, + bio: { linkage: nil } } }], posts: [{ @@ -106,18 +118,18 @@ module ActiveModel body: "Body", id: "42", links: { - comments: ["1"], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: "1" } ] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }, { title: "Hello!!", body: "Hello, world!!", id: "43", links: { - comments: [], - blog: "999", - author: nil + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: nil } } }] } diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index 48b3952d..b7967feb 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -25,18 +25,55 @@ module ActiveModel end def test_include_multiple_posts - assert_equal([ - { title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } }, - { title: "New Post", body: "Body", id: "2", links: { comments: [], blog: "999", author: "1" } } - ], @adapter.serializable_hash[:data]) + expected = [ + { + title: "Hello!!", + body: "Hello, world!!", + id: "1", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } + } + }, + { + title: "New Post", + body: "Body", + id: "2", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } + } + } + ] + + assert_equal(expected, @adapter.serializable_hash[:data]) end def test_limiting_fields @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title']) - assert_equal([ - { title: "Hello!!", links: { comments: [], blog: "999", author: "1" } }, - { title: "New Post", links: { comments: [], blog: "999", author: "1" } } - ], @adapter.serializable_hash[:data]) + + expected = [ + { + title: "Hello!!", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } + } + }, + { + title: "New Post", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } + } + } + ] + + assert_equal(expected, @adapter.serializable_hash[:data]) end end diff --git a/test/adapter/json_api/has_many_embed_ids_test.rb b/test/adapter/json_api/has_many_embed_ids_test.rb index 1f68006d..2a2cb3ea 100644 --- a/test/adapter/json_api/has_many_embed_ids_test.rb +++ b/test/adapter/json_api/has_many_embed_ids_test.rb @@ -25,7 +25,9 @@ module ActiveModel end def test_includes_comment_ids - assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:posts]) + expected = {:linkage=>[{:type=>"posts", :id=>"1"}, {:type=>"posts", :id=>"2"}]} + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts]) end def test_no_includes_linked_comments diff --git a/test/adapter/json_api/has_many_explicit_serializer_test.rb b/test/adapter/json_api/has_many_explicit_serializer_test.rb index e1df69f0..01368873 100644 --- a/test/adapter/json_api/has_many_explicit_serializer_test.rb +++ b/test/adapter/json_api/has_many_explicit_serializer_test.rb @@ -29,37 +29,70 @@ module ActiveModel end def test_includes_comment_ids - assert_equal(['1', '2'], - @adapter.serializable_hash[:data][:links][:comments]) + expected = { + linkage: [ + { type: 'comments', id: '1' }, + { type: 'comments', id: '2' } + ] + } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) end def test_includes_linked_comments # If CommentPreviewSerializer is applied correctly the body text will not be present in the output - assert_equal([{ id: '1', links: { post: @post.id.to_s}}, - { id: '2', links: { post: @post.id.to_s}}], + expected = [ + { + id: '1', + links: { + post: { linkage: { type: 'post', id: @post.id.to_s } } + } + }, + { + id: '2', + links: { + post: { linkage: { type: 'post', id: @post.id.to_s } } + } + } + ] + + assert_equal(expected, @adapter.serializable_hash[:linked][:comments]) end def test_includes_author_id - assert_equal(@author.id.to_s, - @adapter.serializable_hash[:data][:links][:author]) + expected = { + linkage: { type: "author", id: @author.id.to_s } + } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:author]) end def test_includes_linked_authors - assert_equal([{ id: @author.id.to_s, links: { posts: [@post.id.to_s] } }], - @adapter.serializable_hash[:linked][:authors]) + expected = [{ + id: @author.id.to_s, + links: { + posts: { linkage: [ { type: "posts", id: @post.id.to_s } ] } + } + }] + + assert_equal(expected, @adapter.serializable_hash[:linked][:authors]) end def test_explicit_serializer_with_null_resource @post.author = nil - assert_equal(nil, - @adapter.serializable_hash[:data][:links][:author]) + + expected = { linkage: nil } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:author]) end def test_explicit_serializer_with_null_collection @post.comments = [] - assert_equal([], - @adapter.serializable_hash[:data][:links][:comments]) + + expected = { linkage: [] } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) end end end diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index a4ec8efd..eae4c620 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -33,7 +33,9 @@ module ActiveModel end def test_includes_comment_ids - assert_equal(["1", "2"], @adapter.serializable_hash[:data][:links][:comments]) + expected = { linkage: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) end def test_includes_linked_comments @@ -42,15 +44,15 @@ module ActiveModel id: "1", body: 'ZOMG A COMMENT', links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }, { id: "2", body: 'ZOMG ANOTHER COMMENT', links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }] assert_equal expected, @adapter.serializable_hash[:linked][:comments] @@ -61,14 +63,14 @@ module ActiveModel expected = [{ id: "1", links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }, { id: "2", links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }] assert_equal expected, @adapter.serializable_hash[:linked][:comments] @@ -86,8 +88,10 @@ module ActiveModel adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) actual = adapter.serializable_hash[:data][:links][:articles] expected = { - type: "posts", - ids: ["1"] + linkage: [{ + type: "posts", + id: "1" + }] } assert_equal expected, actual end diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb index 42a76156..f8e6dccf 100644 --- a/test/adapter/json_api/has_one_test.rb +++ b/test/adapter/json_api/has_one_test.rb @@ -30,12 +30,25 @@ module ActiveModel end def test_includes_bio_id - assert_equal("43", @adapter.serializable_hash[:data][:links][:bio]) + expected = { linkage: { type: "bio", id: "43" } } + + assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio]) end def test_includes_linked_bio @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio') - assert_equal([{id: "43", :content=>"AMS Contributor", :links=>{:author=>"1"}}], @adapter.serializable_hash[:linked][:bios]) + + expected = [ + { + id: "43", + content:"AMS Contributor", + links: { + author: { linkage: { type: "author", id: "1" } } + } + } + ] + + assert_equal(expected, @adapter.serializable_hash[:linked][:bios]) end end end diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index dd28e74d..2a930238 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -57,15 +57,15 @@ module ActiveModel id: "1", body: "ZOMG A COMMENT", links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } }, { id: "2", body: "ZOMG ANOTHER COMMENT", links: { - post: "1", - author: nil + post: { linkage: { type: "post", id: "1" } }, + author: { linkage: nil } } } ], @@ -74,17 +74,17 @@ module ActiveModel id: "1", name: "Steve K.", links: { - posts: ["1", "3"], - roles: [], - bio: "1" + posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] }, + roles: { linkage: [] }, + bio: { linkage: { type: "bio", id: "1" } } } }, { id: "2", name: "Tenderlove", links: { - posts: ["2"], - roles: [], - bio: "2" + posts: { linkage: [ { type: "posts", id:"2" } ] }, + roles: { linkage: [] }, + bio: { linkage: { type: "bio", id: "2" } } } } ], @@ -93,13 +93,13 @@ module ActiveModel id: "1", content: "AMS Contributor", links: { - author: "1" + author: { linkage: { type: "author", id: "1" } } } }, { id: "2", content: "Rails Contributor", links: { - author: "2" + author: { linkage: { type: "author", id: "2" } } } } ] @@ -110,9 +110,9 @@ module ActiveModel title: "Hello!!", body: "Hello, world!!", links: { - comments: ['1', '2'], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }, { @@ -120,9 +120,9 @@ module ActiveModel title: "New Post", body: "Body", links: { - comments: [], - blog: "999", - author: "2" + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "2" } } } } ] @@ -148,9 +148,9 @@ module ActiveModel id: "1", name: "Steve K.", links: { - posts: ["10", "30"], - roles: [], - bio: "1" + posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] }, + roles: { linkage: [] }, + bio: { linkage: { type: "bio", id: "1" }} } } ], @@ -160,18 +160,18 @@ module ActiveModel title: "Hello!!", body: "Hello, world!!", links: { - comments: ["1", "2"], - blog: "999", - author: "1" + comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } }, { id: "30", title: "Yet Another Post", body: "Body", links: { - comments: [], - blog: "999", - author: "1" + comments: { linkage: [] }, + blog: { linkage: { type: "blog", id: "999" } }, + author: { linkage: { type: "author", id: "1" } } } } ] @@ -188,8 +188,10 @@ module ActiveModel links = adapter.serializable_hash[:data][:links] expected = { related: { - type: 'unrelated_links', - ids: ['456'] + linkage: [{ + type: 'unrelated_links', + id: '456' + }] } } assert_equal expected, links diff --git a/test/test_helper.rb b/test/test_helper.rb index f3977b61..b71a4b57 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,5 @@ require 'bundler/setup' - +require 'pp' require 'rails' require 'action_controller' require 'action_controller/test_case' From d82c599c687a47162c98b1af72e06ec8a43211b0 Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Fri, 20 Mar 2015 14:17:03 -0400 Subject: [PATCH 05/13] Always use plural for linked types Although spec is agnostic about inflection rules, examples given are plural --- .../serializer/adapter/json_api.rb | 7 +---- .../action_controller/json_api_linked_test.rb | 4 +-- test/adapter/json_api/belongs_to_test.rb | 20 ++++++------- test/adapter/json_api/collection_test.rb | 16 +++++----- .../json_api/has_many_embed_ids_test.rb | 7 ++++- .../has_many_explicit_serializer_test.rb | 6 ++-- test/adapter/json_api/has_many_test.rb | 8 ++--- test/adapter/json_api/has_one_test.rb | 4 +-- test/adapter/json_api/linked_test.rb | 30 +++++++++---------- 9 files changed, 51 insertions(+), 51 deletions(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 0c5aaf97..ef51ea14 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -118,12 +118,7 @@ module ActiveModel def serialized_object_type(serializer) return false unless Array(serializer).first - type_name = Array(serializer).first.object.class.to_s.demodulize.underscore - if serializer.respond_to?(:first) - type_name.pluralize - else - type_name - end + Array(serializer).first.object.class.to_s.demodulize.underscore.pluralize end def add_resource_links(attrs, serializer, options = {}) diff --git a/test/action_controller/json_api_linked_test.rb b/test/action_controller/json_api_linked_test.rb index e24f695b..3f8b45eb 100644 --- a/test/action_controller/json_api_linked_test.rb +++ b/test/action_controller/json_api_linked_test.rb @@ -111,13 +111,13 @@ module ActionController "id" => "1", "name" => "admin", "links" => { - "author" => { "linkage" => { "type" =>"author", "id" => "1" } } + "author" => { "linkage" => { "type" =>"authors", "id" => "1" } } } }, { "id" => "2", "name" => "colab", "links" => { - "author" => { "linkage" => { "type" =>"author", "id" => "1" } } + "author" => { "linkage" => { "type" =>"authors", "id" => "1" } } } }] } diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index a6b1cd55..38f80800 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -32,7 +32,7 @@ module ActiveModel end def test_includes_post_id - expected = { linkage: { type: "post", id: "42" } } + expected = { linkage: { type: "posts", id: "42" } } assert_equal(expected, @adapter.serializable_hash[:data][:links][:post]) end @@ -45,8 +45,8 @@ module ActiveModel body: 'Body', links: { comments: { linkage: [ { type: "comments", id: "1" } ] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } }] assert_equal expected, @adapter.serializable_hash[:linked][:posts] @@ -58,8 +58,8 @@ module ActiveModel title: 'New Post', links: { comments: { linkage: [ { type: "comments", id: "1" } ] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } }] assert_equal expected, @adapter.serializable_hash[:linked][:posts] @@ -69,7 +69,7 @@ module ActiveModel serializer = PostSerializer.new(@anonymous_post) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blog", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links]) + assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links]) end def test_include_type_for_association_when_different_than_name @@ -79,7 +79,7 @@ module ActiveModel expected = { writer: { linkage: { - type: "author", + type: "authors", id: "1" } }, @@ -119,8 +119,8 @@ module ActiveModel id: "42", links: { comments: { linkage: [ { type: "comments", id: "1" } ] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } }, { title: "Hello!!", @@ -128,7 +128,7 @@ module ActiveModel id: "43", links: { comments: { linkage: [] }, - blog: { linkage: { type: "blog", id: "999" } }, + blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil } } }] diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index b7967feb..c0216c36 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -32,8 +32,8 @@ module ActiveModel id: "1", links: { comments: { linkage: [] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } }, { @@ -42,8 +42,8 @@ module ActiveModel id: "2", links: { comments: { linkage: [] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } } ] @@ -59,16 +59,16 @@ module ActiveModel title: "Hello!!", links: { comments: { linkage: [] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } }, { title: "New Post", links: { comments: { linkage: [] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } } ] diff --git a/test/adapter/json_api/has_many_embed_ids_test.rb b/test/adapter/json_api/has_many_embed_ids_test.rb index 2a2cb3ea..50f367c1 100644 --- a/test/adapter/json_api/has_many_embed_ids_test.rb +++ b/test/adapter/json_api/has_many_embed_ids_test.rb @@ -25,7 +25,12 @@ module ActiveModel end def test_includes_comment_ids - expected = {:linkage=>[{:type=>"posts", :id=>"1"}, {:type=>"posts", :id=>"2"}]} + expected = { + linkage: [ + { type: "posts", id: "1"}, + { type: "posts", id: "2"} + ] + } assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts]) end diff --git a/test/adapter/json_api/has_many_explicit_serializer_test.rb b/test/adapter/json_api/has_many_explicit_serializer_test.rb index 01368873..ecebbaa7 100644 --- a/test/adapter/json_api/has_many_explicit_serializer_test.rb +++ b/test/adapter/json_api/has_many_explicit_serializer_test.rb @@ -45,13 +45,13 @@ module ActiveModel { id: '1', links: { - post: { linkage: { type: 'post', id: @post.id.to_s } } + post: { linkage: { type: 'posts', id: @post.id.to_s } } } }, { id: '2', links: { - post: { linkage: { type: 'post', id: @post.id.to_s } } + post: { linkage: { type: 'posts', id: @post.id.to_s } } } } ] @@ -62,7 +62,7 @@ module ActiveModel def test_includes_author_id expected = { - linkage: { type: "author", id: @author.id.to_s } + linkage: { type: "authors", id: @author.id.to_s } } assert_equal(expected, @adapter.serializable_hash[:data][:links][:author]) diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index eae4c620..b520e5b0 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -44,14 +44,14 @@ module ActiveModel id: "1", body: 'ZOMG A COMMENT', links: { - post: { linkage: { type: "post", id: "1" } }, + post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } }, { id: "2", body: 'ZOMG ANOTHER COMMENT', links: { - post: { linkage: { type: "post", id: "1" } }, + post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } }] @@ -63,13 +63,13 @@ module ActiveModel expected = [{ id: "1", links: { - post: { linkage: { type: "post", id: "1" } }, + post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } }, { id: "2", links: { - post: { linkage: { type: "post", id: "1" } }, + post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } }] diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb index f8e6dccf..267ec4e5 100644 --- a/test/adapter/json_api/has_one_test.rb +++ b/test/adapter/json_api/has_one_test.rb @@ -30,7 +30,7 @@ module ActiveModel end def test_includes_bio_id - expected = { linkage: { type: "bio", id: "43" } } + expected = { linkage: { type: "bios", id: "43" } } assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio]) end @@ -43,7 +43,7 @@ module ActiveModel id: "43", content:"AMS Contributor", links: { - author: { linkage: { type: "author", id: "1" } } + author: { linkage: { type: "authors", id: "1" } } } } ] diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index 2a930238..e85c4b7d 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -57,14 +57,14 @@ module ActiveModel id: "1", body: "ZOMG A COMMENT", links: { - post: { linkage: { type: "post", id: "1" } }, + post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } }, { id: "2", body: "ZOMG ANOTHER COMMENT", links: { - post: { linkage: { type: "post", id: "1" } }, + post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } } @@ -76,7 +76,7 @@ module ActiveModel links: { posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] }, roles: { linkage: [] }, - bio: { linkage: { type: "bio", id: "1" } } + bio: { linkage: { type: "bios", id: "1" } } } }, { id: "2", @@ -84,7 +84,7 @@ module ActiveModel links: { posts: { linkage: [ { type: "posts", id:"2" } ] }, roles: { linkage: [] }, - bio: { linkage: { type: "bio", id: "2" } } + bio: { linkage: { type: "bios", id: "2" } } } } ], @@ -93,13 +93,13 @@ module ActiveModel id: "1", content: "AMS Contributor", links: { - author: { linkage: { type: "author", id: "1" } } + author: { linkage: { type: "authors", id: "1" } } } }, { id: "2", content: "Rails Contributor", links: { - author: { linkage: { type: "author", id: "2" } } + author: { linkage: { type: "authors", id: "2" } } } } ] @@ -111,8 +111,8 @@ module ActiveModel body: "Hello, world!!", links: { comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } }, { @@ -121,8 +121,8 @@ module ActiveModel body: "Body", links: { comments: { linkage: [] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "2" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "2" } } } } ] @@ -150,7 +150,7 @@ module ActiveModel links: { posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] }, roles: { linkage: [] }, - bio: { linkage: { type: "bio", id: "1" }} + bio: { linkage: { type: "bios", id: "1" }} } } ], @@ -161,8 +161,8 @@ module ActiveModel body: "Hello, world!!", links: { comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } }, { id: "30", @@ -170,8 +170,8 @@ module ActiveModel body: "Body", links: { comments: { linkage: [] }, - blog: { linkage: { type: "blog", id: "999" } }, - author: { linkage: { type: "author", id: "1" } } + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } } ] From 33f3a88ba0327a0991fbea081fb61ed5d52771b4 Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Fri, 20 Mar 2015 16:04:33 -0400 Subject: [PATCH 06/13] Implement `included` and `id` and `type` as per spec --- lib/active_model/serializer.rb | 10 +++ .../serializer/adapter/json_api.rb | 27 +++---- .../adapter_selector_test.rb | 12 ++- .../action_controller/json_api_linked_test.rb | 47 ++++++----- .../serialization_scope_name_test.rb | 16 ++-- test/action_controller/serialization_test.rb | 81 ++++++++++++++----- test/adapter/json_api/belongs_to_test.rb | 27 ++++--- test/adapter/json_api/collection_test.rb | 10 ++- .../has_many_explicit_serializer_test.rb | 25 +++--- test/adapter/json_api/has_many_test.rb | 8 +- test/adapter/json_api/has_one_test.rb | 3 +- test/adapter/json_api/linked_test.rb | 67 ++++++++------- 12 files changed, 203 insertions(+), 130 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index b6193400..588abc99 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -164,6 +164,14 @@ module ActiveModel end end + def id + object.id if object + end + + def type + object.class.to_s.demodulize.underscore.pluralize + end + def attributes(options = {}) attributes = if options[:fields] @@ -172,6 +180,8 @@ module ActiveModel self.class._attributes.dup end + attributes += options[:required_fields] if options[:required_fields] + attributes.each_with_object({}) do |name, hash| hash[name] = send(name) end diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index ef51ea14..69e69028 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -35,11 +35,9 @@ module ActiveModel private def add_links(resource, name, serializers) - type = serialized_object_type(serializers) resource[:links] ||= {} - resource[:links][name] ||= { linkage: [] } - resource[:links][name][:linkage] += serializers.map { |serializer| { type: type, id: serializer.id.to_s } } + resource[:links][name][:linkage] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } } end def add_link(resource, name, serializer) @@ -47,9 +45,7 @@ module ActiveModel resource[:links][name] = { linkage: nil } if serializer && serializer.object - type = serialized_object_type(serializer) - - resource[:links][name][:linkage] = { type: type, id: serializer.id.to_s } + resource[:links][name][:linkage] = { type: serializer.type, id: serializer.id.to_s } end end @@ -58,17 +54,15 @@ module ActiveModel resource_path = [parent, resource_name].compact.join('.') - if include_assoc?(resource_path) && resource_type = serialized_object_type(serializers) - plural_name = resource_type.pluralize.to_sym - @top[:linked] ||= {} - @top[:linked][plural_name] ||= [] + if include_assoc?(resource_path) + @top[:included] ||= [] serializers.each do |serializer| attrs = attributes_for_serializer(serializer, @options) add_resource_links(attrs, serializer, add_included: false) - @top[:linked][plural_name].push(attrs) unless @top[:linked][plural_name].include?(attrs) + @top[:included].push(attrs) unless @top[:included].include?(attrs) end end @@ -85,14 +79,16 @@ module ActiveModel result = [] serializer.each do |object| options[:fields] = @fieldset && @fieldset.fields_for(serializer) + options[:required_fields] = [:id, :type] attributes = object.attributes(options) - attributes[:id] = attributes[:id].to_s if attributes[:id] + attributes[:id] = attributes[:id].to_s result << attributes end else options[:fields] = @fieldset && @fieldset.fields_for(serializer) + options[:required_fields] = [:id, :type] result = serializer.attributes(options) - result[:id] = result[:id].to_s if result[:id] + result[:id] = result[:id].to_s end result @@ -116,11 +112,6 @@ module ActiveModel end end - def serialized_object_type(serializer) - return false unless Array(serializer).first - Array(serializer).first.object.class.to_s.demodulize.underscore.pluralize - end - def add_resource_links(attrs, serializer, options = {}) options[:add_included] = options.fetch(:add_included, true) diff --git a/test/action_controller/adapter_selector_test.rb b/test/action_controller/adapter_selector_test.rb index ea266881..efa61f7b 100644 --- a/test/action_controller/adapter_selector_test.rb +++ b/test/action_controller/adapter_selector_test.rb @@ -29,7 +29,17 @@ module ActionController def test_render_using_adapter_override get :render_using_adapter_override - assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', response.body + + expected = { + data: { + name: "Name 1", + description: "Description 1", + id: assigns(:profile).id.to_s, + type: "profiles" + } + } + + assert_equal expected.to_json, response.body end def test_render_skipping_adapter diff --git a/test/action_controller/json_api_linked_test.rb b/test/action_controller/json_api_linked_test.rb index 3f8b45eb..321974e3 100644 --- a/test/action_controller/json_api_linked_test.rb +++ b/test/action_controller/json_api_linked_test.rb @@ -83,80 +83,87 @@ module ActionController def test_render_resource_without_include get :render_resource_without_include response = JSON.parse(@response.body) - refute response.key? 'linked' + refute response.key? 'included' end def test_render_resource_with_include get :render_resource_with_include response = JSON.parse(@response.body) - assert response.key? 'linked' - assert_equal 1, response['linked']['authors'].size - assert_equal 'Steve K.', response['linked']['authors'].first['name'] + assert response.key? 'included' + assert_equal 1, response['included'].size + assert_equal 'Steve K.', response['included'].first['name'] end def test_render_resource_with_nested_has_many_include get :render_resource_with_nested_has_many_include response = JSON.parse(@response.body) - expected_linked = { - "authors" => [{ + expected_linked = [ + { "id" => "1", + "type" => "authors", "name" => "Steve K.", "links" => { "posts" => { "linkage" => [] }, "roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] }, "bio" => { "linkage" => nil } } - }], - "roles"=>[{ + }, { "id" => "1", + "type" => "roles", "name" => "admin", "links" => { "author" => { "linkage" => { "type" =>"authors", "id" => "1" } } } }, { "id" => "2", + "type" => "roles", "name" => "colab", "links" => { "author" => { "linkage" => { "type" =>"authors", "id" => "1" } } } - }] - } - assert_equal expected_linked, response['linked'] + } + ] + assert_equal expected_linked, response['included'] end def test_render_resource_with_nested_include get :render_resource_with_nested_include response = JSON.parse(@response.body) - assert response.key? 'linked' - assert_equal 1, response['linked']['authors'].size - assert_equal 'Anonymous', response['linked']['authors'].first['name'] + assert response.key? 'included' + assert_equal 1, response['included'].size + assert_equal 'Anonymous', response['included'].first['name'] end def test_render_collection_without_include get :render_collection_without_include response = JSON.parse(@response.body) - refute response.key? 'linked' + refute response.key? 'included' end def test_render_collection_with_include get :render_collection_with_include response = JSON.parse(@response.body) - assert response.key? 'linked' + assert response.key? 'included' end def test_render_resource_with_nested_attributes_even_when_missing_associations get :render_resource_with_missing_nested_has_many_include response = JSON.parse(@response.body) - assert response.key? 'linked' - refute response['linked'].key? 'roles' + assert response.key? 'included' + refute has_type?(response['included'], 'roles') end def test_render_collection_with_missing_nested_has_many_include get :render_collection_with_missing_nested_has_many_include response = JSON.parse(@response.body) - assert response.key? 'linked' - assert response['linked'].key? 'roles' + assert response.key? 'included' + assert has_type?(response['included'], 'roles') end + + def has_type?(collection, value) + collection.detect { |i| i['type'] == value} + end + end end end diff --git a/test/action_controller/serialization_scope_name_test.rb b/test/action_controller/serialization_scope_name_test.rb index df77231a..65f44c00 100644 --- a/test/action_controller/serialization_scope_name_test.rb +++ b/test/action_controller/serialization_scope_name_test.rb @@ -2,7 +2,7 @@ require 'test_helper' require 'pathname' class DefaultScopeNameTest < ActionController::TestCase - TestUser = Struct.new(:name, :admin) + TestUser = Struct.new(:id, :name, :admin) class UserSerializer < ActiveModel::Serializer attributes :admin? @@ -17,11 +17,11 @@ class DefaultScopeNameTest < ActionController::TestCase before_filter { request.format = :json } def current_user - TestUser.new('Pete', false) + TestUser.new(1, 'Pete', false) end def render_new_user - render json: TestUser.new('pete', false), serializer: UserSerializer, adapter: :json_api + render json: TestUser.new(1, 'pete', false), serializer: UserSerializer, adapter: :json_api end end @@ -29,12 +29,12 @@ class DefaultScopeNameTest < ActionController::TestCase def test_default_scope_name get :render_new_user - assert_equal '{"data":{"admin?":false}}', @response.body + assert_equal '{"data":{"admin?":false,"id":"1","type":"test_users"}}', @response.body end end class SerializationScopeNameTest < ActionController::TestCase - TestUser = Struct.new(:name, :admin) + TestUser = Struct.new(:id, :name, :admin) class AdminUserSerializer < ActiveModel::Serializer attributes :admin? @@ -50,11 +50,11 @@ class SerializationScopeNameTest < ActionController::TestCase before_filter { request.format = :json } def current_admin - TestUser.new('Bob', true) + TestUser.new(1, 'Bob', true) end def render_new_user - render json: TestUser.new('pete', false), serializer: AdminUserSerializer, adapter: :json_api + render json: TestUser.new(1, 'pete', false), serializer: AdminUserSerializer, adapter: :json_api end end @@ -62,6 +62,6 @@ class SerializationScopeNameTest < ActionController::TestCase def test_override_scope_name_with_controller get :render_new_user - assert_equal '{"data":{"admin?":true}}', @response.body + assert_equal '{"data":{"admin?":true,"id":"1","type":"test_users"}}', @response.body end end \ No newline at end of file diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index 28325ede..b47db521 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -15,13 +15,11 @@ module ActionController end def render_using_default_adapter_root - old_adapter = ActiveModel::Serializer.config.adapter - # JSON-API adapter sets root by default - ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile - ensure - ActiveModel::Serializer.config.adapter = old_adapter + with_adapter ActiveModel::Serializer::Adapter::JsonApi do + # JSON-API adapter sets root by default + @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) + render json: @profile + end end def render_using_custom_root_in_adapter_with_a_default @@ -39,15 +37,14 @@ module ActionController end def render_array_using_implicit_serializer_and_meta - old_adapter = ActiveModel::Serializer.config.adapter + with_adapter ActiveModel::Serializer::Adapter::JsonApi do - ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi - array = [ - Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - ] - render json: array, meta: { total: 10 } - ensure - ActiveModel::Serializer.config.adapter = old_adapter + @profiles = [ + Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) + ] + + render json: @profiles, meta: { total: 10 } + end end def render_object_with_cache_enabled @@ -88,6 +85,15 @@ module ActionController adapter = ActiveModel::Serializer.adapter.new(serializer) adapter.to_json end + + def with_adapter(adapter) + old_adapter = ActiveModel::Serializer.config.adapter + # JSON-API adapter sets root by default + ActiveModel::Serializer.config.adapter = adapter + yield + ensure + ActiveModel::Serializer.config.adapter = old_adapter + end end tests MyController @@ -96,8 +102,13 @@ module ActionController def test_render_using_implicit_serializer get :render_using_implicit_serializer + expected = { + name: "Name 1", + description: "Description 1" + } + assert_equal 'application/json', @response.content_type - assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body + assert_equal expected.to_json, @response.body end def test_render_using_custom_root @@ -110,15 +121,33 @@ module ActionController def test_render_using_default_root get :render_using_default_adapter_root + expected = { + data: { + name: "Name 1", + description: "Description 1", + id: assigns(:profile).id.to_s, + type: "profiles" + } + } + assert_equal 'application/json', @response.content_type - assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body + assert_equal expected.to_json, @response.body end def test_render_using_custom_root_in_adapter_with_a_default get :render_using_custom_root_in_adapter_with_a_default + expected = { + data: { + name: "Name 1", + description: "Description 1", + id: assigns(:profile).id.to_s, + type: "profiles" + } + } + assert_equal 'application/json', @response.content_type - assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body + assert_equal expected.to_json, @response.body end def test_render_array_using_implicit_serializer @@ -142,8 +171,22 @@ module ActionController def test_render_array_using_implicit_serializer_and_meta get :render_array_using_implicit_serializer_and_meta + expected = { + data: [ + { + name: "Name 1", + description: "Description 1", + id: assigns(:profiles).first.id.to_s, + type: "profiles" + } + ], + meta: { + total: 10 + } + } + assert_equal 'application/json', @response.content_type - assert_equal '{"data":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body + assert_equal expected.to_json, @response.body end def test_render_with_cache_enable diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index 38f80800..3ce8074e 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -41,6 +41,7 @@ module ActiveModel @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post') expected = [{ id: "42", + type: "posts", title: 'New Post', body: 'Body', links: { @@ -49,12 +50,14 @@ module ActiveModel author: { linkage: { type: "authors", id: "1" } } } }] - assert_equal expected, @adapter.serializable_hash[:linked][:posts] + assert_equal expected, @adapter.serializable_hash[:included] end def test_limiting_linked_post_fields @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]}) expected = [{ + id: "42", + type: "posts", title: 'New Post', links: { comments: { linkage: [ { type: "comments", id: "1" } ] }, @@ -62,7 +65,7 @@ module ActiveModel author: { linkage: { type: "authors", id: "1" } } } }] - assert_equal expected, @adapter.serializable_hash[:linked][:posts] + assert_equal expected, @adapter.serializable_hash[:included] end def test_include_nil_author @@ -102,37 +105,39 @@ module ActiveModel def test_include_linked_resources_with_type_name serializer = BlogSerializer.new(@blog) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: ['writer', 'articles']) - linked = adapter.serializable_hash[:linked] - expected = { - authors: [{ + linked = adapter.serializable_hash[:included] + expected = [ + { id: "1", + type: "authors", name: "Steve K.", links: { posts: { linkage: [] }, roles: { linkage: [] }, bio: { linkage: nil } } - }], - posts: [{ + },{ + id: "42", + type: "posts", title: "New Post", body: "Body", - id: "42", links: { comments: { linkage: [ { type: "comments", id: "1" } ] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: { type: "authors", id: "1" } } } }, { + id: "43", + type: "posts", title: "Hello!!", body: "Hello, world!!", - id: "43", links: { comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil } } - }] - } + } + ] assert_equal expected, linked end end diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index c0216c36..36fb68e2 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -27,9 +27,10 @@ module ActiveModel def test_include_multiple_posts expected = [ { + id: "1", + type: "posts", title: "Hello!!", body: "Hello, world!!", - id: "1", links: { comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, @@ -37,9 +38,10 @@ module ActiveModel } }, { + id: "2", + type: "posts", title: "New Post", body: "Body", - id: "2", links: { comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, @@ -56,6 +58,8 @@ module ActiveModel expected = [ { + id: "1", + type: "posts", title: "Hello!!", links: { comments: { linkage: [] }, @@ -64,6 +68,8 @@ module ActiveModel } }, { + id: "2", + type: "posts", title: "New Post", links: { comments: { linkage: [] }, diff --git a/test/adapter/json_api/has_many_explicit_serializer_test.rb b/test/adapter/json_api/has_many_explicit_serializer_test.rb index ecebbaa7..4ff53728 100644 --- a/test/adapter/json_api/has_many_explicit_serializer_test.rb +++ b/test/adapter/json_api/has_many_explicit_serializer_test.rb @@ -39,25 +39,33 @@ module ActiveModel assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) end - def test_includes_linked_comments + def test_includes_linked_data # If CommentPreviewSerializer is applied correctly the body text will not be present in the output expected = [ { id: '1', + type: 'comments', links: { post: { linkage: { type: 'posts', id: @post.id.to_s } } } }, { id: '2', + type: 'comments', links: { post: { linkage: { type: 'posts', id: @post.id.to_s } } } + }, + { + id: @author.id.to_s, + type: "authors", + links: { + posts: { linkage: [ {type: "posts", id: @post.id.to_s } ] } + } } ] - assert_equal(expected, - @adapter.serializable_hash[:linked][:comments]) + assert_equal(expected, @adapter.serializable_hash[:included]) end def test_includes_author_id @@ -68,17 +76,6 @@ module ActiveModel assert_equal(expected, @adapter.serializable_hash[:data][:links][:author]) end - def test_includes_linked_authors - expected = [{ - id: @author.id.to_s, - links: { - posts: { linkage: [ { type: "posts", id: @post.id.to_s } ] } - } - }] - - assert_equal(expected, @adapter.serializable_hash[:linked][:authors]) - end - def test_explicit_serializer_with_null_resource @post.author = nil diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index b520e5b0..cdd4bf3e 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -42,6 +42,7 @@ module ActiveModel @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments') expected = [{ id: "1", + type: "comments", body: 'ZOMG A COMMENT', links: { post: { linkage: { type: "posts", id: "1" } }, @@ -49,31 +50,34 @@ module ActiveModel } }, { id: "2", + type: "comments", body: 'ZOMG ANOTHER COMMENT', links: { post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } }] - assert_equal expected, @adapter.serializable_hash[:linked][:comments] + assert_equal expected, @adapter.serializable_hash[:included] end def test_limit_fields_of_linked_comments @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]}) expected = [{ id: "1", + type: "comments", links: { post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } }, { id: "2", + type: "comments", links: { post: { linkage: { type: "posts", id: "1" } }, author: { linkage: nil } } }] - assert_equal expected, @adapter.serializable_hash[:linked][:comments] + assert_equal expected, @adapter.serializable_hash[:included] end def test_no_include_linked_if_comments_is_empty diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb index 267ec4e5..8847e020 100644 --- a/test/adapter/json_api/has_one_test.rb +++ b/test/adapter/json_api/has_one_test.rb @@ -41,6 +41,7 @@ module ActiveModel expected = [ { id: "43", + type: "bios", content:"AMS Contributor", links: { author: { linkage: { type: "authors", id: "1" } } @@ -48,7 +49,7 @@ module ActiveModel } ] - assert_equal(expected, @adapter.serializable_hash[:linked][:bios]) + assert_equal(expected, @adapter.serializable_hash[:included]) end end end diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index e85c4b7d..c6fada87 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -142,42 +142,41 @@ module ActiveModel include: 'author,author.posts' ) - expected = { - authors: [ - { - id: "1", - name: "Steve K.", - links: { - posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] }, - roles: { linkage: [] }, - bio: { linkage: { type: "bios", id: "1" }} - } + expected = [ + { + id: "1", + type: "authors", + name: "Steve K.", + links: { + posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] }, + roles: { linkage: [] }, + bio: { linkage: { type: "bios", id: "1" }} } - ], - posts: [ - { - id: "10", - title: "Hello!!", - body: "Hello, world!!", - links: { - comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] }, - blog: { linkage: { type: "blogs", id: "999" } }, - author: { linkage: { type: "authors", id: "1" } } - } - }, { - id: "30", - title: "Yet Another Post", - body: "Body", - links: { - comments: { linkage: [] }, - blog: { linkage: { type: "blogs", id: "999" } }, - author: { linkage: { type: "authors", id: "1" } } - } + }, { + id: "10", + type: "posts", + title: "Hello!!", + body: "Hello, world!!", + links: { + comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] }, + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } } - ] - } - assert_equal expected, adapter.serializable_hash[:linked] - assert_equal expected, alt_adapter.serializable_hash[:linked] + }, { + id: "30", + type: "posts", + title: "Yet Another Post", + body: "Body", + links: { + comments: { linkage: [] }, + blog: { linkage: { type: "blogs", id: "999" } }, + author: { linkage: { type: "authors", id: "1" } } + } + } + ] + + assert_equal expected, adapter.serializable_hash[:included] + assert_equal expected, alt_adapter.serializable_hash[:included] end def test_ignore_model_namespace_for_linked_resource_type From 294d06624ffc918388e7da4793387d61b9f43aa2 Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Fri, 20 Mar 2015 16:06:58 -0400 Subject: [PATCH 07/13] Remove unused embed option --- lib/active_model/serializer/adapter/json_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 69e69028..7774cd13 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -124,7 +124,7 @@ module ActiveModel add_link(attrs, name, association) end - if @options[:embed] != :ids && options[:add_included] + if options[:add_included] Array(association).each do |association| add_included(name, association) end From 946d1dba14b41dbdf2fe44368642d7ae54ac8abe Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Fri, 20 Mar 2015 16:14:52 -0400 Subject: [PATCH 08/13] Remove debug include --- test/test_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index b71a4b57..f3977b61 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,5 @@ require 'bundler/setup' -require 'pp' + require 'rails' require 'action_controller' require 'action_controller/test_case' From 4fcacb0b16a50df9fa11d397e2cfee8cd47b318e Mon Sep 17 00:00:00 2001 From: Mateo Murphy <33degrees@gmail.com> Date: Sat, 21 Mar 2015 10:50:50 -0400 Subject: [PATCH 09/13] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f85e9cff..b2079e3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,4 +2,5 @@ * adds support for `meta` and `meta_key` [@kurko] * adds method to override association [adcb99e, @kurko] - * add `has_one` attribute for backwards compatibility [@ggordon] + * adds `has_one` attribute for backwards compatibility [@ggordon] + * updates JSON API support to RC3 [@mateomurphy] From ef3bfdd1e9588e56cce80b787352bb5cdbff9f1f Mon Sep 17 00:00:00 2001 From: Mateo Murphy <33degrees@gmail.com> Date: Sat, 21 Mar 2015 10:58:57 -0400 Subject: [PATCH 10/13] Update README.md Indicate support for RC3 of JSON API --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4f75004a..127b0451 100644 --- a/README.md +++ b/README.md @@ -181,9 +181,9 @@ end #### JSONAPI -This adapter follows the format specified in +This adapter follows RC3 of the format specified in [jsonapi.org/format](http://jsonapi.org/format). It will include the associated -resources in the `"linked"` member when the resource names are included in the +resources in the `"included"` member when the resource names are included in the `include` option. ```ruby From b6951809b07010b6c9a4081840882e5e07815b11 Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Sun, 22 Mar 2015 19:32:22 -0400 Subject: [PATCH 11/13] Add test for required fields --- test/serializers/attributes_test.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/serializers/attributes_test.rb b/test/serializers/attributes_test.rb index 7c4bbc8b..692b4b4a 100644 --- a/test/serializers/attributes_test.rb +++ b/test/serializers/attributes_test.rb @@ -15,7 +15,13 @@ module ActiveModel def test_attributes_with_fields_option assert_equal({name: 'Name 1'}, - @profile_serializer.attributes( { fields: [:name] } ) ) + @profile_serializer.attributes(fields: [:name])) + end + + def test_required_fields + assert_equal({name: 'Name 1', description: 'Description 1'}, + @profile_serializer.attributes(fields: [:name, :description], required_fields: [:name])) + end end end From 90c7005c79ba984399b80759d07d0ba8a7cb1697 Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Mon, 23 Mar 2015 12:38:15 -0400 Subject: [PATCH 12/13] Don't store the root as we don't need it elsewhere --- lib/active_model/serializer/adapter/json_api.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 7774cd13..96a3f442 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -16,16 +16,14 @@ module ActiveModel end def serializable_hash(options = {}) - @root = :data - if serializer.respond_to?(:each) - @hash[@root] = serializer.map do |s| - self.class.new(s, @options.merge(top: @top, fieldset: @fieldset)).serializable_hash[@root] + @hash[:data] = serializer.map do |s| + self.class.new(s, @options.merge(top: @top, fieldset: @fieldset)).serializable_hash[:data] end else @hash = cached_object do - @hash[@root] = attributes_for_serializer(serializer, @options) - add_resource_links(@hash[@root], serializer) + @hash[:data] = attributes_for_serializer(serializer, @options) + add_resource_links(@hash[:data], serializer) @hash end end From 9480b567efe1d56934aac2d23b63277b8c520b7a Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Mon, 23 Mar 2015 13:44:30 -0400 Subject: [PATCH 13/13] Refactor TestUser in SerializationScopeNameTest Use the same base class we use for other test models --- .../serialization_scope_name_test.rb | 16 ++++++---------- test/fixtures/poro.rb | 1 + 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/test/action_controller/serialization_scope_name_test.rb b/test/action_controller/serialization_scope_name_test.rb index 65f44c00..7a406e7a 100644 --- a/test/action_controller/serialization_scope_name_test.rb +++ b/test/action_controller/serialization_scope_name_test.rb @@ -2,8 +2,6 @@ require 'test_helper' require 'pathname' class DefaultScopeNameTest < ActionController::TestCase - TestUser = Struct.new(:id, :name, :admin) - class UserSerializer < ActiveModel::Serializer attributes :admin? def admin? @@ -17,11 +15,11 @@ class DefaultScopeNameTest < ActionController::TestCase before_filter { request.format = :json } def current_user - TestUser.new(1, 'Pete', false) + User.new(id: 1, name: 'Pete', admin: false) end def render_new_user - render json: TestUser.new(1, 'pete', false), serializer: UserSerializer, adapter: :json_api + render json: User.new(id: 1, name: 'Pete', admin: false), serializer: UserSerializer, adapter: :json_api end end @@ -29,13 +27,11 @@ class DefaultScopeNameTest < ActionController::TestCase def test_default_scope_name get :render_new_user - assert_equal '{"data":{"admin?":false,"id":"1","type":"test_users"}}', @response.body + assert_equal '{"data":{"admin?":false,"id":"1","type":"users"}}', @response.body end end class SerializationScopeNameTest < ActionController::TestCase - TestUser = Struct.new(:id, :name, :admin) - class AdminUserSerializer < ActiveModel::Serializer attributes :admin? def admin? @@ -50,11 +46,11 @@ class SerializationScopeNameTest < ActionController::TestCase before_filter { request.format = :json } def current_admin - TestUser.new(1, 'Bob', true) + User.new(id: 2, name: 'Bob', admin: true) end def render_new_user - render json: TestUser.new(1, 'pete', false), serializer: AdminUserSerializer, adapter: :json_api + render json: User.new(id: 1, name: 'Pete', admin: false), serializer: AdminUserSerializer, adapter: :json_api end end @@ -62,6 +58,6 @@ class SerializationScopeNameTest < ActionController::TestCase def test_override_scope_name_with_controller get :render_new_user - assert_equal '{"data":{"admin?":true,"id":"1","type":"test_users"}}', @response.body + assert_equal '{"data":{"admin?":true,"id":"1","type":"users"}}', @response.body end end \ No newline at end of file diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index d04d79dd..6ce52e44 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -63,6 +63,7 @@ Author = Class.new(Model) Bio = Class.new(Model) Blog = Class.new(Model) Role = Class.new(Model) +User = Class.new(Model) module Spam; end Spam::UnrelatedLink = Class.new(Model)