-
-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decorate Spree::UsersController
when already existing from Solidus Frontend
#206
Open
spaghetticode
wants to merge
1
commit into
solidusio:main
Choose a base branch
from
spaghetticode:spaghetticode/remove-account-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
lib/decorators/frontend/controllers/spree/users_controller_decorator.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,87 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module UsersControllerDecorator | ||
def self.prepended(base) | ||
base.prepend_before_action :authorize_actions, only: :new | ||
base.prepend_before_action :load_object, only: [:show, :edit, :update] | ||
end | ||
|
||
def create | ||
@user = Spree::User.new(user_params) | ||
if @user.save | ||
|
||
if current_order | ||
session[:guest_token] = nil | ||
end | ||
|
||
redirect_back_or_default(root_url) | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def update | ||
if @user.update(user_params) | ||
spree_current_user.reload | ||
redirect_url = spree.account_url | ||
|
||
if params[:user][:password].present? | ||
# this logic needed b/c devise wants to log us out after password changes | ||
if Spree::Auth::Config[:signout_after_password_change] | ||
redirect_url = spree.login_url | ||
else | ||
bypass_sign_in(@user) | ||
end | ||
end | ||
redirect_to redirect_url, notice: I18n.t('spree.account_updated') | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
private | ||
|
||
def user_params | ||
params.require(:user).permit(Spree::PermittedAttributes.user_attributes | [:email]) | ||
end | ||
|
||
def authorize_actions | ||
authorize! params[:action].to_sym, Spree::User.new | ||
end | ||
|
||
# When using a version of Solidus that includes the account page, we just need to | ||
# decorate the existing controller with further behavior from this extension. | ||
# | ||
# On the other hand, when using a version of Solidus that does not include the | ||
# account page, we need to define the controller from scratch. | ||
# | ||
# Once we drop the support for all Solidus versions that don't include the | ||
# account page, we can just leave the decorator and remove anything else, including | ||
# the view file at `lib/views/frontend/spree/users/show.html.erb`. | ||
if defined?(Spree::UsersController) | ||
Spree::UsersController.prepend(self) | ||
else | ||
class Spree::UsersController < Spree::StoreController | ||
skip_before_action :set_current_order, only: :show, raise: false | ||
|
||
include Spree::Core::ControllerHelpers | ||
|
||
def show | ||
@orders = @user.orders.complete.order('completed_at desc') | ||
end | ||
|
||
def load_object | ||
@user ||= Spree::User.find_by(id: spree_current_user&.id) | ||
authorize! params[:action].to_sym, @user | ||
end | ||
|
||
def accurate_title | ||
I18n.t('spree.my_account') | ||
end | ||
|
||
prepend Spree::UsersControllerDecorator | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spaghetticode What about keeping the controller in the past directory and duplicating the ifs? I'm just thinking that people may expect to look for this controller in that folder and not finding it. On the other side, I understand that the controller code will be split into two different files and people can't know that this file exists. Well, unless we tell them with a code comment! 😬
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would that work with Zeitwerk?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it does, I was more worried about this version to be honest. With what I proposed, we'll have
Spree::UsersController
class defined intolib/controllers/frontend/spree/users_controller.rb
.