Skip to content

Commit

Permalink
Merge pull request rails-api#880 from groyoh/serializer-inheritance
Browse files Browse the repository at this point in the history
Inabling subclasses serializers to inherit attributes
  • Loading branch information
joaomdmoura committed May 18, 2015
2 parents 1e4ff26 + 02ffff5 commit d981ee5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion test/serializers/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

26 changes: 26 additions & 0 deletions test/serializers/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit d981ee5

Please sign in to comment.