rabl icon indicating copy to clipboard operation
rabl copied to clipboard

Syntax for child with alias and condition is unexpected

Open vimutter opened this issue 12 years ago • 5 comments

child foo: :bar, if: true is going to call data_object with {foo: :bar, if: true}, so then check on size == 1 will fail and :bar wont be taken into account.

https://github.com/nesquena/rabl/blob/master/lib/rabl/helpers.rb#L12

vimutter avatar May 16 '13 16:05 vimutter

The same happens for me. It is annoying because you can't aliase a child field and use if: at the same time. There is any workaround?

jfcalvo avatar Jun 06 '13 16:06 jfcalvo

This ticket needs renamed to be noticed, IMO.

The issue is that because child() can take a 'data' field (first param) that is a hash, data_object will not grab the proper data field (the key in the hash in this case) if there is a second parameter that is also a hash (like an if clause)

My workaround is to wrap the child node in a regular if/end

attributes :one, :two, :three

if root_object.has_four?
   child(root_object.four => :four) do
      attributes :fourohone, :fourohtwo
   end
end

onyxraven avatar Jun 12 '13 22:06 onyxraven

You could probably also do:

child({ :foo => :bar }, { :if => true } do
  # ...
end

Not ideal but that should work as well. Renamed ticket to reflect the issue.

nesquena avatar Jun 13 '13 08:06 nesquena

Feel free to rename, or suggest your name. What is misleading in current title?

vimutter avatar Jun 14 '13 12:06 vimutter

+1

hamtie avatar Aug 27 '13 06:08 hamtie