Class: Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
lib/can_play/ability.rb

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Ability

Returns a new instance of Ability.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/can_play/ability.rb', line 4

def initialize(user)
  clear_aliased_actions
  CanPlay::Config.tap do |i|
    user = user||i.user_class_name.constantize.new
    if i.super_roles.is_a?(Array)
      i.super_roles.each do |role_name|
        can(:manage, :all) if user.send(i.role_judge_method, role_name)
      end
    elsif i.super_roles.is_a?(Proc)
      can(:manage, :all) if i.super_roles.call(user)
    end
    i.role_class_name.constantize.all.each do |role|
      next unless user.send(i.role_judge_method, role)
      role.send(i.role_resources_relation_name).each do |role_resource|
        resource = CanPlay.find_by_name_and_code(role_resource.resource_name, CanPlay.override_code)
        next unless resource
        if resource[:type] == 'collection'
          if resource[:behavior]
            block = resource[:behavior]
            can(resource[:verb], resource[:object]) if block.call
          else
            can resource[:verb], resource[:object]
          end
        elsif resource[:type] == 'member'
          if resource[:behavior]
            block = resource[:behavior]
            can resource[:verb], resource[:object] do |object|
              block.call(object)
            end
          else
            can resource[:verb], resource[:object]
          end
        end
      end
    end
  end
end