File tree Expand file tree Collapse file tree 3 files changed +20
-1
lines changed
lib/active_model/serializer Expand file tree Collapse file tree 3 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -97,11 +97,15 @@ def associations
97
97
return unless object
98
98
99
99
Enumerator . new do |y |
100
- self . class . _reflections . each do |reflection |
100
+ reflections . each do |reflection |
101
101
y . yield reflection . build_association ( self , options )
102
102
end
103
103
end
104
104
end
105
+
106
+ def reflections
107
+ self . class . _reflections
108
+ end
105
109
end
106
110
end
107
111
end
Original file line number Diff line number Diff line change @@ -102,6 +102,13 @@ def custom_options
102
102
end
103
103
end
104
104
105
+ CustomizablePostSerializer = Class . new ( PostSerializer ) do
106
+ def reflections
107
+ excludes = @options [ :excludes ] || [ ]
108
+ super . select { |r | excludes . exclude? ( r . name ) }
109
+ end
110
+ end
111
+
105
112
SpammyPostSerializer = Class . new ( ActiveModel ::Serializer ) do
106
113
attributes :id
107
114
has_many :related
Original file line number Diff line number Diff line change @@ -145,6 +145,14 @@ def test_associations_custom_keys
145
145
assert expected_association_keys . include? :writer
146
146
assert expected_association_keys . include? :site
147
147
end
148
+
149
+ def test_overriding_associations
150
+ serializer = CustomizablePostSerializer . new ( @post , excludes : [ :comments ] )
151
+
152
+ association_keys = serializer . associations . map ( &:key )
153
+
154
+ assert association_keys . exclude? :comments
155
+ end
148
156
end
149
157
end
150
158
end
You can’t perform that action at this time.
0 commit comments