Skip to content

Commit

Permalink
send activation email after the commit in transactional databases (#130)
Browse files Browse the repository at this point in the history
* send activation email after the commit in transactional databases

* extract callback_name to separate method

* fix typo in parameter

* disable transactional fixtures to be able to test after_commit

* Raise on failure when change_password!
  • Loading branch information
anaumov authored and Ch4s3 committed Jul 9, 2018
1 parent 2fea1d7 commit 8959f8c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/sorcery/adapters/active_record_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def define_field(name, type, options = {})
end

def define_callback(time, event, method_name, options = {})
@klass.send "#{time}_#{event}", method_name, options.slice(:if)
@klass.send "#{time}_#{event}", method_name, options.slice(:if, :on)
end

def find_by_oauth_credentials(provider, uid)
Expand Down
10 changes: 9 additions & 1 deletion lib/sorcery/adapters/mongoid_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ def define_field(name, type, options={})
end

def define_callback(time, event, method_name, options={})
@klass.send "#{time}_#{event}", method_name, options.slice(:if)
@klass.send callback_name(time, event, options), method_name, options.slice(:if)
end

def callback_name(time, event, options)
if event == :commit
options[:on] == :create ? "#{time}_create" : "#{time}_save"
else
"#{time}_#{event}"
end
end

def credential_regex(credential)
Expand Down
6 changes: 3 additions & 3 deletions lib/sorcery/model/submodules/reset_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ def deliver_reset_password_instructions!
end
mail
end

# Increment access_count_to_reset_password_page attribute.
# For example, access_count_to_reset_password_page attribute is over 1, which
# means the user doesn't have a right to access.
def increment_password_reset_page_access_counter
sorcery_adapter.increment(self.sorcery_config.reset_password_page_access_count_attribute_name)
end

# Reset access_count_to_reset_password_page attribute into 0.
# This is expected to be used after sending an instruction email.
def reset_password_reset_page_access_counter
Expand All @@ -126,7 +126,7 @@ def reset_password_reset_page_access_counter
def change_password!(new_password)
clear_reset_password_token
send(:"#{sorcery_config.password_attribute_name}=", new_password)
sorcery_adapter.save
sorcery_adapter.save raise_on_failure: true
end

protected
Expand Down
2 changes: 1 addition & 1 deletion lib/sorcery/model/submodules/user_activation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.included(base)
# don't setup activation if no password supplied - this user is created automatically
sorcery_adapter.define_callback :before, :create, :setup_activation, if: proc { |user| user.send(sorcery_config.password_attribute_name).present? }
# don't send activation needed email if no crypted password created - this user is external (OAuth etc.)
sorcery_adapter.define_callback :after, :create, :send_activation_needed_email!, if: :send_activation_needed_email?
sorcery_adapter.define_callback :after, :commit, :send_activation_needed_email!, on: :create, if: :send_activation_needed_email?
end

base.sorcery_config.after_config << :validate_mailer_defined
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestMailer < ActionMailer::Base; end
config.include RSpec::Rails::ControllerExampleGroup, file_path: /controller(.)*_spec.rb$/
config.mock_with :rspec

config.use_transactional_fixtures = true
config.use_transactional_fixtures = false

config.before(:suite) { setup_orm }
config.after(:suite) { teardown_orm }
Expand Down

0 comments on commit 8959f8c

Please sign in to comment.