From d82c599c687a47162c98b1af72e06ec8a43211b0 Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Fri, 20 Mar 2015 14:17:03 -0400 Subject: [PATCH] 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" } } } } ]