Module: Cancannible::Grantee

Extended by:
ActiveSupport::Concern
Defined in:
lib/cancannible/grantee.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

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
# 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)
    # performs a crude compatibility check: cancan rules won't have a @rules_index
    # (neither will an empty ability object, but we ignore this case)
    result unless result && !result.instance_variable_defined?(:@rules_index)
  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



93
94
95
# File 'lib/cancannible/grantee.rb', line 93

def can(ability, resource)
  permissions << [ability, resource]
end

#can?(ability, resource) ⇒ Boolean

Returns true it the ability is permitted on resource - persisted or dynamic (delegated to CanCan)

Returns:

  • (Boolean)


83
84
85
# File 'lib/cancannible/grantee.rb', line 83

def can?(ability, resource)
  abilities.can?(ability, resource)
end

#cannot(ability, resource) ⇒ Object

Command: prohibit the permission to do ability on resource



98
99
100
# File 'lib/cancannible/grantee.rb', line 98

def cannot(ability, resource)
  permissions << [ability, resource, false]
end

#cannot?(ability, resource) ⇒ Boolean

Returns true it the ability is prohibited on resource - persisted or dynamic (delegated to CanCan)

Returns:

  • (Boolean)


88
89
90
# File 'lib/cancannible/grantee.rb', line 88

def cannot?(ability, resource)
  abilities.cannot?(ability, resource)
end

#inherited_permissionsObject

Returns the collection of inherited permission records



72
73
74
75
76
77
78
79
80
# File 'lib/cancannible/grantee.rb', line 72

def inherited_permissions
  inherited_perms = []
  self.class.inheritable_permissions.each do |relation|
    Array(self.send(relation)).each do |record|
      inherited_perms.concat(record.permissions.reload)
    end
  end
  inherited_perms
end