From 78bbf6dcc4d0a63ab68c31d31d8905dbf8f9c1bb Mon Sep 17 00:00:00 2001 From: ALLEN WANG QIANG Date: Tue, 3 May 2016 08:32:14 +0800 Subject: [PATCH] Send on create confirmation email after commit (#4064) Call send_on_create_confirmation_instructions in after_commit instead of after_create, I think this is no harm in general and it makes things like async job work. Fix #4062 --- Gemfile | 1 + Gemfile.lock | 3 +++ gemfiles/Gemfile.rails-4.1-stable | 1 + gemfiles/Gemfile.rails-4.1-stable.lock | 5 ++++- gemfiles/Gemfile.rails-4.2-stable | 1 + gemfiles/Gemfile.rails-4.2-stable.lock | 7 +++++-- lib/devise/models/confirmable.rb | 11 ++++++++--- test/orm/active_record.rb | 4 +++- 8 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 97187bb9..738aba2d 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,7 @@ group :test do gem "omniauth-openid", "~> 1.0.1" gem "webrat", "0.7.3", require: false gem "mocha", "~> 1.1", require: false + gem 'test_after_commit', require: false end platforms :jruby do diff --git a/Gemfile.lock b/Gemfile.lock index 55f456ec..4fc37cd4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -145,6 +145,8 @@ GEM activesupport (>= 4.0) sprockets (>= 3.0.0) sqlite3 (1.3.11) + test_after_commit (1.0.0) + activerecord (>= 3.2) thor (0.19.1) thread_safe (0.3.5) tzinfo (1.2.2) @@ -173,6 +175,7 @@ DEPENDENCIES rails (~> 4.2.6) rdoc sqlite3 + test_after_commit webrat (= 0.7.3) BUNDLED WITH diff --git a/gemfiles/Gemfile.rails-4.1-stable b/gemfiles/Gemfile.rails-4.1-stable index 72d73d6f..aa966c1c 100644 --- a/gemfiles/Gemfile.rails-4.1-stable +++ b/gemfiles/Gemfile.rails-4.1-stable @@ -12,6 +12,7 @@ group :test do gem "omniauth-openid", "~> 1.0.1" gem "webrat", "0.7.3", require: false gem "mocha", "~> 1.1", require: false + gem 'test_after_commit', require: false end platforms :jruby do diff --git a/gemfiles/Gemfile.rails-4.1-stable.lock b/gemfiles/Gemfile.rails-4.1-stable.lock index b67b6de6..76652824 100644 --- a/gemfiles/Gemfile.rails-4.1-stable.lock +++ b/gemfiles/Gemfile.rails-4.1-stable.lock @@ -48,7 +48,7 @@ GIT PATH remote: .. specs: - devise (4.0.0.rc2) + devise (4.0.1) bcrypt (~> 3.0) orm_adapter (~> 0.1) railties (>= 4.1.0, < 5.1) @@ -133,6 +133,8 @@ GEM activesupport (>= 3.0) sprockets (>= 2.8, < 4.0) sqlite3 (1.3.11) + test_after_commit (1.0.0) + activerecord (>= 3.2) thor (0.19.1) thread_safe (0.3.5) tzinfo (1.2.2) @@ -161,6 +163,7 @@ DEPENDENCIES rails! rdoc sqlite3 + test_after_commit webrat (= 0.7.3) BUNDLED WITH diff --git a/gemfiles/Gemfile.rails-4.2-stable b/gemfiles/Gemfile.rails-4.2-stable index de9c4d05..10c0c1b6 100644 --- a/gemfiles/Gemfile.rails-4.2-stable +++ b/gemfiles/Gemfile.rails-4.2-stable @@ -12,6 +12,7 @@ group :test do gem "omniauth-openid", "~> 1.0.1" gem "webrat", "0.7.3", require: false gem "mocha", "~> 1.1", require: false + gem 'test_after_commit', require: false end platforms :jruby do diff --git a/gemfiles/Gemfile.rails-4.2-stable.lock b/gemfiles/Gemfile.rails-4.2-stable.lock index 6144a11d..751c8d35 100644 --- a/gemfiles/Gemfile.rails-4.2-stable.lock +++ b/gemfiles/Gemfile.rails-4.2-stable.lock @@ -58,7 +58,7 @@ GIT PATH remote: .. specs: - devise (4.0.0.rc2) + devise (4.0.1) bcrypt (~> 3.0) orm_adapter (~> 0.1) railties (>= 4.1.0, < 5.1) @@ -144,7 +144,7 @@ GEM rake (11.0.1) rdoc (4.2.2) json (~> 1.4) - responders (2.1.1) + responders (2.1.2) railties (>= 4.2.0, < 5.1) ruby-openid (2.7.0) sprockets (3.5.2) @@ -155,6 +155,8 @@ GEM activesupport (>= 4.0) sprockets (>= 3.0.0) sqlite3 (1.3.11) + test_after_commit (1.0.0) + activerecord (>= 3.2) thor (0.19.1) thread_safe (0.3.5) tzinfo (1.2.2) @@ -183,6 +185,7 @@ DEPENDENCIES rails! rdoc sqlite3 + test_after_commit webrat (= 0.7.3) BUNDLED WITH diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index 02f4847b..c00e41da 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -43,9 +43,15 @@ module Devise included do before_create :generate_confirmation_token, if: :confirmation_required? - after_create :send_on_create_confirmation_instructions, if: :send_confirmation_notification? + after_create :skip_reconfirmation!, if: :send_confirmation_notification? + if respond_to?(:after_commit) # ActiveRecord + after_commit :send_on_create_confirmation_instructions, on: :create, if: :send_confirmation_notification? + after_commit :send_reconfirmation_instructions, on: :update, if: :reconfirmation_required? + else # Mongoid + after_create :send_on_create_confirmation_instructions, if: :send_confirmation_notification? + after_update :send_reconfirmation_instructions, if: :reconfirmation_required? + end before_update :postpone_email_change_until_confirmation_and_regenerate_confirmation_token, if: :postpone_email_change? - after_update :send_reconfirmation_instructions, if: :reconfirmation_required? end def initialize(*args, &block) @@ -169,7 +175,6 @@ module Devise # in models to map to a nice sign up e-mail. def send_on_create_confirmation_instructions send_confirmation_instructions - skip_reconfirmation! end # Callback to overwrite if confirmation is required or not. diff --git a/test/orm/active_record.rb b/test/orm/active_record.rb index 2386f1fe..d9455434 100644 --- a/test/orm/active_record.rb +++ b/test/orm/active_record.rb @@ -5,9 +5,11 @@ ActiveRecord::Base.include_root_in_json = true ActiveRecord::Migrator.migrate(File.expand_path("../../rails_app/db/migrate/", __FILE__)) class ActiveSupport::TestCase - if Rails.version >= '5.0.0' + if Devise.rails5? self.use_transactional_tests = true else + # Let `after_commit` work with transactional fixtures, however this is not needed for Rails 5. + require 'test_after_commit' self.use_transactional_fixtures = true end