Class: RoleCore::CanCanCanPermission

Inherits:
Permission show all
Defined in:
lib/role_core/contrib/can_can_can_permission.rb

Instance Attribute Summary collapse

Attributes inherited from Permission

#callable, #name, #namespace, #priority

Instance Method Summary collapse

Methods inherited from Permission

#==, #hash

Constructor Details

#initialize(name, _namespace: [], _priority: 0, _callable: true, **options, &block) ⇒ CanCanCanPermission

Returns a new instance of CanCanCanPermission.



7
8
9
10
11
12
13
14
15
# File 'lib/role_core/contrib/can_can_can_permission.rb', line 7

def initialize(name, _namespace: [], _priority: 0, _callable: true, **options, &block)
  super
  return unless _callable

  @model_name = options[:model_name]
  @action = options[:action] || name
  @options = options.except(:model, :model_name, :action)
  @block = block
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



5
6
7
# File 'lib/role_core/contrib/can_can_can_permission.rb', line 5

def action
  @action
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/role_core/contrib/can_can_can_permission.rb', line 5

def options
  @options
end

Instance Method Details

#block_attached?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/role_core/contrib/can_can_can_permission.rb', line 28

def block_attached?
  !!@block
end

#call(context, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/role_core/contrib/can_can_can_permission.rb', line 17

def call(context, *args)
  return unless callable

  model = @model_name.constantize
  if block_attached?
    context.can @action, model, &@block.curry[*args]
  else
    context.can @action, model, @options
  end
end