Module: CanCan::Ability

Defined in:
lib/cancan/ability.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/cancan/ability.rb', line 3

def user
  @user
end

Instance Method Details

#alias_action(*args) ⇒ Object



43
44
45
46
47
# File 'lib/cancan/ability.rb', line 43

def alias_action(*args)
  @aliased_actions ||= default_alias_actions
  target = args.pop[:to]
  @aliased_actions[target] = args
end

#can(action, target, &block) ⇒ Object



38
39
40
41
# File 'lib/cancan/ability.rb', line 38

def can(action, target, &block)
  @can_history ||= []
  @can_history << [action, target, block]
end

#can?(original_action, target) ⇒ Boolean

TODO this could use some refactoring

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cancan/ability.rb', line 5

def can?(original_action, target) # TODO this could use some refactoring
  (@can_history || []).reverse.each do |can_action, can_target, can_block|
    can_actions = [can_action].flatten
    can_targets = [can_target].flatten
    possible_actions_for(original_action).each do |action|
      if (can_actions.include?(:manage) || can_actions.include?(action)) && (can_targets.include?(:all) || can_targets.include?(target) || can_targets.any? { |c| target.kind_of?(c) })
        if can_block.nil?
          return true
        else
          block_args = []
          block_args << action if can_actions.include?(:manage)
          block_args << (target.class == Class ? target : target.class) if can_targets.include?(:all)
          block_args << (target.class == Class ? nil : target)
          return can_block.call(*block_args)
        end
      end
    end
  end
  false
end

#cannot?(*args) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/cancan/ability.rb', line 26

def cannot?(*args)
  !can?(*args)
end

#default_alias_actionsObject



49
50
51
52
53
54
55
# File 'lib/cancan/ability.rb', line 49

def default_alias_actions
  {
    :read => [:index, :show],
    :create => [:new],
    :update => [:edit],
  }
end

#possible_actions_for(initial_action) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/cancan/ability.rb', line 30

def possible_actions_for(initial_action)
  actions = [initial_action]
  (@aliased_actions || default_alias_actions).each do |target, aliases|
    actions += possible_actions_for(target) if aliases.include? initial_action
  end
  actions
end