Accessing the child instance in a RABL template

后端 未结 4 2128
不思量自难忘°
不思量自难忘° 2021-01-06 07:26

I have a RABL template as shown below

object @user
attributes :name
child :contacts do
  # does not work
  if contact.is_foo?
    attributes :a1, :a2
  else
         


        
4条回答
  •  情深已故
    2021-01-06 07:45

    Here's one way:

    child :contacts do
      node(:a1, :if => lambda { |c| c.is_foo? }
      node(:a2, :if => lambda { |c| c.is_foo? }
    
      node(:a3, :unless => lambda { |c| c.is_foo? }
      node(:a4, :unless => lambda { |c| c.is_foo? }
    end
    

    Not exactly the same but one possibility, another is:

    node :contacts do |u|
      u.contacts.map do |c|
        if contact.is_foo?
          partial("contacta", :object => c)
          # or { :a1 => "foo", :a2 => "bar" }
        else
          partial("contactb", :object => c)
          # or { :a3 => "foo", :a4 => "bar" }
        end
      end
    end
    

提交回复
热议问题