Module: Cancannible::Grantee
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/cancannible/grantee.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#abilities(refresh = false) ⇒ Object
Returns the Ability set for the owner.
-
#can(ability, resource) ⇒ Object
Command: grant the permission to do
ability
onresource
. -
#can?(ability, resource) ⇒ Boolean
Returns true it the
ability
is permitted onresource
- persisted or dynamic (delegated to CanCan). -
#cannot(ability, resource) ⇒ Object
Command: prohibit the permission to do
ability
onresource
. -
#cannot?(ability, resource) ⇒ Boolean
Returns true it the
ability
is prohibited onresource
- persisted or dynamic (delegated to CanCan). -
#inherited_permissions ⇒ Object
Returns the collection of inherited permission records.
Instance Method Details
#abilities(refresh = false) ⇒ Object
Returns the Ability set for the owner. Set refresh
to true to force a reload of permissions.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cancannible/grantee.rb', line 49 def abilities(refresh = false) @abilities = if refresh nil elsif Cancannible.get_cached_abilities.respond_to?(:call) result = Cancannible.get_cached_abilities.call(self) if result # performs a crude compatibility check rules_size = result.send(:rules).size rescue nil rules_index_size = (result.instance_variable_get(:@rules_index) || []).size result if !rules_size.nil? && rules_index_size == rules_size end end return @abilities if @abilities @abilities ||= if ability_class = ('Ability'.constantize rescue nil) unless ability_class.included_modules.include?(Cancannible::PreloadAdapter) ability_class.send :include, Cancannible::PreloadAdapter end ability_class.new(self) end Cancannible.store_cached_abilities.call(self, @abilities) if Cancannible.store_cached_abilities.respond_to?(:call) @abilities end |
#can(ability, resource) ⇒ Object
Command: grant the permission to do ability
on resource
96 97 98 |
# File 'lib/cancannible/grantee.rb', line 96 def can(ability, resource) << [ability, resource] end |
#can?(ability, resource) ⇒ Boolean
Returns true it the ability
is permitted on resource
- persisted or dynamic (delegated to CanCan)
86 87 88 |
# File 'lib/cancannible/grantee.rb', line 86 def can?(ability, resource) abilities.can?(ability, resource) end |
#cannot(ability, resource) ⇒ Object
Command: prohibit the permission to do ability
on resource
101 102 103 |
# File 'lib/cancannible/grantee.rb', line 101 def cannot(ability, resource) << [ability, resource, false] end |
#cannot?(ability, resource) ⇒ Boolean
Returns true it the ability
is prohibited on resource
- persisted or dynamic (delegated to CanCan)
91 92 93 |
# File 'lib/cancannible/grantee.rb', line 91 def cannot?(ability, resource) abilities.cannot?(ability, resource) end |
#inherited_permissions ⇒ Object
Returns the collection of inherited permission records
75 76 77 78 79 80 81 82 83 |
# File 'lib/cancannible/grantee.rb', line 75 def inherited_perms = [] self.class..each do |relation| Array(self.send(relation)).each do |record| inherited_perms.concat(record..reload) end end inherited_perms end |