mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #468 from stas/attrs_and_assoc_inheritance
Serializers now inherit attributes and associations.
This commit is contained in:
commit
adee76181f
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user