7

In a grape-entity, I want to show a field only if present (not nil?) with no luck.

I'm trying this code but doesn't work as expected at all, but hiding the field always.

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

I think the code itself explains what I really need but, as I say, I'm not getting the expected result.

Any clue?

Jorge Diaz
  • 2,503
  • 1
  • 14
  • 15

2 Answers2

11

Ok, checking grape-entity's code I figured out you need to pass this block as a Ruby Proc. This code will work as expected:

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

Hope it helps someone. Cheers

Jorge Diaz
  • 2,503
  • 1
  • 14
  • 15
3

https://github.com/ruby-grape/grape-entity#expose-nil

Grape Entity now provides expose_nil option, so this should do the trick:

expose :winner, :using => PlayerEntity, expose_nil: false
lulalala
  • 17,572
  • 15
  • 110
  • 169