Class: Pbw::Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
app/models/pbw/ability.rb

Constant Summary collapse

MANAGED_CLASSES =
[Area, Capability, Command, Constraint, Item, ItemContainer, ItemConversion, Process, Rule, Token, Trigger, User]

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Ability

Returns a new instance of Ability.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/pbw/ability.rb', line 7

def initialize(user)
    user ||= User.new
    if user.superadmin?
        can :manage, :all
    else
      can do |action, subject_class, subject|
        case action.to_s
        when "index", "show", "search"
          check_method = :viewable_by?
        when "create", "new"
          check_method = :creatable_by?
        when "edit", "update"
          check_method = :editable_by?
        when "delete", "destroy"
          check_method = :deletable_by?
        else 
          check_method = :viewable_by?
        end
        check_class = subject_class.respond_to?(check_method) ? subject_class : parent_of_subject_class(subject_class)
        check_class.send(check_method, user, subject)
      end
    end
end

Instance Method Details

#parent_of_subject_class(subject_class) ⇒ Object



31
32
33
34
35
# File 'app/models/pbw/ability.rb', line 31

def parent_of_subject_class(subject_class)
    MANAGED_CLASSES.each do |klass|
      return klass if subject_class.ancestors.include?(klass)
    end
end