From a9cb0971098d0638057c7c7c8a15c981ea34a2d8 Mon Sep 17 00:00:00 2001 From: Wasif Hossain Date: Thu, 21 Feb 2019 03:03:35 +0600 Subject: [PATCH] Refactoring --- lib/active_model_serializers/adapter/attributes.rb | 4 +++- test/adapter/json/fields_test.rb | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/active_model_serializers/adapter/attributes.rb b/lib/active_model_serializers/adapter/attributes.rb index af95797a..3372e2be 100644 --- a/lib/active_model_serializers/adapter/attributes.rb +++ b/lib/active_model_serializers/adapter/attributes.rb @@ -16,6 +16,8 @@ module ActiveModelSerializers self.class.transform_key_casing!(serialized_hash, instance_options) end + private + def fields_to_fieldset(fields) return fields if fields.nil? resource_fields = [] @@ -27,7 +29,7 @@ module ActiveModelSerializers else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'" end end - relationship_fields.merge(serializer.json_key.to_sym => resource_fields) + relationship_fields.merge!(serializer.json_key.to_sym => resource_fields) end end end diff --git a/test/adapter/json/fields_test.rb b/test/adapter/json/fields_test.rb index f44a242f..04da0017 100644 --- a/test/adapter/json/fields_test.rb +++ b/test/adapter/json/fields_test.rb @@ -17,7 +17,7 @@ module ActiveModelSerializers end class PostSerializer < ActiveModel::Serializer - type 'posts' + type 'post' attributes :title, :body belongs_to :author has_many :comments @@ -28,7 +28,7 @@ module ActiveModelSerializers end class CommentSerializer < ActiveModel::Serializer - type 'comments' + type 'comment' attributes :title, :body belongs_to :author end @@ -47,7 +47,7 @@ module ActiveModelSerializers fields = [:title] hash = serializable(@post, adapter: :json, fields: fields, include: []).serializable_hash expected = { title: 'Title 1' } - assert_equal(expected, hash[:posts]) + assert_equal(expected, hash[:post]) end def test_fields_included @@ -55,7 +55,7 @@ module ActiveModelSerializers hash = serializable(@post, adapter: :json, include: [:comments], fields: fields).serializable_hash expected = [{ body: @comment1.body }, { body: @comment2.body }] - assert_equal(expected, hash[:posts][:comments]) + assert_equal(expected, hash[:post][:comments]) end end end