Module: Boilerman::Actions

Defined in:
lib/boilerman/actions.rb

Constant Summary collapse

METADATA_KEYS =
[:_non_existant_route,
:_method_conditionals,
:_proc_conditionals,
:_weird_controller]

Class Method Summary collapse

Class Method Details

.get_action_hash(filters = {}) ⇒ Object



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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/boilerman/actions.rb', line 9

def self.get_action_hash(filters={})
  controller_filters = filters[:controller_filters] || []

  with_actions       = filters[:with_actions]       || []
  without_actions    = filters[:without_actions]    || []

  with_filters       = filters[:with_filters]       || []
  without_filters    = filters[:without_filters]    || []

  ignore_filters     = filters[:ignore_filters]     || []
  ignore_actions     = filters[:ignore_actions]     || []

  # Biggie Smalls... Biggie Smalls... Biggie Smalls....
  routes =  Rails.application.routes.routes.routes.select do |route|
    # Select routes that point to a specific controller and action. Also
    # ignore routes that just redirect
    route.defaults.key?(:controller) &&
      !route.defaults.empty? &&
      route.app.class != ActionDispatch::Routing::Redirect
  end

  # We only care about the defaults which will give us an array of
  # controller/action hashes. We're also going to rearrange things a bit so
  # that the controller is the key and the value is another hash that
  # represent each action and it's corresponding filters. This will look
  # like:
  #
  # {:controler_name1 => {"action1" => [filter1, filter2, ... , filterN]}}
  controller_action_hash = build_controller_action_list(routes)

  filter_list = build_filter_list(controller_action_hash)

  # controller_filters
  unless controller_filters.empty?
    filter_list.select! do |controller, _|
      METADATA_KEYS.include?(controller) || include_controller?(controller_filters, controller.to_s)
    end
  end

  # ignore_actions
  unless ignore_actions.empty?
    filter_list = filter_list.inject(Hash.new) do |new_results, (controller, actions)|
      new_results[controller] = actions.reject{|action, filter| ignore_actions.include?(action) }
      new_results
    end
  end

  # ignore_filters
  unless ignore_filters.empty?
    filter_list = filter_list.inject(Hash.new) do |new_results, (controller, actions)|
      # FIXME Is this idiomatic Ruby code? Feels a bit icky to me.
      # Mapping over a hash turns it into a 2D array so we need to
      # turn it back into a hash using the Hash[] syntax.
      new_results[controller] = Hash[actions.map do |action, filters|
        [action, filters.reject{|filter| ignore_filters.include?(filter.to_s)}]
      end]
      new_results
    end
  end

  # without_filters
  unless without_filters.empty?
    filter_list = filter_list.inject(Hash.new) do |new_results, (controller, actions)|
      new_results[controller] = actions.select{|action, filters| (without_filters & Array(filters)).empty? }
      new_results
    end
  end

  # with_filters
  unless with_filters.empty?
    filter_list = filter_list.inject(Hash.new) do |new_results, (controller, actions)|
      new_results[controller] = actions.select{|action, filters| (with_filters - Array(filters)).empty? }
      new_results
    end
  end

  filter_list

  #if !with_actions.empty? && !without_actions.empty?
    ## This means that both with_actions AND without_actions were specified
    #next route_hash if without_actions.include?(defaults[:action])
    #next route_hash if !with_actions.include?(defaults[:action])
  #elsif with_actions.empty?
    ## This means that just without_actions filtering was specified
    #next route_hash if without_actions.include?(defaults[:action])
  #elsif without_actions.empty?
    ## This means that just with_action filtering was specified
    #next route_hash if !with_actions.include?(defaults[:action])
  #end
end