module Corral::ControllerAdditions

Public Class Methods

included(base) click to toggle source
# File lib/corral/controller_additions.rb, line 25
def self.included(base)
  base.extend ClassMethods
  base.helper_method :can?, :cannot?, :current_ability if base.respond_to? :helper_method
end

Public Instance Methods

authorize!(*args) click to toggle source

Raises a Corral::AccessDenied exception if the current_ability cannot perform the given action. This is usually called in a controller action or before filter to perform the authorization.

A :message option can be passed to specify a different message.

You can rescue from the exception in the controller to customize how unauthorized access is displayed to the user.

See the load_and_authorize_resource method to automatically add the authorize! behavior to the default RESTful actions.

# File lib/corral/controller_additions.rb, line 41
def authorize!(*args)
  @_authorized = true
  current_ability.authorize!(*args)
end
can?(*args) click to toggle source

Use in the controller or view to check the user's permission for a given action and object.

This simply calls “can?” on the current_ability. See Ability#can?.

# File lib/corral/controller_additions.rb, line 60
def can?(*args)
  current_ability.can?(*args)
end
cannot?(*args) click to toggle source

Convenience method which works the same as “can?” but returns the opposite value.

# File lib/corral/controller_additions.rb, line 66
def cannot?(*args)
  current_ability.cannot?(*args)
end
current_ability() click to toggle source

Creates and returns the current user's ability and caches it. If you want to override how the Ability is defined then this is the place. Just define the method in the controller to change behavior.

Notice it is important to memoize the ability object so it is not recreated every time.

# File lib/corral/controller_additions.rb, line 52
def current_ability
  @current_ability ||= ::Ability.new(current_user)
end