Skip to content

Commit fad97b8

Browse files
committed
Extract redirection behavior from lambda to classes
We have to lambdas, so we need two classes.
1 parent 8cf0eec commit fad97b8

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module Auth
5+
# This service object is responsible for handling unauthorized redirects
6+
class UnauthorizedAdminAccessHandler
7+
# @param controller [ApplicationController] an instance of ApplicationController
8+
# or its subclasses.
9+
def initialize(controller)
10+
@controller = controller
11+
end
12+
13+
# This method is responsible for handling unauthorized redirects
14+
def call
15+
if spree_current_user
16+
flash[:error] = I18n.t('spree.authorization_failure')
17+
18+
redirect_to(spree.admin_unauthorized_path)
19+
else
20+
store_location
21+
22+
redirect_to(spree.admin_login_path)
23+
end
24+
end
25+
26+
private
27+
28+
attr_reader :controller
29+
30+
delegate :flash, :redirect_to, :spree_current_user, :store_location, :spree, to: :controller
31+
end
32+
end
33+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module Auth
5+
# This service object is responsible for handling unauthorized redirects
6+
class UnauthorizedCustomerAccessHandler
7+
# @param controller [ApplicationController] an instance of ApplicationController
8+
# or its subclasses.
9+
def initialize(controller)
10+
@controller = controller
11+
end
12+
13+
# This method is responsible for handling unauthorized redirects
14+
def call
15+
if spree_current_user
16+
flash[:error] = I18n.t('spree.authorization_failure')
17+
18+
redirect_back(fallback_location: spree.unauthorized_path)
19+
else
20+
store_location
21+
22+
redirect_back(fallback_location: spree.login_path)
23+
end
24+
end
25+
26+
private
27+
28+
attr_reader :controller
29+
30+
delegate :flash, :redirect_back, :spree_current_user, :store_location, :spree, to: :controller
31+
end
32+
end
33+
end

lib/spree/auth/engine.rb

+2-18
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,13 @@ class Engine < Rails::Engine
3030

3131
def self.prepare_backend
3232
Spree::Admin::BaseController.unauthorized_redirect = -> do
33-
if spree_current_user
34-
flash[:error] = I18n.t('spree.authorization_failure')
35-
36-
redirect_to(spree.admin_unauthorized_path)
37-
else
38-
store_location
39-
40-
redirect_to(spree.admin_login_path)
41-
end
33+
Spree::Auth::UnauthorizedAdminAccessHandler.new(self).call
4234
end
4335
end
4436

4537
def self.prepare_frontend
4638
Spree::BaseController.unauthorized_redirect = -> do
47-
if spree_current_user
48-
flash[:error] = I18n.t('spree.authorization_failure')
49-
50-
redirect_back(fallback_location: spree.unauthorized_path)
51-
else
52-
store_location
53-
54-
redirect_back(fallback_location: spree.login_path)
55-
end
39+
Spree::Auth::UnauthorizedCustomerAccessHandler.new(self).call
5640
end
5741
end
5842
end

0 commit comments

Comments
 (0)