class RuboCop::Cop::Rails::ActionFilter

This cop enforces the consistent use of action filter methods.

The cop is configurable and can enforce the use of the older something_filter methods or the newer something_action methods.

@example EnforcedStyle: action (default)

# bad
after_filter :do_stuff
append_around_filter :do_stuff
skip_after_filter :do_stuff

# good
after_action :do_stuff
append_around_action :do_stuff
skip_after_action :do_stuff

@example EnforcedStyle: filter

# bad
after_action :do_stuff
append_around_action :do_stuff
skip_after_action :do_stuff

# good
after_filter :do_stuff
append_around_filter :do_stuff
skip_after_filter :do_stuff

Constants

ACTION_METHODS
FILTER_METHODS
MSG
RESTRICT_ON_SEND

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 72
def on_block(node)
  check_method_node(node.send_node)
end
on_send(node) click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 76
def on_send(node)
  check_method_node(node) unless node.receiver
end

Private Instance Methods

bad_methods() click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 93
def bad_methods
  style == :action ? FILTER_METHODS : ACTION_METHODS
end
check_method_node(node) click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 82
def check_method_node(node)
  method_name = node.method_name
  return unless bad_methods.include?(method_name)

  message = format(MSG, prefer: preferred_method(method_name), current: method_name)

  add_offense(node.loc.selector, message: message) do |corrector|
    corrector.replace(node.loc.selector, preferred_method(node.loc.selector.source))
  end
end
good_methods() click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 97
def good_methods
  style == :action ? ACTION_METHODS : FILTER_METHODS
end
preferred_method(method) click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 101
def preferred_method(method)
  good_methods[bad_methods.index(method.to_sym)]
end