Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 56 lines (49 sloc) 1.657 kb
0cfb8c7 @ryanb adding basic ability module
authored
1 module CanCan
2 module Ability
c663eff @ryanb using instance_exec to change scope of can blocks to instance of ability...
authored
3 attr_accessor :user
4
1034c81 @ryanb adding a before filter for loading and authorizing a resource
authored
5 def can?(original_action, target) # TODO this could use some refactoring
4b6f538 @ryanb moving can definition into ability instance instead of class, this remov...
authored
6 (@can_history || []).reverse.each do |can_action, can_target, can_block|
0b8b51b @ryanb adding aliasing of actions
authored
7 possible_actions_for(original_action).each do |action|
8 if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target))
9 if can_block.nil?
10 return true
11 else
12 block_args = []
13 block_args << action if can_action == :manage
14 block_args << (target.class == Class ? target : target.class) if can_target == :all
15 block_args << (target.class == Class ? nil : target)
4b6f538 @ryanb moving can definition into ability instance instead of class, this remov...
authored
16 return can_block.call(*block_args)
0b8b51b @ryanb adding aliasing of actions
authored
17 end
0cfb8c7 @ryanb adding basic ability module
authored
18 end
19 end
20 end
21 false
22 end
23
0b8b51b @ryanb adding aliasing of actions
authored
24 def possible_actions_for(initial_action)
25 actions = [initial_action]
4b6f538 @ryanb moving can definition into ability instance instead of class, this remov...
authored
26 (@aliased_actions || default_alias_actions).each do |target, aliases|
0b8b51b @ryanb adding aliasing of actions
authored
27 actions += possible_actions_for(target) if aliases.include? initial_action
28 end
29 actions
30 end
c663eff @ryanb using instance_exec to change scope of can blocks to instance of ability...
authored
31
4b6f538 @ryanb moving can definition into ability instance instead of class, this remov...
authored
32 def can(action, target, &block)
33 @can_history ||= []
34 @can_history << [action, target, block]
35 end
36
37 def alias_action(*args)
38 @aliased_actions ||= default_alias_actions
39 target = args.pop[:to]
40 @aliased_actions[target] = args
41 end
42
43 def default_alias_actions
44 {
45 :read => [:index, :show],
46 :create => [:new],
47 :update => [:edit],
48 }
49 end
50
51 def prepare(user)
52 # to be overriden by included class
0cfb8c7 @ryanb adding basic ability module
authored
53 end
54 end
55 end
Something went wrong with that request. Please try again.