-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract redirection behavior from lambda to classes
We have to lambdas, so we need two classes.
- Loading branch information
Showing
3 changed files
with
68 additions
and
18 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
app/models/spree/auth/unauthorized_admin_access_handler.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Auth | ||
# This service object is responsible for handling unauthorized redirects | ||
class UnauthorizedAdminAccessHandler | ||
# @param controller [ApplicationController] an instance of ApplicationController | ||
# or its subclasses. | ||
def initialize(controller) | ||
@controller = controller | ||
end | ||
|
||
# This method is responsible for handling unauthorized redirects | ||
def call | ||
if spree_current_user | ||
flash[:error] = I18n.t('spree.authorization_failure') | ||
|
||
redirect_to(spree.admin_unauthorized_path) | ||
else | ||
store_location | ||
|
||
redirect_to(spree.admin_login_path) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :controller | ||
|
||
delegate :flash, :redirect_to, :spree_current_user, :store_location, :spree, to: :controller | ||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
app/models/spree/auth/unauthorized_customer_access_handler.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Auth | ||
# This service object is responsible for handling unauthorized redirects | ||
class UnauthorizedCustomerAccessHandler | ||
# @param controller [ApplicationController] an instance of ApplicationController | ||
# or its subclasses. | ||
def initialize(controller) | ||
@controller = controller | ||
end | ||
|
||
# This method is responsible for handling unauthorized redirects | ||
def call | ||
if spree_current_user | ||
flash[:error] = I18n.t('spree.authorization_failure') | ||
|
||
redirect_back(fallback_location: spree.unauthorized_path) | ||
else | ||
store_location | ||
|
||
redirect_back(fallback_location: spree.login_path) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :controller | ||
|
||
delegate :flash, :redirect_back, :spree_current_user, :store_location, :spree, to: :controller | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters