Class: MongoidAbility::Ability

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ Ability

=====================================================================



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongoid_ability/ability.rb', line 19

def initialize owner
  @owner = owner

  can do |action, subject_type, subject, options|
    subject_class = subject_type.to_s.constantize
    outcome = nil
    options ||= {}

    subject_class.self_and_ancestors_with_default_locks.each do |cls|
      outcome = ResolveInheritedLocks.call(owner, action, cls, subject, options)
      break if outcome != nil
    end

    outcome
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

lambda for easy permission checking: .select(&current_ability.can_read) .select(&current_ability.can_update) .select(&current_ability.can_destroy) etc.



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

def method_missing name, *args
  return super unless name.to_s =~ /\A(can|cannot)_/
  return unless action = name.to_s.gsub(/\A(can|cannot)_/, '').to_sym
  name =~ /can_/ ? lambda { |doc| can? action, doc } : lambda { |doc| cannot? action, doc }
end

Instance Attribute Details

#owner ⇒ Object (readonly)

Returns the value of attribute owner.



7
8
9
# File 'lib/mongoid_ability/ability.rb', line 7

def owner
  @owner
end

Class Method Details

.subject_classes ⇒ Object



9
10
11
# File 'lib/mongoid_ability/ability.rb', line 9

def self.subject_classes
  Object.descendants.select{ |cls| cls.included_modules.include?(MongoidAbility::Subject) }
end

.subject_root_classes ⇒ Object



13
14
15
# File 'lib/mongoid_ability/ability.rb', line 13

def self.subject_root_classes
  subject_classes.reject{ |cls| cls.superclass.included_modules.include?(MongoidAbility::Subject) }
end