Skip to content

Commit 02ffff5

Browse files
Yohan Robertgroyoh
authored andcommitted
Serializers now inherit attributes
1 parent 1577969 commit 02ffff5

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

lib/active_model/serializer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class << self
2222
end
2323

2424
def self.inherited(base)
25-
base._attributes = []
26-
base._attributes_keys = {}
27-
base._associations = {}
25+
base._attributes = self._attributes.try(:dup) || []
26+
base._attributes_keys = self._attributes_keys.try(:dup) || {}
27+
base._associations = self._associations.try(:dup) || {}
2828
base._urls = []
2929
end
3030

test/serializers/associations_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ def test_belongs_to_with_custom_method
101101

102102
assert blog_is_present
103103
end
104+
105+
def test_associations_inheritance
106+
inherited_klass = Class.new(PostSerializer)
107+
108+
assert_equal(PostSerializer._associations, inherited_klass._associations)
109+
end
110+
111+
def test_associations_inheritance_with_new_association
112+
inherited_klass = Class.new(PostSerializer) do
113+
has_many :top_comments, serializer: CommentSerializer
114+
end
115+
expected_associations = PostSerializer._associations.merge(
116+
top_comments: {
117+
type: :has_many,
118+
association_options: {
119+
serializer: CommentSerializer
120+
}
121+
}
122+
)
123+
assert_equal(inherited_klass._associations, expected_associations)
124+
end
104125
end
105126
end
106127
end

test/serializers/attribute_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ def test_json_serializable_hash
1717
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
1818
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
1919
end
20+
21+
def test_attribute_inheritance_with_key
22+
inherited_klass = Class.new(AlternateBlogSerializer)
23+
blog_serializer = inherited_klass.new(@blog)
24+
adapter = ActiveModel::Serializer::Adapter::Json.new(blog_serializer)
25+
assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
26+
end
2027
end
2128
end
2229
end
23-

test/serializers/attributes_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class AttributesTest < Minitest::Test
66
def setup
77
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
88
@profile_serializer = ProfileSerializer.new(@profile)
9+
@comment = Comment.new(id: 1, body: "ZOMG!!", date: "2015")
10+
@serializer_klass = Class.new(CommentSerializer)
11+
@serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
12+
attributes :date, :likes
13+
end
914
end
1015

1116
def test_attributes_definition
@@ -23,6 +28,27 @@ def test_required_fields
2328
@profile_serializer.attributes(fields: [:name, :description], required_fields: [:name]))
2429

2530
end
31+
32+
def test_attributes_inheritance_definition
33+
assert_equal([:id, :body], @serializer_klass._attributes)
34+
end
35+
36+
def test_attributes_inheritance
37+
serializer = @serializer_klass.new(@comment)
38+
assert_equal({id: 1, body: "ZOMG!!"},
39+
serializer.attributes)
40+
end
41+
42+
def test_attribute_inheritance_with_new_attribute_definition
43+
assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
44+
assert_equal([:id, :body], CommentSerializer._attributes)
45+
end
46+
47+
def test_attribute_inheritance_with_new_attribute
48+
serializer = @serializer_klass_with_new_attributes.new(@comment)
49+
assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil},
50+
serializer.attributes)
51+
end
2652
end
2753
end
2854
end

0 commit comments

Comments
 (0)