Class: Ability
- Inherits:
-
Object
- Object
- Ability
- Includes:
- CanCan::Ability
- Defined in:
- lib/role_up/ability.rb
Constant Summary collapse
- @@ability_definitions =
{}
Instance Attribute Summary collapse
-
#authorization_instance ⇒ Object
readonly
Attributes.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_standard_rules_to_resources_hash_if_defined(resources) ⇒ Object
Instance Methods.
- #constantize_authorization_class ⇒ Object
- #get_new_authorization_instance ⇒ Object
-
#initialize(current_authorization_instance = nil, *resources) ⇒ Ability
constructor
A new instance of Ability.
- #load_all_rules ⇒ Object
- #load_resource_rules(resource) ⇒ Object
- #load_resource_rules!(resource) ⇒ Object
- #load_rules_for(*resources) ⇒ Object
- #load_rules_for!(*resources) ⇒ Object
- #rules_defined_for?(resource) ⇒ Boolean
- #standard_rules_defined? ⇒ Boolean
Constructor Details
#initialize(current_authorization_instance = nil, *resources) ⇒ Ability
Returns a new instance of Ability.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/role_up/ability.rb', line 18 def initialize( = nil , *resources ) # Load our authorization class if @authorization_instance = else @authorization_instance = end # Load rules if resources.empty? load_all_rules else load_rules_for! *resources end end |
Instance Attribute Details
#authorization_instance ⇒ Object (readonly)
Attributes
9 10 11 |
# File 'lib/role_up/ability.rb', line 9 def @authorization_instance end |
Class Method Details
.define_rules(&rule_set) ⇒ Object
Class Methods
38 39 40 41 42 |
# File 'lib/role_up/ability.rb', line 38 def self.define_rules( &rule_set ) raise RoleUp::Errors::IncorrectAbilityClassDefinition unless self.to_s =~ /(\w+)Ability$/ resource = $1.underscore.to_sym ability_definitions[ resource ] = rule_set end |
.standard_rules(&rule_set) ⇒ Object
44 45 46 |
# File 'lib/role_up/ability.rb', line 44 def self.standard_rules( &rule_set ) ability_definitions[ :__standard ] = rule_set end |
Instance Method Details
#add_standard_rules_to_resources_hash_if_defined(resources) ⇒ Object
Instance Methods
50 51 52 53 54 |
# File 'lib/role_up/ability.rb', line 50 def add_standard_rules_to_resources_hash_if_defined( resources ) resources.delete :__standard # just get rid of it - we'll add it back if it's really there resources << :__standard if standard_rules_defined? resources end |
#constantize_authorization_class ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/role_up/ability.rb', line 56 def begin if RoleUp..is_a? Symbol return RoleUp..to_s.camelize.constantize elsif RoleUp..is_a? Class return RoleUp. elsif RoleUp..is_a? String return RoleUp..camelize.constantize end rescue raise RoleUp::Errors::BadAuthorizationClass.new "Provided authorization class is malformed :: #{RoleUp.}" end end |
#get_new_authorization_instance ⇒ Object
70 71 72 |
# File 'lib/role_up/ability.rb', line 70 def .new end |
#load_all_rules ⇒ Object
74 75 76 |
# File 'lib/role_up/ability.rb', line 74 def load_all_rules load_rules_for *self.class.ability_definitions.keys end |
#load_resource_rules(resource) ⇒ Object
78 79 80 81 |
# File 'lib/role_up/ability.rb', line 78 def load_resource_rules( resource ) rules_block = self.class.ability_definitions[ resource ] self.instance_exec( , &rules_block ) end |
#load_resource_rules!(resource) ⇒ Object
83 84 85 86 |
# File 'lib/role_up/ability.rb', line 83 def load_resource_rules!( resource ) raise RoleUp::Errors::RulesNotDefinedError.new( "Rules not defined for #{resource}" ) unless rules_defined_for? resource load_resource_rules resource end |
#load_rules_for(*resources) ⇒ Object
88 89 90 91 |
# File 'lib/role_up/ability.rb', line 88 def load_rules_for( *resources ) add_standard_rules_to_resources_hash_if_defined resources resources.each { |resource| load_resource_rules resource } end |
#load_rules_for!(*resources) ⇒ Object
93 94 95 96 |
# File 'lib/role_up/ability.rb', line 93 def load_rules_for!( *resources ) add_standard_rules_to_resources_hash_if_defined resources resources.each { |resource| load_resource_rules! resource } end |
#rules_defined_for?(resource) ⇒ Boolean
98 99 100 |
# File 'lib/role_up/ability.rb', line 98 def rules_defined_for?( resource ) self.class.ability_definitions.include? resource end |
#standard_rules_defined? ⇒ Boolean
102 103 104 |
# File 'lib/role_up/ability.rb', line 102 def standard_rules_defined? rules_defined_for? :__standard end |