Skip to content

Commit

Permalink
Merge pull request #509 from spree/mailers-urls-fix
Browse files Browse the repository at this point in the history
[SD-1004] Changes emails urls to contain current store url
  • Loading branch information
damianlegawiec authored Jan 6, 2021
2 parents 23df4ac + 66250fb commit 43347be
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
12 changes: 12 additions & 0 deletions app/controllers/spree/user_confirmations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ class Spree::UserConfirmationsController < Devise::ConfirmationsController

before_action :set_current_order

# POST /resource/confirmation
def create
self.resource = resource_class.send_confirmation_instructions(resource_params, current_store)
yield resource if block_given?

if successfully_sent?(resource)
respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
else
respond_with(resource)
end
end

# GET /resource/confirmation?confirmation_token=abcdef
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/spree/user_registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ def new
# POST /resource/sign_up
def create
@user = build_resource(spree_user_params)
resource.skip_confirmation_notification! if Spree::Auth::Config[:confirmable]
resource_saved = resource.save
yield resource if block_given?
if resource_saved
if resource.active_for_authentication?
set_flash_message :notice, :signed_up
sign_up(resource_name, resource)
session[:spree_user_signup] = true
resource.send_confirmation_instructions(current_store) if Spree::Auth::Config[:confirmable]
redirect_to_checkout_or_account_path(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}"
expire_data_after_sign_in!
resource.send_confirmation_instructions(current_store) if Spree::Auth::Config[:confirmable]
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
Expand Down
8 changes: 5 additions & 3 deletions app/mailers/spree/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ def reset_password_instructions(user, token, *_args)
@edit_password_reset_url = spree.edit_spree_user_password_url(reset_password_token: token, host: @current_store.url)
@user = user

mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :reset_password_instructions])
mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :reset_password_instructions]), store_url: @current_store.url
end

def confirmation_instructions(user, token, _opts = {})
@confirmation_url = spree.spree_user_confirmation_url(confirmation_token: token, host: Spree::Store.current.url)
current_store_id = _opts[:current_store_id]
@current_store = Spree::Store.find(current_store_id) || Spree::Store.current
@confirmation_url = spree_user_confirmation_url(confirmation_token: token, host: Spree::Store.current.url)
@email = user.email

mail to: user.email, from: from_address, subject: Spree::Store.current.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :confirmation_instructions])
mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :confirmation_instructions]), store_url: @current_store.url
end
end
end
25 changes: 25 additions & 0 deletions app/models/spree/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ def admin?
has_spree_role?('admin')
end

def self.send_confirmation_instructions(attributes = {}, current_store)
confirmable = find_by_unconfirmed_email_with_errors(attributes) if reconfirmable
unless confirmable.try(:persisted?)
confirmable = find_or_initialize_with_errors(confirmation_keys, attributes, :not_found)
end
confirmable.resend_confirmation_instructions(current_store) if confirmable.persisted?
confirmable
end

def resend_confirmation_instructions(current_store)
pending_any_confirmation do
send_confirmation_instructions(current_store)
end
end

def send_confirmation_instructions(current_store)
unless @raw_confirmation_token
generate_confirmation_token!
end

opts = pending_reconfirmation? ? { to: unconfirmed_email } : {}
opts[:current_store_id] = current_store.id
send_devise_notification(:confirmation_instructions, @raw_confirmation_token, opts)
end

def self.send_reset_password_instructions(attributes={}, current_store)
recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
recoverable.send_reset_password_instructions(current_store) if recoverable.persisted?
Expand Down
2 changes: 1 addition & 1 deletion spec/features/confirmation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.feature 'Confirmation', type: :feature, reload_user: true do
before do
set_confirmable_option(true)
Spree::UserMailer.stub(:confirmation_instructions).and_return(double(deliver: true))
expect(Spree::UserMailer).to receive(:confirmation_instructions).with(anything, anything, { current_store_id: Spree::Store.current.id }).and_return(double(deliver: true))
end

after(:each) { set_confirmable_option(false) }
Expand Down
2 changes: 1 addition & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
describe "confirmable", reload_user: true do
it "is confirmable if the confirmable option is enabled" do
set_confirmable_option(true)
Spree::UserMailer.stub(:confirmation_instructions).and_return(double(deliver: true))
Spree::UserMailer.stub(:confirmation_instructions).with(anything, anything, { current_store_id: Spree::Store.current.id }).and_return(double(deliver: true))
expect(Spree.user_class.devise_modules).to include(:confirmable)
set_confirmable_option(false)
end
Expand Down

0 comments on commit 43347be

Please sign in to comment.