Skip to content
jacklin edited this page Sep 27, 2014 · 1 revision

Background

Rails provides by default 7 controller actions:

index, create, show, update, destroy, new, edit

These are pretty common actions, but you'll find in your app that you'll often need to deviate from this list.

For example in Kennel Captain we are using canner's built in support for multiple branches. So our user model has an action for switching a users current branch. Oddly enoug that action is called switch_branch.

It looks a little like this:

  def switch_branch
    can? :switch_branch, :user

    // omitting actual code for this example 
  end

Our user_policy.rb can map out the authorization for the switch method just as easily as
it can any of the 7 common actions:

def can?
  case @method
  when :index, :update, :edit
    has_role?(:basic)
  when :switch_branch
    has_role?(:gen_manager)
  end
end

That's all you have to do to authorize custom actions using canner.
This works with the view as well. We don't want to show the screen control
for branch switching to those who are not permitted to switch branches.
That looks a little like this:

  - if canner_policy(:switch_branch, :user).can?
    div.form-block
      = f.label :active_branch
      = f.select :active_branch_id, @user.branches.collect{|b| [b.name, b.id]}, {}, class: 'form-control'

This will use user_policy.rb to determine if your apps current_user is authorized to
switch to a different store branch.

Clone this wiki locally