Module: CanCanDry::AbilityMapping

Defined in:
lib/can_can_dry/ability_mapping.rb,
lib/can_can_dry/ability_mapping/path_mapping_not_found.rb,
lib/can_can_dry/ability_mapping/action_mapping_not_found.rb

Defined Under Namespace

Classes: ActionMappingNotFound, FindCanArgsList, PathMappingNotFound

Constant Summary collapse

RESOURCES_ACTION_MAPPING =
{
  read: %w[index show],
  create: %w[new create],
  update: %w[update edit],
  destroy: %w[destroy]
}.freeze
ALL_ACTION =
'ALL'

Instance Method Summary collapse

Instance Method Details

#can_args_by_path(root_path, path, method) ⇒ Object



51
52
53
54
55
# File 'lib/can_can_dry/ability_mapping.rb', line 51

def can_args_by_path(root_path, path, method)
  can_args_by_path_hash(recognize_path(root_path, path, method))
rescue ActionMappingNotFound => e
  raise PathMappingNotFound.new(path, method, e)
end

#can_args_by_path_hash(path_hash) ⇒ Object



57
58
59
60
# File 'lib/can_can_dry/ability_mapping.rb', line 57

def can_args_by_path_hash(path_hash)
  can_args_args = find_can_args_list(path_hash[:controller], path_hash[:action])
  replace_model_by_record(can_args_args, path_hash[:id])
end

#map_action(controller, action, *can_args) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/can_can_dry/ability_mapping.rb', line 41

def map_action(controller, action, *can_args)
  if can_args.count == 1
    raise "\"can_args\" deve ter 0 ou 2 ou mais elementos (can_args.count=#{can_args.count})"
  end

  mapping[controller] ||= {}
  mapping[controller][action] ||= []
  mapping[controller][action] << can_args
end

#map_controller(controller, *can_actions) ⇒ Object



19
20
21
# File 'lib/can_can_dry/ability_mapping.rb', line 19

def map_controller(controller, *can_actions)
  map_action(controller, ALL_ACTION, *can_actions)
end

#map_resources(resource, controller = nil) ⇒ Object

  1. Encontra o controller associado a resource.

  2. Mapeia:

  • map_action(controller, ‘index’, :read, resource)

  • map_action(controller, ‘show’, :read, resource)

  • map_action(controller, ‘new’, :create, resource)

  • map_action(controller, ‘create’, :create, resource)

  • map_action(controller, ‘update’, :update, resource)

  • map_action(controller, ‘edit’, :update, resource)

  • map_action(controller, ‘destroy’, :destroy, resource)



32
33
34
35
36
37
38
39
# File 'lib/can_can_dry/ability_mapping.rb', line 32

def map_resources(resource, controller = nil)
  controller ||= controller_by_resource(resource)
  RESOURCES_ACTION_MAPPING.each do |can_action, actions|
    actions.each do |action|
      map_action(controller, action, can_action, resource)
    end
  end
end

#mappingObject



15
16
17
# File 'lib/can_can_dry/ability_mapping.rb', line 15

def mapping
  @mapping ||= {}
end