Serializers now inherit attributes

This commit is contained in:
Yohan Robert
2015-04-08 16:51:28 +02:00
committed by groyoh
parent 1577969cb7
commit 02ffff599f
4 changed files with 57 additions and 4 deletions

View File

@@ -6,6 +6,11 @@ module ActiveModel
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 @@ module ActiveModel
@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