Serializers now inherit attributes and associations.

This commit is contained in:
Stas SUȘCOV 2013-12-11 17:22:07 +02:00
parent 2faa571a9c
commit 0febd8f628
3 changed files with 26 additions and 2 deletions

View File

@ -13,8 +13,8 @@ module ActiveModel
class << self
def inherited(base)
base._attributes = []
base._associations = {}
base._attributes = (self._attributes || []).dup
base._associations = (self._associations || {}).dup
end
def setup

View File

@ -24,6 +24,18 @@ module ActiveModel
'profile' => { name: 'Name 1', description: 'Description 1' }
}, @profile_serializer.as_json)
end
def test_attributes_inheritance
inherited_serializer_klass = Class.new(ProfileSerializer) do
attributes :comments
end
another_inherited_serializer_klass = Class.new(ProfileSerializer)
assert_equal([:name, :description, :comments],
inherited_serializer_klass._attributes)
assert_equal([:name, :description],
another_inherited_serializer_klass._attributes)
end
end
end
end

View File

@ -21,6 +21,18 @@ module ActiveModel
assert_equal 'comments', @association.name
end
def test_associations_inheritance
inherited_serializer_klass = Class.new(PostSerializer) do
has_many :some_associations
end
another_inherited_serializer_klass = Class.new(PostSerializer)
assert_equal(PostSerializer._associations.length + 1,
inherited_serializer_klass._associations.length)
assert_equal(PostSerializer._associations.length,
another_inherited_serializer_klass._associations.length)
end
def test_associations_embedding_ids_serialization_using_serializable_hash
@association.embed = :ids