Class: CanTango::Configuration::Permits

Inherits:
PermitRegistry show all
Includes:
Singleton
Defined in:
lib/cantango/configuration/permits.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PermitRegistry

#all, #registered_by, #registered_for, #show_all

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



58
59
60
# File 'lib/cantango/configuration/permits.rb', line 58

def method_missing method_name, *args
  accounts[method_name] ||= PermitRegistry.new
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



6
7
8
# File 'lib/cantango/configuration/permits.rb', line 6

def accounts
  @accounts
end

#enabled_typesObject Also known as: enabled



9
10
11
# File 'lib/cantango/configuration/permits.rb', line 9

def enabled_types
  @enabled_types || available_types
end

Instance Method Details

#account_hash(name) ⇒ Object



54
55
56
# File 'lib/cantango/configuration/permits.rb', line 54

def  name
  accounts[name]
end

#allowed(candidate, actions, subjects, *extra_args) ⇒ Object



69
70
71
72
73
74
# File 'lib/cantango/configuration/permits.rb', line 69

def allowed candidate, actions, subjects, *extra_args
  executed_for(candidate).inject([]) do |result, permit|
    result << permit.class if permit.can? actions, subjects, *extra_args
    result
  end
end

#available_typesObject



14
15
16
# File 'lib/cantango/configuration/permits.rb', line 14

def available_types
  [:user, :account, :role, :role_group, :special]
end

#clear_executed!Object



95
96
97
# File 'lib/cantango/configuration/permits.rb', line 95

def clear_executed!
  @executed = nil
end

#denied(candidate, actions, subjects, *extra_args) ⇒ Object



76
77
78
79
80
81
# File 'lib/cantango/configuration/permits.rb', line 76

def denied candidate, actions, subjects, *extra_args
  executed_for(candidate).inject([]) do |result, permit|
    result << permit.class if permit.cannot? actions, subjects, *extra_args
    result
  end
end

#disable_for(type, *names) ⇒ Object



27
28
29
30
# File 'lib/cantango/configuration/permits.rb', line 27

def disable_for type, *names
  @disabled ||= {}
  @disabled[type.to_sym] = names.flatten.select_labels.map{|n| n.to_s.underscore}
end

#disable_types(*types) ⇒ Object Also known as: disable



18
19
20
# File 'lib/cantango/configuration/permits.rb', line 18

def disable_types *types
  @enabled_types = available_types - types.flatten
end

#disabledObject



37
38
39
# File 'lib/cantango/configuration/permits.rb', line 37

def disabled
  @disabled ||= {}
end

#disabled_for(type) ⇒ Object



41
42
43
# File 'lib/cantango/configuration/permits.rb', line 41

def disabled_for type
  disabled[type]
end

#enable_all!Object



45
46
47
48
# File 'lib/cantango/configuration/permits.rb', line 45

def enable_all!
  @disabled = {}
  enable_all_types!
end

#enable_all_for(type) ⇒ Object



32
33
34
35
# File 'lib/cantango/configuration/permits.rb', line 32

def enable_all_for type
  @disabled ||= {}
  @disabled[type.to_sym] = nil
end

#enable_all_types!Object



23
24
25
# File 'lib/cantango/configuration/permits.rb', line 23

def enable_all_types!
  @enabled_types = available_types
end

#executedObject



91
92
93
# File 'lib/cantango/configuration/permits.rb', line 91

def executed
  @executed ||= {}
end

#executed_for(ability) ⇒ Object



87
88
89
# File 'lib/cantango/configuration/permits.rb', line 87

def executed_for ability
  executed[hash_key_for(ability)] ||= []
end

#register_permit_class(permit_name, permit_clazz, permit_type, account_name) ⇒ Object



62
63
64
65
66
67
# File 'lib/cantango/configuration/permits.rb', line 62

def register_permit_class(permit_name, permit_clazz, permit_type, )
  registry =  ? self.send(.to_sym) : self
  puts "Registering #{permit_type} permit: #{permit_name} of class #{permit_clazz}" if CanTango.debug?
  registry.send(permit_type)[permit_name] = permit_clazz
  puts registry.send(permit_type).inspect if CanTango.debug?
end

#was_executed(permit, ability) ⇒ Object



83
84
85
# File 'lib/cantango/configuration/permits.rb', line 83

def was_executed permit, ability
  executed_for(ability) << permit
end