Class: CanTango::Configuration::Permits

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Debug

#debug

Methods inherited from PermitRegistry

#all, #permits_for, #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

[View source]

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

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

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.


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

def accounts
  @accounts
end

#enabled_typesObject Also known as: enabled


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

def enabled_types
  @enabled_types || available_types
end

Instance Method Details

#account_hash(name) ⇒ Object

[View source]

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

def  name
  accounts[name]
end

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

[View source]

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

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_classesObject

[View source]

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

def available_classes
  available_permits.values.compact
end

#available_permitsObject

[View source]

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

def available_permits
  @available_permits ||= default_permits
end

#available_typesObject

[View source]

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

def available_types
  available_permits.keys
end

#clear_executed!Object

[View source]

113
114
115
# File 'lib/cantango/configuration/permits.rb', line 113

def clear_executed!
  @executed = nil
end

#default_permitsObject

[View source]

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

def default_permits
  { :user       => CanTango::Permits::UserPermit,
    :account    => CanTango::Permits::AccountPermit,
    :role       => CanTango::Permits::RolePermit,
    :role_group => CanTango::Permits::RoleGroupPermit,
    :special    => nil
  }
end

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

[View source]

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

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

[View source]

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

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

[View source]

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

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

#disabledObject

[View source]

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

def disabled
  @disabled ||= {}
end

#disabled_for(type) ⇒ Object

[View source]

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

def disabled_for type
  disabled[type]
end

#enable_all!Object

[View source]

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

def enable_all!
  @disabled = {}
  enable_all_types!
end

#enable_all_for(type) ⇒ Object

[View source]

50
51
52
53
# File 'lib/cantango/configuration/permits.rb', line 50

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

#enable_all_types!Object

[View source]

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

def enable_all_types!
  @enabled_types = available_types
end

#executedObject

[View source]

109
110
111
# File 'lib/cantango/configuration/permits.rb', line 109

def executed
  @executed ||= {}
end

#executed_for(ability) ⇒ Object

[View source]

105
106
107
# File 'lib/cantango/configuration/permits.rb', line 105

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

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

[View source]

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

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

#was_executed(permit, ability) ⇒ Object

[View source]

101
102
103
# File 'lib/cantango/configuration/permits.rb', line 101

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