@@ -55,7 +55,7 @@ class UserResource1 < UserResource
55
55
end
56
56
57
57
class UserResource2 < UserResource
58
- attribute :username , if : proc { |user | user . id == 1 } do
58
+ attribute :username , if : proc { |user , _username | user . id == 1 } do
59
59
'username'
60
60
end
61
61
end
@@ -248,6 +248,43 @@ def test_conditional_attributes_with_if_and_resource_method
248
248
)
249
249
end
250
250
251
+ class UserResource14 < UserResource
252
+ one :profile , resource : ProfileResource , key : :my_profile , if : proc { |_user , profile | profile . email . end_with? ( 'com' ) }
253
+ end
254
+
255
+ def test_conditional_one_with_key_option
256
+ assert_equal (
257
+ '{"id":1,"my_profile":{"email":"test@example.com"}}' ,
258
+ UserResource14 . new ( @user ) . serialize
259
+ )
260
+ user = User . new ( 2 , 'Foo' )
261
+ profile = Profile . new ( 2 , 'test@example.org' )
262
+ user . profile = profile
263
+ assert_equal (
264
+ '{"id":2}' ,
265
+ UserResource14 . new ( user ) . serialize
266
+ )
267
+ end
268
+
269
+ class UserResource15 < UserResource
270
+ many :articles ,
271
+ proc { |articles | articles } , # dummy
272
+ resource : ArticleResource ,
273
+ if : proc { |_user , articles | !articles . empty? }
274
+ end
275
+
276
+ def test_conditional_many_with_condition_proc
277
+ assert_equal (
278
+ '{"id":1,"articles":[{"title":"Hello World!"}]}' ,
279
+ UserResource15 . new ( @user ) . serialize
280
+ )
281
+ user = User . new ( 2 , 'Foo' )
282
+ assert_equal (
283
+ '{"id":2}' ,
284
+ UserResource15 . new ( user ) . serialize
285
+ )
286
+ end
287
+
251
288
class Foo
252
289
attr_reader :id , :name
253
290
@@ -262,7 +299,7 @@ class FooResource
262
299
263
300
attributes :id
264
301
265
- nested :bar , if : proc { params [ :flag ] } do
302
+ nested :bar , if : proc { | foo , _ | params [ :flag ] || foo . id == 42 } do # dummy second parameter to test it doesn't raise an exception
266
303
attributes :name
267
304
attribute :baz do
268
305
'baz'
@@ -280,6 +317,11 @@ def test_conditional_attributes_with_nested_attributes
280
317
'{"id":1}' ,
281
318
FooResource . new ( foo , params : { flag : false } ) . serialize
282
319
)
320
+ foo42 = Foo . new ( 42 , 'name' )
321
+ assert_equal (
322
+ '{"id":42,"bar":{"name":"name","baz":"baz"}}' ,
323
+ FooResource . new ( foo42 , params : { flag : false } ) . serialize
324
+ )
283
325
end
284
326
285
327
class FooTypedResource
@@ -323,4 +365,29 @@ def test_conditional_attributes_with_type_and_resource_method
323
365
FooTypedResource2 . new ( foo , params : { flag : false } ) . serialize
324
366
)
325
367
end
368
+
369
+ class FooTypedResource3
370
+ include Alba ::Resource
371
+
372
+ attributes id : Integer
373
+ attributes name : :String , if : proc { |foo , name | foo . id == 1 || name == 'my name' }
374
+ end
375
+
376
+ def test_conditional_attributes_with_type_with_two_arity_condition
377
+ foo1 = Foo . new ( 1 , 'name' )
378
+ assert_equal (
379
+ '{"id":1,"name":"name"}' ,
380
+ FooTypedResource3 . new ( foo1 ) . serialize
381
+ )
382
+ foo2 = Foo . new ( 2 , 'name' )
383
+ assert_equal (
384
+ '{"id":2}' ,
385
+ FooTypedResource3 . new ( foo2 ) . serialize
386
+ )
387
+ foo3 = Foo . new ( 3 , 'my name' )
388
+ assert_equal (
389
+ '{"id":3,"name":"my name"}' ,
390
+ FooTypedResource3 . new ( foo3 ) . serialize
391
+ )
392
+ end
326
393
end
0 commit comments