Skip to content

Commit 89a21e8

Browse files
committed
[Fix #1595] Fix a false negative for Rails/I18nLocaleTexts when using redirect_back_or_to
1 parent 2a59f84 commit 89a21e8

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1595](https://github.com/rubocop/rubocop-rails/issues/1595): Fix a false negative for `Rails/I18nLocaleTexts` when using `redirect_back_or_to` with a flash message. ([@55728][])

lib/rubocop/cop/rails/i18n_locale_texts.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ module Rails
4747
# end
4848
#
4949
# # bad
50+
# class PostsController < ApplicationController
51+
# def update
52+
# # ...
53+
# redirect_back_or_to root_path, alert: "Failed to update!"
54+
# end
55+
# end
56+
#
57+
# # good
58+
# # config/locales/en.yml
59+
# # en:
60+
# # posts:
61+
# # update:
62+
# # failure: "Failed to update!"
63+
#
64+
# class PostsController < ApplicationController
65+
# def update
66+
# # ...
67+
# redirect_back_or_to root_path, alert: t(".failure")
68+
# end
69+
# end
70+
#
71+
# # bad
5072
# class UserMailer < ApplicationMailer
5173
# def welcome(user)
5274
# mail(to: user.email, subject: "Welcome to My Awesome Site")
@@ -69,7 +91,7 @@ module Rails
6991
class I18nLocaleTexts < Base
7092
MSG = 'Move locale texts to the locale files in the `config/locales` directory.'
7193

72-
RESTRICT_ON_SEND = %i[validates redirect_to redirect_back []= mail].freeze
94+
RESTRICT_ON_SEND = %i[validates redirect_to redirect_back redirect_back_or_to []= mail].freeze
7395

7496
def_node_search :validation_message, <<~PATTERN
7597
(pair (sym :message) $str)
@@ -98,7 +120,7 @@ def on_send(node)
98120
add_offense(text_node)
99121
end
100122
return
101-
when :redirect_to, :redirect_back
123+
when :redirect_to, :redirect_back, :redirect_back_or_to
102124
text_node = redirect_to_flash(node).to_a.last
103125
when :[]=
104126
text_node = flash_assignment?(node)

spec/rubocop/cop/rails/i18n_locale_texts_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@
3333
RUBY
3434
end
3535

36+
it 'registers an offense when using `redirect_back_or_to` with a notice text message' do
37+
expect_offense(<<~RUBY)
38+
redirect_back_or_to root_path, notice: "Post created!"
39+
^^^^^^^^^^^^^^^ Move locale texts to the locale files in the `config/locales` directory.
40+
RUBY
41+
end
42+
43+
it 'registers an offense when using `redirect_back_or_to` with an alert text message' do
44+
expect_offense(<<~RUBY)
45+
redirect_back_or_to root_path, alert: "Failed to update!"
46+
^^^^^^^^^^^^^^^^^^^ Move locale texts to the locale files in the `config/locales` directory.
47+
RUBY
48+
end
49+
50+
it 'does not register an offense when using `redirect_back_or_to` with localized flash messages' do
51+
expect_no_offenses(<<~RUBY)
52+
redirect_back_or_to root_path, notice: t(".success")
53+
RUBY
54+
end
55+
3656
it 'does not register an offense when using `redirect_to` with localized flash messages' do
3757
expect_no_offenses(<<~RUBY)
3858
redirect_to root_path, notice: t(".success")

0 commit comments

Comments
 (0)