class RuboCop::Cop::Rails::IgnoredSkipActionFilterOption
This cop checks that `if` and `only` (or `except`) are not used together as options of `skip_*` action filter.
The `if` option will be ignored when `if` and `only` are used together. Similarly, the `except` option will be ignored when `if` and `except` are used together.
@example
# bad class MyPageController < ApplicationController skip_before_action :login_required, only: :show, if: :trusted_origin? end # good class MyPageController < ApplicationController skip_before_action :login_required, if: -> { trusted_origin? && action_name == "show" } end
@example
# bad class MyPageController < ApplicationController skip_before_action :login_required, except: :admin, if: :trusted_origin? end # good class MyPageController < ApplicationController skip_before_action :login_required, if: -> { trusted_origin? && action_name != "admin" } end
Constants
- FILTERS
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb, line 62 def on_send(node) options = filter_options(node) return unless options return unless options.hash_type? options = options_hash(options) if if_and_only?(options) add_offense(options[:if], message: format(MSG, prefer: :only, ignore: :if)) elsif if_and_except?(options) add_offense(options[:except], message: format(MSG, prefer: :if, ignore: :except)) end end
Private Instance Methods
if_and_except?(options)
click to toggle source
# File lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb, line 90 def if_and_except?(options) options.key?(:if) && options.key?(:except) end
if_and_only?(options)
click to toggle source
# File lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb, line 86 def if_and_only?(options) options.key?(:if) && options.key?(:only) end
options_hash(options)
click to toggle source
# File lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb, line 80 def options_hash(options) options.pairs .select { |pair| pair.key.sym_type? } .map { |pair| [pair.key.value, pair] }.to_h end