I’m using RABL to render JSON.
I have these models:
ProjectTaskUser
The “show project” action should result in rendering of the details of the project including a list of the Tasks in the Project, but only those who the user is permitted to view.
So far, I’ve added all of the Tasks to the Project show.rabl, and it’s working fine:
child tasks: :tasks do
extends "task/show"
end
Is there a way to tell RABL to only render tasks that answer a given condition? — I already have the logic for that in the controller, but I can’t figure how to tell that to RABL…
–update–
I’ve figured a way to do that by adding a “code” section, passing in the project object and then building a hash from the visible tasks and rendering it. This works, but it is very ugly and defeats the purpose of using RABL in the first place.
It looks roughly like that:
code :tasks do |project|
project.tasks.visible_by(current_user).inject([]){|arr, task| ...}
end
I’m sure my use case is not that unique. Is there a more straight-forward way to do that?
I would refactor the query into your model into a method called
specific_tasksand then call something like this from RABL: