Class: RuboCop::Cop::Rails::RedirectBackOrTo

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRailsVersion
Defined in:
lib/rubocop/cop/rails/redirect_back_or_to.rb

Overview

Checks for uses of ‘redirect_back(fallback_location: …)` and suggests using `redirect_back_or_to(…)` instead.

‘redirect_back(fallback_location: …)` was soft deprecated in Rails 7.0 and `redirect_back_or_to` was introduced as a replacement.

Examples:

# bad
redirect_back(fallback_location: root_path)

# good
redirect_back_or_to(root_path)

# bad
redirect_back(fallback_location: root_path, allow_other_host: false)

# good
redirect_back_or_to(root_path, allow_other_host: false)

Constant Summary collapse

MSG =
'Use `redirect_back_or_to` instead of `redirect_back` with `:fallback_location` keyword argument.'
RESTRICT_ON_SEND =
%i[redirect_back].freeze

Constants included from TargetRailsVersion

TargetRailsVersion::TARGET_GEM_NAME, TargetRailsVersion::USES_REQUIRES_GEM_API

Instance Method Summary collapse

Methods included from TargetRailsVersion

minimum_target_rails_version, support_target_rails_version?

Instance Method Details

#on_send(node) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/rubocop/cop/rails/redirect_back_or_to.rb', line 40

def on_send(node)
  redirect_back_with_fallback_location(node) do |fallback_pair, fallback_value, options|
    add_offense(node.loc.selector) do |corrector|
      correct_redirect_back(corrector, node, fallback_pair, fallback_value, options)
    end
  end
end