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
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 77 def autocorrect(node) lambda do |corrector| corrector.replace(node.loc.selector, preferred_method(node.loc.selector.source).to_s) end end
on_block(node)
click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 69 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 73 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 97 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 86 def check_method_node(node) return unless bad_methods.include?(node.method_name) add_offense(node, location: :selector) end
good_methods()
click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 101 def good_methods style == :action ? ACTION_METHODS : FILTER_METHODS end
message(node)
click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 92 def message(node) format(MSG, prefer: preferred_method(node.method_name), current: node.method_name) end
preferred_method(method)
click to toggle source
# File lib/rubocop/cop/rails/action_filter.rb, line 105 def preferred_method(method) good_methods[bad_methods.index(method.to_sym)] end