diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 969fa3083..9c7172861 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -22,9 +22,9 @@ class << self end def self.inherited(base) - base._attributes = [] - base._attributes_keys = {} - base._associations = {} + base._attributes = self._attributes.try(:dup) || [] + base._attributes_keys = self._attributes_keys.try(:dup) || {} + base._associations = self._associations.try(:dup) || {} base._urls = [] end diff --git a/test/serializers/associations_test.rb b/test/serializers/associations_test.rb index f5976a67c..ab481de7d 100644 --- a/test/serializers/associations_test.rb +++ b/test/serializers/associations_test.rb @@ -101,6 +101,27 @@ def test_belongs_to_with_custom_method assert blog_is_present end + + def test_associations_inheritance + inherited_klass = Class.new(PostSerializer) + + assert_equal(PostSerializer._associations, inherited_klass._associations) + end + + def test_associations_inheritance_with_new_association + inherited_klass = Class.new(PostSerializer) do + has_many :top_comments, serializer: CommentSerializer + end + expected_associations = PostSerializer._associations.merge( + top_comments: { + type: :has_many, + association_options: { + serializer: CommentSerializer + } + } + ) + assert_equal(inherited_klass._associations, expected_associations) + end end end end diff --git a/test/serializers/attribute_test.rb b/test/serializers/attribute_test.rb index 35f27ee4f..79d4ea098 100644 --- a/test/serializers/attribute_test.rb +++ b/test/serializers/attribute_test.rb @@ -17,7 +17,13 @@ def test_json_serializable_hash adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer) assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash) end + + def test_attribute_inheritance_with_key + inherited_klass = Class.new(AlternateBlogSerializer) + blog_serializer = inherited_klass.new(@blog) + adapter = ActiveModel::Serializer::Adapter::Json.new(blog_serializer) + assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash) + end end end end - diff --git a/test/serializers/attributes_test.rb b/test/serializers/attributes_test.rb index 692b4b4a1..32151afb1 100644 --- a/test/serializers/attributes_test.rb +++ b/test/serializers/attributes_test.rb @@ -6,6 +6,11 @@ class AttributesTest < Minitest::Test def setup @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @profile_serializer = ProfileSerializer.new(@profile) + @comment = Comment.new(id: 1, body: "ZOMG!!", date: "2015") + @serializer_klass = Class.new(CommentSerializer) + @serializer_klass_with_new_attributes = Class.new(CommentSerializer) do + attributes :date, :likes + end end def test_attributes_definition @@ -23,6 +28,27 @@ def test_required_fields @profile_serializer.attributes(fields: [:name, :description], required_fields: [:name])) end + + def test_attributes_inheritance_definition + assert_equal([:id, :body], @serializer_klass._attributes) + end + + def test_attributes_inheritance + serializer = @serializer_klass.new(@comment) + assert_equal({id: 1, body: "ZOMG!!"}, + serializer.attributes) + end + + def test_attribute_inheritance_with_new_attribute_definition + assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes) + assert_equal([:id, :body], CommentSerializer._attributes) + end + + def test_attribute_inheritance_with_new_attribute + serializer = @serializer_klass_with_new_attributes.new(@comment) + assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil}, + serializer.attributes) + end end end end