-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Include Devise routes/controllers/views directly in the project #153
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,14 @@ class CheckoutController < Spree::StoreController | |
|
||
before_action :setup_for_current_state, only: [:edit, :update] | ||
|
||
before_action :check_registration, except: [:registration, :update_registration] | ||
before_action :check_authorization | ||
|
||
# This action builds some associations on the order, ex. addresses, which we | ||
# don't to build or save here. | ||
# TODO: this doesn't seem needed, see setup_for_current_state definition. Investigate if we can remove it. | ||
skip_before_action :setup_for_current_state, only: [:registration, :update_registration] | ||
|
||
helper 'spree/orders' | ||
|
||
rescue_from Spree::Core::GatewayError, with: :rescue_from_spree_gateway_error | ||
|
@@ -47,6 +55,20 @@ def update | |
end | ||
end | ||
|
||
def registration | ||
@user = Spree::User.new | ||
end | ||
|
||
def update_registration | ||
if params[:order][:email] =~ Devise.email_regexp && current_order.update(email: params[:order][:email]) | ||
redirect_to spree.checkout_path | ||
else | ||
flash[:registration_error] = t(:email_is_invalid, scope: [:errors, :messages]) | ||
@user = Spree::User.new | ||
render 'registration' | ||
end | ||
end | ||
|
||
private | ||
|
||
def update_order | ||
|
@@ -175,7 +197,9 @@ def ensure_sufficient_stock_lines | |
|
||
# Provides a route to redirect after order completion | ||
def completion_route | ||
spree.order_path(@order) | ||
return spree.order_path(@order) if spree_current_user | ||
|
||
spree.token_order_path(@order, @order.guest_token) | ||
end | ||
|
||
def setup_for_current_state | ||
|
@@ -240,5 +264,41 @@ def insufficient_stock_error | |
end | ||
end | ||
end | ||
|
||
def order_params | ||
params. | ||
fetch(:order, {}). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/DotPosition: Place the . on the next line, together with the method name. |
||
permit(:email) | ||
end | ||
|
||
def skip_state_validation? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint/DuplicateMethods: Method Spree::CheckoutController#skip_state_validation? is defined at both app/controllers/spree/checkout_controller.rb:160 and app/controllers/spree/checkout_controller.rb:274. |
||
%w(registration update_registration).include?(params[:action]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/PercentLiteralDelimiters: %w-literals should be delimited by [ and ]. |
||
end | ||
|
||
def check_authorization | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint/DuplicateMethods: Method Spree::CheckoutController#check_authorization is defined at both app/controllers/spree/checkout_controller.rb:247 and app/controllers/spree/checkout_controller.rb:278. |
||
authorize!(:edit, current_order, cookies.signed[:guest_token]) | ||
end | ||
|
||
# Introduces a registration step whenever the +registration_step+ preference is true. | ||
def check_registration | ||
return unless registration_required? | ||
|
||
store_location | ||
redirect_to spree.checkout_registration_path | ||
end | ||
|
||
def registration_required? | ||
Spree::Auth::Config[:registration_step] && | ||
!already_registered? | ||
end | ||
|
||
def already_registered? | ||
spree_current_user || guest_authenticated? | ||
end | ||
|
||
def guest_authenticated? | ||
current_order&.email.present? && | ||
Spree::Config[:allow_guest_checkout] | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class Spree::UserConfirmationsController < Devise::ConfirmationsController | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ClassAndModuleChildren: Use nested module/class definitions instead of compact style. |
||
helper 'spree/base', 'spree/store' | ||
|
||
include Spree::Core::ControllerHelpers::Auth | ||
include Spree::Core::ControllerHelpers::Common | ||
include Spree::Core::ControllerHelpers::Order | ||
include Spree::Core::ControllerHelpers::Store | ||
|
||
include SolidusStarterFrontend::Taxonomies | ||
|
||
protected | ||
|
||
def after_confirmation_path_for(resource_name, resource) | ||
signed_in?(resource_name) ? spree.signed_in_root_path(resource) : spree.login_path | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
class Spree::UserPasswordsController < Devise::PasswordsController | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ClassAndModuleChildren: Use nested module/class definitions instead of compact style. |
||
helper 'spree/base', 'spree/store' | ||
|
||
include Spree::Core::ControllerHelpers::Auth | ||
include Spree::Core::ControllerHelpers::Common | ||
include Spree::Core::ControllerHelpers::Order | ||
include Spree::Core::ControllerHelpers::Store | ||
|
||
include SolidusStarterFrontend::Taxonomies | ||
|
||
# Overridden due to bug in Devise. | ||
# respond_with resource, location: new_session_path(resource_name) | ||
# is generating bad url /session/new.user | ||
# | ||
# overridden to: | ||
# respond_with resource, location: spree.login_path | ||
# | ||
def create | ||
self.resource = resource_class.send_reset_password_instructions(params[resource_name]) | ||
|
||
set_flash_message(:notice, :send_instructions) if is_navigational_format? | ||
|
||
if resource.errors.empty? | ||
respond_with resource, location: spree.login_path | ||
else | ||
respond_with_navigational(resource) { render :new } | ||
end | ||
end | ||
|
||
# Devise::PasswordsController allows for blank passwords. | ||
# Silly Devise::PasswordsController! | ||
# Fixes spree/spree#2190. | ||
def update | ||
if params[:spree_user][:password].blank? | ||
self.resource = resource_class.new | ||
resource.reset_password_token = params[:spree_user][:reset_password_token] | ||
set_flash_message(:error, :cannot_be_blank) | ||
render :edit | ||
else | ||
super | ||
end | ||
end | ||
|
||
protected | ||
|
||
def translation_scope | ||
'devise.user_passwords' | ||
end | ||
|
||
def new_session_path(resource_name) | ||
spree.send("new_#{resource_name}_session_path") | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
class Spree::UserRegistrationsController < Devise::RegistrationsController | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ClassAndModuleChildren: Use nested module/class definitions instead of compact style. |
||
helper 'spree/base', 'spree/store' | ||
|
||
include Spree::Core::ControllerHelpers::Auth | ||
include Spree::Core::ControllerHelpers::Common | ||
include Spree::Core::ControllerHelpers::Order | ||
include Spree::Core::ControllerHelpers::Store | ||
|
||
include SolidusStarterFrontend::Taxonomies | ||
|
||
before_action :check_permissions, only: [:edit, :update] | ||
skip_before_action :require_no_authentication | ||
|
||
def create | ||
build_resource(spree_user_params) | ||
if resource.save | ||
set_flash_message(:notice, :signed_up) | ||
sign_in(:spree_user, resource) | ||
session[:spree_user_signup] = true | ||
respond_with resource, location: after_sign_up_path_for(resource) | ||
else | ||
clean_up_passwords(resource) | ||
respond_with(resource) do |format| | ||
format.html { render :new } | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def translation_scope | ||
'devise.user_registrations' | ||
end | ||
|
||
def check_permissions | ||
authorize!(:create, resource) | ||
end | ||
|
||
private | ||
|
||
def spree_user_params | ||
params.require(:spree_user).permit(Spree::PermittedAttributes.user_attributes | [:email]) | ||
end | ||
end |
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.
Layout/DotPosition: Place the . on the next line, together with the method name.