Created
June 16, 2011 03:38
-
-
Save ramontayag/1028632 to your computer and use it in GitHub Desktop.
How I integrated cancan into a widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MenuEditor::Page::FormWidget < Apotomo::Widget | |
# Step 1: make sure the current_user variable (or something like it) is available for use | |
include Devise::Controllers::Helpers | |
helper_method :current_user | |
responds_to_event :submit | |
def display | |
... | |
end | |
def submit(event) | |
# Step 3: call `current_ability.authorize!` like you would `authorize!` in a controller | |
current_ability.authorize! :manage, @current_site | |
@nav_item = @current_site.nav_items.new params[:nav_item] | |
... | |
end | |
private | |
def setup!(*) | |
@current_site ||= options[:current_site] | |
end | |
# Step 2: instantiate Ability with passing the current_user | |
def current_ability | |
::Ability.new current_user | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also add:
Then make that method a helper:
So that you can do this in your views: