From 6f4f30cda6b4c11c6885e3a5d94469afbe77586b Mon Sep 17 00:00:00 2001 From: Gauthier Delacroix Date: Fri, 5 Sep 2014 18:46:08 +0200 Subject: [PATCH 1/4] Allow JSONAPI unsuffixed associations keys As shown here : https://github.com/json-api/json-api/blob/gh-pages/format/index.md#resource-relationships- --- lib/active_model/serializer/association/has_many.rb | 6 +++++- lib/active_model/serializer/association/has_one.rb | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/active_model/serializer/association/has_many.rb b/lib/active_model/serializer/association/has_many.rb index d1b8d8eb..52d66d26 100644 --- a/lib/active_model/serializer/association/has_many.rb +++ b/lib/active_model/serializer/association/has_many.rb @@ -5,7 +5,11 @@ module ActiveModel def initialize(name, *args) super @root_key = @embedded_key - @key ||= "#{name.to_s.singularize}_ids" + @key ||= case CONFIG.default_key_type + when :name then name.to_s.pluralize + else "#{name.to_s.singularize}_ids" + end + end def serializer_class(object, _) diff --git a/lib/active_model/serializer/association/has_one.rb b/lib/active_model/serializer/association/has_one.rb index 3b9acddf..38c8b9bc 100644 --- a/lib/active_model/serializer/association/has_one.rb +++ b/lib/active_model/serializer/association/has_one.rb @@ -5,7 +5,10 @@ module ActiveModel def initialize(name, *args) super @root_key = @embedded_key.to_s.pluralize - @key ||= "#{name}_id" + @key ||= case CONFIG.default_key_type + when :name then name.to_s.singularize + else "#{name}_id" + end end def serializer_class(object, options = {}) From 4ac07de799a18b0b751e83e64d4e5586008fe961 Mon Sep 17 00:00:00 2001 From: Gauthier Delacroix Date: Wed, 8 Oct 2014 16:20:49 +0200 Subject: [PATCH 2/4] Unsuffixed association keys tests --- .../active_model/serializer/has_many_test.rb | 26 +++++++++++++++++++ .../active_model/serializer/has_one_test.rb | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/test/unit/active_model/serializer/has_many_test.rb b/test/unit/active_model/serializer/has_many_test.rb index 2e5d212f..d1bb83ad 100644 --- a/test/unit/active_model/serializer/has_many_test.rb +++ b/test/unit/active_model/serializer/has_many_test.rb @@ -242,6 +242,32 @@ module ActiveModel } }, @post_serializer.as_json) end + + CONFIG.default_key_type = :name + class NameKeyPostSerializer < ActiveModel::Serializer + attributes :title, :body + + has_many :comments + end + CONFIG.default_key_type = nil + + def test_associations_name_key_embedding_ids_serialization_using_serializable_hash + @association = NameKeyPostSerializer._associations[:comments] + @association.embed = :ids + + assert_equal({ + title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } + }, NameKeyPostSerializer.new(@post).serializable_hash) + end + + def test_associations_name_key_embedding_ids_serialization_using_as_json + @association = NameKeyPostSerializer._associations[:comments] + @association.embed = :ids + + assert_equal({ + 'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } } + }, NameKeyPostSerializer.new(@post).as_json) + end end end end diff --git a/test/unit/active_model/serializer/has_one_test.rb b/test/unit/active_model/serializer/has_one_test.rb index 52fac88d..521c16d5 100644 --- a/test/unit/active_model/serializer/has_one_test.rb +++ b/test/unit/active_model/serializer/has_one_test.rb @@ -216,6 +216,32 @@ module ActiveModel } }, @user_serializer.as_json) end + + CONFIG.default_key_type = :name + class NameKeyUserSerializer < ActiveModel::Serializer + attributes :name, :email + + has_one :profile + end + CONFIG.default_key_type = nil + + def test_associations_name_key_embedding_ids_serialization_using_serializable_hash + @association = NameKeyUserSerializer._associations[:profile] + @association.embed = :ids + + assert_equal({ + name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id + }, NameKeyUserSerializer.new(@user).serializable_hash) + end + + def test_associations_name_key_embedding_ids_serialization_using_as_json + @association = NameKeyUserSerializer._associations[:profile] + @association.embed = :ids + + assert_equal({ + 'name_key_user' => { name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id } + }, NameKeyUserSerializer.new(@user).as_json) + end end end end From 226cbc416038d7a8fb9bf277276e886df1792ae5 Mon Sep 17 00:00:00 2001 From: Gauthier Delacroix Date: Wed, 8 Oct 2014 16:31:09 +0200 Subject: [PATCH 3/4] Unsuffixed association keys doc --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index fbcf9152..eff8a7b0 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,18 @@ end BlogSerializer.new(object, key_format: :lower_camel) ``` +## Changing the default association key type + +You can specify that serializers use unsuffixed names as association keys by default. + +`````ruby +ActiveModel::Serializer.setup do |config| + config.default_key_type = :name +end +```` + +This will build association keys like `comments` or `author` instead of `comment_ids` or `author_id`. + ## Getting the old version If you find that your project is already relying on the old rails to_json From a39d08d2ad563acdd6b56b84c341997de87a25b5 Mon Sep 17 00:00:00 2001 From: Gauthier Delacroix Date: Wed, 8 Oct 2014 16:51:44 +0200 Subject: [PATCH 4/4] Cleaner unsuffixed association keys tests --- test/fixtures/poro.rb | 22 +++++++++++++++++++ .../active_model/serializer/has_many_test.rb | 8 ------- .../active_model/serializer/has_one_test.rb | 8 ------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 14fa3e5a..50bb1795 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -142,3 +142,25 @@ module TestNamespace class ProfileSerializer < ::ProfileSerializer; end class UserSerializer < ::UserSerializer; end end + +ActiveModel::Serializer.setup do |config| + config.default_key_type = :name +end + +class NameKeyUserSerializer < ActiveModel::Serializer + attributes :name, :email + + has_one :profile +end + +class NameKeyPostSerializer < ActiveModel::Serializer + attributes :title, :body + + has_many :comments +end + +ActiveModel::Serializer.setup do |config| + config.default_key_type = nil +end + + diff --git a/test/unit/active_model/serializer/has_many_test.rb b/test/unit/active_model/serializer/has_many_test.rb index d1bb83ad..f8b2c738 100644 --- a/test/unit/active_model/serializer/has_many_test.rb +++ b/test/unit/active_model/serializer/has_many_test.rb @@ -243,14 +243,6 @@ module ActiveModel }, @post_serializer.as_json) end - CONFIG.default_key_type = :name - class NameKeyPostSerializer < ActiveModel::Serializer - attributes :title, :body - - has_many :comments - end - CONFIG.default_key_type = nil - def test_associations_name_key_embedding_ids_serialization_using_serializable_hash @association = NameKeyPostSerializer._associations[:comments] @association.embed = :ids diff --git a/test/unit/active_model/serializer/has_one_test.rb b/test/unit/active_model/serializer/has_one_test.rb index 521c16d5..9f7e393c 100644 --- a/test/unit/active_model/serializer/has_one_test.rb +++ b/test/unit/active_model/serializer/has_one_test.rb @@ -217,14 +217,6 @@ module ActiveModel }, @user_serializer.as_json) end - CONFIG.default_key_type = :name - class NameKeyUserSerializer < ActiveModel::Serializer - attributes :name, :email - - has_one :profile - end - CONFIG.default_key_type = nil - def test_associations_name_key_embedding_ids_serialization_using_serializable_hash @association = NameKeyUserSerializer._associations[:profile] @association.embed = :ids