I have just made the switch to Pundit from CanCan. I am unsure about a couple of things, and how Pundit is best used.
For example:
If you have a resource that can have multiple parent objects, for instance lets say a Goal belongs to a student and instructor. Therefor, a student can have many goals and an instructor can have many goals. In a controller index action you might do:
if params[:student_id].present?
@account = Student.find(params[:student_id])
@goals = @account.goals
elsif params[:instructor_id].present?
@account Instructor.find(params[:instructor_id])
@goals = @account.goals
end
params
are not usable inside policies, so the logic needs to be done here. I think. For what I can tell, if you skip the policy_scope
you will get an unauthorized error when viewing the index page for goals.
Would you:
@goals = policy_scope(@account.goals)
OR
@goals = policy_scope(Goal.scoped).where( account_id: @account.id)
What happens when you throw a bunch of includes in the mix?
@example = policy_scoped(@school.courses.includes(:account => :user, :teacher ))
Or when needed to order...is this correct?
policy_scope(Issue.scoped).order("created_at desc")
When using scopes: What is :scope
here? Is :scope
an instance of the model being evaluated? I've tried accessing its attributes via :scope
, but didn't work.
class Scope < Struct.new(:user, :scope)
user
andscope
insideresolve
, also make them part of the private interface. – Moua