moving can definition into ability instance instead of class, this removes ugly instance_exec command
This commit is contained in:
@@ -2,15 +2,8 @@ module CanCan
|
||||
module Ability
|
||||
attr_accessor :user
|
||||
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.alias_action :index, :show, :to => :read
|
||||
base.alias_action :new, :to => :create
|
||||
base.alias_action :edit, :to => :update
|
||||
end
|
||||
|
||||
def can?(original_action, target) # TODO this could use some refactoring
|
||||
(self.class.can_history || []).reverse.each do |can_action, can_target, can_block|
|
||||
(@can_history || []).reverse.each do |can_action, can_target, can_block|
|
||||
possible_actions_for(original_action).each do |action|
|
||||
if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target))
|
||||
if can_block.nil?
|
||||
@@ -20,7 +13,7 @@ module CanCan
|
||||
block_args << action if can_action == :manage
|
||||
block_args << (target.class == Class ? target : target.class) if can_target == :all
|
||||
block_args << (target.class == Class ? nil : target)
|
||||
return instance_exec(*block_args, &can_block)
|
||||
return can_block.call(*block_args)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -30,32 +23,33 @@ module CanCan
|
||||
|
||||
def possible_actions_for(initial_action)
|
||||
actions = [initial_action]
|
||||
(self.class.aliased_actions || []).each do |target, aliases|
|
||||
(@aliased_actions || default_alias_actions).each do |target, aliases|
|
||||
actions += possible_actions_for(target) if aliases.include? initial_action
|
||||
end
|
||||
actions
|
||||
end
|
||||
|
||||
def can(action, target, &block)
|
||||
@can_history ||= []
|
||||
@can_history << [action, target, block]
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
attr_reader :can_history
|
||||
attr_reader :aliased_actions
|
||||
|
||||
def can(action, target, &block)
|
||||
@can_history ||= []
|
||||
@can_history << [action, target, block]
|
||||
end
|
||||
|
||||
def alias_action(*args)
|
||||
@aliased_actions ||= {}
|
||||
target = args.pop[:to]
|
||||
@aliased_actions[target] = args
|
||||
end
|
||||
|
||||
def for_user(user)
|
||||
ability = new
|
||||
ability.user = user
|
||||
ability
|
||||
end
|
||||
def alias_action(*args)
|
||||
@aliased_actions ||= default_alias_actions
|
||||
target = args.pop[:to]
|
||||
@aliased_actions[target] = args
|
||||
end
|
||||
|
||||
def default_alias_actions
|
||||
{
|
||||
:read => [:index, :show],
|
||||
:create => [:new],
|
||||
:update => [:edit],
|
||||
}
|
||||
end
|
||||
|
||||
def prepare(user)
|
||||
# to be overriden by included class
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user