Class: StrongActions::Decision

Inherits:
Object
  • Object
show all
Defined in:
lib/strong_actions/decision.rb

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Decision

Returns a new instance of Decision.



4
5
6
# File 'lib/strong_actions/decision.rb', line 4

def initialize(target)
  @target = target
end

Instance Method Details

#call(role, controller_path, action_name = nil, params = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/strong_actions/decision.rb', line 8

def call(role, controller_path, action_name = nil, params = {})
  action_name ||= 'index'
  role_definition = StrongActions.config.role_definition(role)
  return true unless role_definition

  controller_names_for(controller_path).each do |controller_name|
    controller_value = role_definition[controller_name]
    next if controller_value.nil?

    if controller_value.is_a?(Hash)
      action_value = controller_value[action_name]
    else
      action_value = controller_value
    end
    next if action_value.nil?

    action_values = Array(action_value)
    action_values.each do |definition|
      next if definition === true
      return false if definition === false

      role_object = role_object_for(role)
      return false unless role_object.instance_eval(definition)
    end

    break
  end

  true
end

#controller_names_for(controller_path) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/strong_actions/decision.rb', line 47

def controller_names_for(controller_path)
  ret = []

  path_elements = controller_path.split('/')
  if path_elements.size == 1
    ret = path_elements
  else
    path_elements.each_with_index do |path_element, i|
      ret << ret.last.to_s + path_element + (i < path_elements.size - 1 ? '/' : '')
    end
    ret.reverse!
  end

  ret
end

#role_object_for(role) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/strong_actions/decision.rb', line 39

def role_object_for(role)
  begin
     return @target.instance_eval(role)
  rescue NameError
    raise "role #{role} is not defined in controller"
  end
end