Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the child instance in a RABL template

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
    attributes :a3, :a4
  end
end

How do I access the Contact object in the child block of the template? I need to perform some conditional logic on the child instance.

like image 425
Harish Shetty Avatar asked Dec 10 '22 00:12

Harish Shetty


1 Answers

You can access the current object by declaring the block parameter.

object @user
attributes :name
child :contacts do |contact|
  if contact.is_foo?
    attributes :a1, :a2
  else
    attributes :a3, :a4
  end
end

Old answer

I ended up using the root_object method, which returns the data object in a given context.

object @user
attributes :name
child :contacts do
  if root_object.is_foo?
    attributes :a1, :a2
  else
    attributes :a3, :a4
  end
end
like image 147
Harish Shetty Avatar answered Dec 23 '22 17:12

Harish Shetty