Class: MongoidAbility::Ability
- Inherits:
-
Object
- Object
- MongoidAbility::Ability
- Includes:
- CanCan::Ability
- Defined in:
- lib/mongoid_ability/ability.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(owner) ⇒ Ability
constructor
=====================================================================.
-
#method_missing(name, *args) ⇒ Object
lambda for easy permission checking: .select(¤t_ability.can_read) .select(¤t_ability.can_update) .select(¤t_ability.can_destroy) etc.
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, | subject_class = subject_type.to_s.constantize outcome = nil ||= {} subject_class.self_and_ancestors_with_default_locks.each do |cls| outcome = ResolveInheritedLocks.call(owner, action, cls, subject, ) 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(¤t_ability.can_read) .select(¤t_ability.can_update) .select(¤t_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 |