Class: Permit::Base
- Inherits:
-
Object
- Object
- Permit::Base
- Includes:
- Util
- Defined in:
- lib/cancan-permits/permit/base_permit.rb
Instance Attribute Summary collapse
-
#ability ⇒ Object
readonly
Returns the value of attribute ability.
-
#strategy ⇒ Object
readonly
this can be used to customize the strategy used by owns to determine ownership, fx to support alternative ORMs.
Instance Method Summary collapse
- #can(action, subject, conditions = nil, &block) ⇒ Object
- #cannot(action, subject, conditions = nil, &block) ⇒ Object
-
#initialize(ability, options = {}) ⇒ Base
constructor
A new instance of Base.
- #licenses(*names) ⇒ Object
- #owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id, strategy_used = nil) ⇒ Object
- #permit?(user, options = {}) ⇒ Boolean
Methods included from Util
Constructor Details
Instance Attribute Details
#ability ⇒ Object (readonly)
Returns the value of attribute ability.
6 7 8 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 6 def ability @ability end |
#strategy ⇒ Object (readonly)
this can be used to customize the strategy used by owns to determine ownership, fx to support alternative ORMs
7 8 9 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 7 def strategy @strategy end |
Instance Method Details
#can(action, subject, conditions = nil, &block) ⇒ Object
35 36 37 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 35 def can(action, subject, conditions = nil, &block) can_definitions << CanCan::CanDefinition.new(true, action, subject, conditions, block) end |
#cannot(action, subject, conditions = nil, &block) ⇒ Object
39 40 41 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 39 def cannot(action, subject, conditions = nil, &block) can_definitions << CanCan::CanDefinition.new(false, action, subject, conditions, block) end |
#licenses(*names) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 9 def licenses *names names.to_strings.each do |name| begin module_name = "#{name.camelize}License" clazz = module_name.constantize rescue raise "License #{module_name} is not defined" end begin clazz.new(self).enforce! rescue raise "License #{clazz} could not be enforced using #{self.inspect}" end end end |
#owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id, strategy_used = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 43 def owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id, strategy_used = nil) begin strategy_used = strategy_used || self.strategy user_id = user.send :"#{user_id_attribute}" rescue raise ArgumentError, "ERROR (owns) - The user of class #{user.class} does not respond to ##{user_id_attribute}" end # puts "can #{clazz} manage ownership: #{ownership_relation.inspect} => #{user_id.inspect} ???" # puts "Using strategy: #{strategy_used}" begin case strategy_used when :string can :manage, clazz, ownership_relation => user_id.to_s when :default can :manage, clazz, ownership_relation => user_id else raise "Trying to use unknown ownership strategy: #{strategy}" end rescue Exception => e puts e.inspect end end |
#permit?(user, options = {}) ⇒ Boolean
31 32 33 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 31 def permit?(user, = {}) false end |