Grape objects are conditionally exposed if the NOT nil field

In the grape object, I want to show the field only if it is present (not zero?) Without any luck.

I try to use this code, but it does not work properly, but I always hide the field.

expose :winner, :using => PlayerEntity, :unless => { :winner => nil }

I think the code itself explains what I really need, but, as I said, I am not getting the expected result.

Any clue?

+4
source share
1 answer

Ok, checking grape entity code. I realized that you need to pass this block as Ruby Proc. This code will work as expected:

expose :winner, :using => PlayerEntity, :unless => Proc.new {|g| g.winner.nil?}

Hope this helps someone. Greetings

+8
source

Source: https://habr.com/ru/post/1528240/


All Articles