Class: Permits::Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability, Permit::Util
Defined in:
lib/cancan-permits/permits/ability.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Permit::Util

#permit_name

Constructor Details

#initialize(user, options = {}) ⇒ Ability

Returns a new instance of Ability.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cancan-permits/permits/ability.rb', line 39

def initialize user, options = {}
  # put ability logic here!
  user ||= Guest.create
  all_permits = Permits::Ability.permits(self, options)
  all_permits.each do |permit|
    # get role name of permit 
    permit_role = permit_name(permit.class)
    if permit_role == :system
      # always execute system permit
      result = permit.permit?(user, options)
      break if result == :break
    else
      # only execute the permit if the user has the role of the permit or is for any role
      if user.has_role?(permit_role) || permit_role == :any
        permit.permit?(user, options) 
      end
    end
  end
end

Class Attribute Details

.ormObject

Returns the value of attribute orm.



8
9
10
# File 'lib/cancan-permits/permits/ability.rb', line 8

def orm
  @orm
end

.strategyObject

Returns the value of attribute strategy.



8
9
10
# File 'lib/cancan-permits/permits/ability.rb', line 8

def strategy
  @strategy
end

Class Method Details

.permits(ability, options = {}) ⇒ Object

set up each Permit instance to share this same Ability so that the can and cannot operations work on the same permission collection!



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cancan-permits/permits/ability.rb', line 23

def self.permits ability, options = {}
  special_permits = []
  special_permits << [:system, :any].map{|role| make_permit(role, ability, options)}
  role_permits = Permits::Roles.available.inject([]) do |permits, role|
    permit = make_permit(role, ability, options)
    permits << permit if permit
  end
  
  # puts "Role permits: #{role_permits}"
  
  all_permits = (special_permits + role_permits).flatten.compact
  # 
  # puts "All permits: #{all_permits}"
  # all_permits      
end