Skip to content

Commit ce4db7e

Browse files
committed
Add an option 'use_redirect_back_or_to_by_rails' to avoid definition conflicts
Fix: #296 Rails 7 released a new method called `redirect_back_or_to` as a replacement for `redirect_back`. That may conflicts with the method by the same name defined by Sorcery. This commit adds an option to set whether to use `redirect_back_or_to` defined by Rails 7, and the method `redirect_to_before_login_path` as an alternative to sorcery's `redirect_back_or_to. ref: rails/rails#40671
1 parent 939dd21 commit ce4db7e

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

lib/generators/sorcery/templates/initializer.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
#
2222
# config.save_return_to_url =
2323

24+
# Set whether to use 'redirect_back_or_to' defined in Rails 7.
25+
# Rails 7 released a new method called 'redirect_back_or_to' as a replacement for 'redirect_back'.
26+
# That may conflict with the method by the same name defined by Sorcery.
27+
# If you set this option to true, Sorcery's 'redirect_back_or_to' calls 'super' to use
28+
# the method of the same name defined in Rails 7.
29+
# Default: `false`
30+
#
31+
# config.use_redirect_back_or_to_by_rails =
32+
2433
# Set domain option for cookies; Useful for remember_me submodule.
2534
# Default: `nil`
2635
#

lib/sorcery/controller.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,16 @@ def current_user=(user)
9696

9797
# used when a user tries to access a page while logged out, is asked to login,
9898
# and we want to return him back to the page he originally wanted.
99-
def redirect_back_or_to(url, flash_hash = {})
99+
def redirect_back_or_to(...)
100+
if Config.use_redirect_back_or_to_by_rails
101+
super
102+
else
103+
warn('[WARNING] `redirect_back_or_to` overrides the method of the same name defined in Rails 7. If you want to avoid overriding, you can set `config.use_redirect_back_or_to_by_rails = true` and use `redirect_to_before_login_path`.')
104+
redirect_to_before_login_path(...)
105+
end
106+
end
107+
108+
def redirect_to_before_login_path(url, flash_hash = {})
100109
redirect_to(session[:return_to_url] || url, flash: flash_hash)
101110
session[:return_to_url] = nil
102111
end

lib/sorcery/controller/config.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class << self
2020
attr_accessor :after_logout
2121
attr_accessor :after_remember_me
2222

23+
# set whether to use 'redirect_back_or_to' defined in Rails 7.
24+
attr_accessor :use_redirect_back_or_to_by_rails
25+
2326
def init!
2427
@defaults = {
2528
:@user_class => nil,
@@ -32,7 +35,8 @@ def init!
3235
:@after_logout => Set.new,
3336
:@after_remember_me => Set.new,
3437
:@save_return_to_url => true,
35-
:@cookie_domain => nil
38+
:@cookie_domain => nil,
39+
:@use_redirect_back_or_to_by_rails => false
3640
}
3741
end
3842

spec/controllers/controller_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222

2323
expect(Sorcery::Controller::Config.not_authenticated_action).to eq :my_action
2424
end
25+
26+
it "enables configuration option 'use_redirect_back_or_to_by_rails'" do
27+
sorcery_controller_property_set(:use_redirect_back_or_to_by_rails, true)
28+
29+
expect(Sorcery::Controller::Config.use_redirect_back_or_to_by_rails).to be true
30+
end
2531
end
2632

2733
# ----------------- PLUGIN ACTIVATED -----------------------
@@ -186,5 +192,34 @@
186192

187193
expect(assigns[:result]).to eq user
188194
end
195+
196+
describe 'redirect_back_or_to' do
197+
describe 'use_redirect_back_or_to_by_rails' do
198+
context 'when true' do
199+
before do
200+
sorcery_controller_property_set(:use_redirect_back_or_to_by_rails, true)
201+
allow_any_instance_of(ActionController::TestRequest).to receive(:referer).and_return('http://test.host/referer_action')
202+
end
203+
204+
it 'uses Rails 7 redirect_back_or_to method' do
205+
get :test_return_to
206+
207+
expect(response).to redirect_to('http://test.host/referer_action')
208+
end
209+
end
210+
211+
context 'when false' do
212+
before { sorcery_controller_property_set(:use_redirect_back_or_to_by_rails, false) }
213+
214+
it 'uses Sorcery redirect_back_or_to method' do
215+
session[:return_to_url] = 'http://test.host/some_action'
216+
get :test_return_to
217+
218+
expect(response).to redirect_to('http://test.host/some_action')
219+
expect(flash[:notice]).to eq 'haha!'
220+
end
221+
end
222+
end
223+
end
189224
end
190225
end

0 commit comments

Comments
 (0)