module ActionDebug::Controller::ClassMethods

Public Instance Methods

action_methods(include_ans = true) click to toggle source

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

Calls superclass method
# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source
# File lib/actiondebug/controller.rb, line 28
def after_filters(action = nil)
  filters kind: :after, action: action
end
after_filters_for_self_and_descendants() click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/actiondebug/controller.rb, line 20
def before_filters(action = nil)
  filters kind: :before, action: action
end
before_filters_for_self_and_descendants() click to toggle source
# File lib/actiondebug/controller.rb, line 39
def before_filters_for_self_and_descendants
  filters_for_self_and_descendants({kind: :before})
end
defined_filters() click to toggle source
# File lib/actiondebug/controller.rb, line 86
def defined_filters
  @defined_filters ||= filters_for_self.map(&:filter)
end
filters(p = {}) click to toggle source

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.

# 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 = {}) click to toggle source
# 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

Private Instance Methods

filter_for_kind?(filter, kind) click to toggle source
# File lib/actiondebug/controller.rb, line 100
def filter_for_kind?(filter, kind)
  return true if kind.nil?
  filter.kind == kind
end
filter_runs_for_action?(filter, action) click to toggle source
# File lib/actiondebug/controller.rb, line 105
def filter_runs_for_action?(filter, action)
  return true if action.nil? or filter.per_key.empty?
  # XXX The @per_key attribute used below builds up its conditionals using
  # action_name as the attribute to compare against. I don't like depending
  # on this, but I don't see any way around it. Just keep this in mind if
  # things ever start breaking.
  action_name = action.to_s
  conditions = ["true"]
  unless filter.per_key[:if].empty?
    conditions << Array.wrap("(#{filter.per_key[:if].first})")
  end
  unless filter.per_key[:unless].empty?
    conditions << Array.wrap("!(#{filter.per_key[:unless].first})")
  end
  # XXX I feel safe using eval because we are only accessing Rails internal
  # stuff, but try to come up with a better way to do this in the future if
  # possible.
  eval conditions.flatten.join(" && ")
end
filters_for_action(action, p) click to toggle source
# File lib/actiondebug/controller.rb, line 125
def filters_for_action(action, p)
  filters_for_self.select do |c|
    filter_for_kind?(c, p[:kind]) and filter_runs_for_action?(c, action)
  end.map(&:filter)
end
filters_for_self() click to toggle source
# File lib/actiondebug/controller.rb, line 92
def filters_for_self
  @filters_for_self ||= _process_action_callbacks
end
is_a_descendant?(klass) click to toggle source
# File lib/actiondebug/controller.rb, line 143
def is_a_descendant?(klass)
  symbolized_descendants.include?(klass)
end
safe_instantiate(klass) click to toggle source
# File lib/actiondebug/controller.rb, line 131
def safe_instantiate(klass)
  return self unless is_a_descendant?(klass)
  klass.to_s.constantize
end
symbolize_descendants() click to toggle source
# File lib/actiondebug/controller.rb, line 136
def symbolize_descendants
  Rails.application.eager_load! if Rails.env != "production"
  descendants.map do |d|
    d.name.to_sym
  end
end
symbolized_descendants() click to toggle source
# File lib/actiondebug/controller.rb, line 96
def symbolized_descendants
  @symbolized_descendants ||= symbolize_descendants
end