Module: ActionDebug::Controller::ClassMethods

Defined in:
lib/actiondebug/controller.rb

Instance Method Summary collapse

Instance Method Details

#action_methods(include_ans = true) ⇒ Object

This is just like action_methods found in the AbstractController class, but we provide the option to not include inherited methods



81
82
83
84
# File 'lib/actiondebug/controller.rb', line 81

def action_methods(include_ans = true)
  methods = super()
  methods & public_instance_methods(false).map(&:to_s) unless include_ans
end

#actions_skipping_filter(filter) ⇒ Object

FIXME: what about filters with the same name in different controllers?



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/actiondebug/controller.rb', line 52

def actions_skipping_filter(filter)
  filters_for_self_and_descendants.reduce({}) do |h, tuple|
    # We want to handle the false positive of someone supplying a filter
    # that simply does not exist either at all, or for the current
    # controller being analyzed.
    if !safe_instantiate(tuple.first).defined_filters.include?(filter)
      puts "Filter #{filter} is not defined for #{tuple.first.to_s}. Skipping."
      h[tuple.first] = []
    else
      h[tuple.first] = tuple.last.keys.select do |action|
        !tuple.last[action].include?(filter.to_sym)
      end
    end
    h
  end.keep_if { |key, val| !val.empty? }
end

#actions_using_filter(filter) ⇒ Object

FIXME: what about filters with the same name in different controllers?



70
71
72
73
74
75
76
77
# File 'lib/actiondebug/controller.rb', line 70

def actions_using_filter(filter)
  filters_for_self_and_descendants.reduce({}) do |h, tuple|
    h[tuple.first] = tuple.last.keys.select do |action|
      tuple.last[action].include?(filter.to_sym)
    end
    h
  end.keep_if { |key, val| !val.empty? }
end

#after_filters(action = nil) ⇒ Object



28
29
30
# File 'lib/actiondebug/controller.rb', line 28

def after_filters(action = nil)
  filters kind: :after, action: action
end

#after_filters_for_self_and_descendantsObject



47
48
49
# File 'lib/actiondebug/controller.rb', line 47

def after_filters_for_self_and_descendants
  filters_for_self_and_descendants({kind: :after})
end

#around_filters(action = nil) ⇒ Object



24
25
26
# File 'lib/actiondebug/controller.rb', line 24

def around_filters(action = nil)
  filters kind: :around, action: action
end

#around_filters_for_self_and_descendants(p = {}) ⇒ Object



43
44
45
# File 'lib/actiondebug/controller.rb', line 43

def around_filters_for_self_and_descendants(p = {})
  filters_for_self_and_descendants({kind: :around})
end

#before_filters(action = nil) ⇒ Object



20
21
22
# File 'lib/actiondebug/controller.rb', line 20

def before_filters(action = nil)
  filters kind: :before, action: action
end

#before_filters_for_self_and_descendantsObject



39
40
41
# File 'lib/actiondebug/controller.rb', line 39

def before_filters_for_self_and_descendants
  filters_for_self_and_descendants({kind: :before})
end

#defined_filtersObject



86
87
88
# File 'lib/actiondebug/controller.rb', line 86

def defined_filters
  @defined_filters ||= filters_for_self.map(&:filter)
end

#filters(p = {}) ⇒ Object

With current knowledge of Rails internals, we are going to have to call this method several times when building up a map of the entire application.



9
10
11
12
13
14
15
16
17
18
# File 'lib/actiondebug/controller.rb', line 9

def filters(p = {})
  if p[:action].nil?
    action_methods(false).reduce({}) do |h, action|
      h[action.to_sym] = filters_for_action(action, p)
      h
    end
  else
    {p[:action].to_sym => filters_for_action(p[:action], p)}
  end
end

#filters_for_self_and_descendants(p = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/actiondebug/controller.rb', line 32

def filters_for_self_and_descendants(p = {})
  [self.name.to_sym, symbolized_descendants].flatten.reduce({}) do |h, d|
    h[d] = safe_instantiate(d).send(:filters, p)
    h
  end
end