我正在尝试使用RABL API构建自定义集合。我有一个Idea模型,它有一个idea_actions数组。我需要使用RABL附加一个自定义的构思动作集合,但是我似乎无法使用child:idea_actions,因为我需要知道当前的动作。下面的代码错误...任何解决方案如何才能获得我想要的自定义集合?
object @idea
attributes *Idea.column_names
# need access to current action
node :idea_actions do
@idea.idea_actions.each do |action|
{ :id => action.id}
{ :custom_stuff => action.some_method } if action.something?
end
end
# can't do that...
# child :idea_actions
答案 0 :(得分:2)
我有一个类似的用例。这就是我必须做的才能让它发挥作用:
解决方案1
引入部分渲染子属性( _idea_action.rabl )
attributes :id
if root_object.something?
:custom_stuff => root_object.some_method
end
修改主视图以扩展新的部分
child(:idea_actions) {
extends("_idea_action")
}
解决方案2
node :idea_actions do
@idea.idea_actions.map do |action|
{ :id => action.id}.tap do |hash|
hash[:custom_stuff] = action.some_method if action.something?
end
end
end
解决方案3
child :idea_actions do
attributes :id
node(:custom_stuff, :if => lambda {|action| action.something?}) do |action|
action.some_method
end
end