Views of controllers that don't have a resource and passing options
At the wiki, we can see "Using authority on controllers that don't have a resource". But how about the view?
Simple example: I whould like to have a link, on a view, only if the user has rights to it.
Models:
User,Course,Role (from rolify)
Controllers: Course and Teacher
I have a controller where only the course's owner can manage the course's teachers.
I don't have a Teacher Model. A teacher is a teacher on a Course if it has a role teacher on that course.
Since I don't have a Model, I'm using the example at "Using authority on controllers that don't have a resource".
It that example, how can I create a link, on the view, only if the user has rights to acess it?
class TeachersController < ApplicationController
before_filter :authenticate_user!
before_filter :load_course
authorize_actions_for TeacherAuthorizer, :for => @Course
So far so good, but how can I use it on the view?
= link_to 'Manage Teachers', course_teachers_path(@Course)
if current_user.can_create?(WHAT_HERE?)
I could use the User model, since the Teacher is a User, but it would require to set User.authorizer_name = TeacherAuthorizer.
But that doesn't fell right.
An other approach is to try to call the TeacherAuthorizer, in the view:
= link_to 'Manage Teachers', course_teachers_path(@Course)
if TeacherAuthorizer.creatable_by?(current_user, :for=>@Course)
But I would get a "wrong number of arguments (2 for 1)".
I think, if we could, somehow say at TeacherAuthorizer:
class TeacherAuthorizer < Authority::Authorizer
def self.auth_symbol = :teacher
Or gets from the Authorizer name of the class
And then, if we could:
= link_to 'Manage Teachers', course_teachers_path(@Course)
if current_user.can_create?(:teacher, :for=>@Course)