Class: Permit::Base

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/cancan-permits/permit/base_permit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#permit_name

Constructor Details

#initialize(ability, options = {}) ⇒ Base

Returns a new instance of Base.



26
27
28
29
# File 'lib/cancan-permits/permit/base_permit.rb', line 26

def initialize ability, options = {}
  @ability  = ability
  @strategy = options[:strategy] || Permits::Ability.strategy || :default      
end

Instance Attribute Details

#abilityObject (readonly)

Returns the value of attribute ability.



6
7
8
# File 'lib/cancan-permits/permit/base_permit.rb', line 6

def ability
  @ability
end

#strategyObject (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

Returns:

  • (Boolean)


31
32
33
# File 'lib/cancan-permits/permit/base_permit.rb', line 31

def permit?(user, options = {}) 
  false
end