Skip to content

Commit 8279db6

Browse files
committed
Extract redirection behavior from lambda to classes
We have two lambdas, so we need two classes.
1 parent c19b9ec commit 8279db6

3 files changed

Lines changed: 68 additions & 18 deletions

File tree

Lines changed: 33 additions & 0 deletions
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
Lines changed: 33 additions & 0 deletions
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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,13 @@ class Engine < Rails::Engine
2828

2929
def self.prepare_backend
3030
Spree::Admin::BaseController.unauthorized_redirect = -> do
31-
if spree_current_user
32-
flash[:error] = I18n.t("spree.authorization_failure")
33-
34-
redirect_to(spree.admin_unauthorized_path)
35-
else
36-
store_location
37-
38-
redirect_to(spree.admin_login_path)
39-
end
31+
Spree::Auth::UnauthorizedAdminAccessHandler.new(self).call
4032
end
4133
end
4234

4335
def self.prepare_frontend
4436
Spree::BaseController.unauthorized_redirect = -> do
45-
if spree_current_user
46-
flash[:error] = I18n.t("spree.authorization_failure")
47-
48-
redirect_back(fallback_location: spree.unauthorized_path)
49-
else
50-
store_location
51-
52-
redirect_back(fallback_location: spree.login_path)
53-
end
37+
Spree::Auth::UnauthorizedCustomerAccessHandler.new(self).call
5438
end
5539
end
5640
end

0 commit comments

Comments
 (0)