File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -238,6 +238,29 @@ def test_associations_namespaced_resources
238
238
end
239
239
end
240
240
end
241
+
242
+ def test_conditional_associations
243
+ serializer = Class . new ( ActiveModel ::Serializer ) do
244
+ belongs_to :if_assoc_included , if : :true
245
+ belongs_to :if_assoc_excluded , if : :false
246
+ belongs_to :unless_assoc_included , unless : :false
247
+ belongs_to :unless_assoc_excluded , unless : :true
248
+
249
+ def true
250
+ true
251
+ end
252
+
253
+ def false
254
+ false
255
+ end
256
+ end
257
+
258
+ model = ::Model . new
259
+ hash = serializable ( model , serializer : serializer ) . serializable_hash
260
+ expected = { if_assoc_included : nil , unless_assoc_included : nil }
261
+
262
+ assert_equal ( expected , hash )
263
+ end
241
264
end
242
265
end
243
266
end
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ module ActiveModel
4
4
class Serializer
5
5
class AttributeTest < ActiveSupport ::TestCase
6
6
def setup
7
- @blog = Blog . new ( { id : 1 , name : 'AMS Hints' , type : 'stuff' } )
7
+ @blog = Blog . new ( id : 1 , name : 'AMS Hints' , type : 'stuff' )
8
8
@blog_serializer = AlternateBlogSerializer . new ( @blog )
9
9
end
10
10
@@ -95,6 +95,29 @@ def test_virtual_attribute_block
95
95
96
96
assert_equal ( expected , hash )
97
97
end
98
+
99
+ def test_conditional_attributes
100
+ serializer = Class . new ( ActiveModel ::Serializer ) do
101
+ attribute :if_attribute_included , if : :true
102
+ attribute :if_attribute_excluded , if : :false
103
+ attribute :unless_attribute_included , unless : :false
104
+ attribute :unless_attribute_excluded , unless : :true
105
+
106
+ def true
107
+ true
108
+ end
109
+
110
+ def false
111
+ false
112
+ end
113
+ end
114
+
115
+ model = ::Model . new
116
+ hash = serializable ( model , serializer : serializer ) . serializable_hash
117
+ expected = { if_attribute_included : nil , unless_attribute_included : nil }
118
+
119
+ assert_equal ( expected , hash )
120
+ end
98
121
end
99
122
end
100
123
end
You can’t perform that action at this time.
0 commit comments