Use after_commit since after_save runs within the transaction as well.

This commit is contained in:
Guo Xiang Tan 2017-03-16 16:02:34 +08:00
parent 299b92b4d7
commit 9f299b6842
3 changed files with 7 additions and 8 deletions

View File

@ -120,6 +120,8 @@ group :test do
gem 'fakeweb', '~> 1.3.0', require: false
gem 'minitest', require: false
gem 'timecop'
# TODO: Remove once we upgrade to Rails 5.
gem 'test_after_commit'
end
group :test, :development do

View File

@ -359,6 +359,8 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
stackprof (0.2.10)
test_after_commit (1.1.0)
activerecord (>= 3.2)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
@ -470,6 +472,7 @@ DEPENDENCIES
sinatra
spork-rails
stackprof
test_after_commit
thor
timecop
uglifier

View File

@ -96,7 +96,6 @@ class User < ActiveRecord::Base
before_save :update_username_lower
before_save :ensure_password_is_hashed
before_save :setup_trigger_user_created_event
after_save :expire_tokens_if_password_changed
after_save :automatic_group_membership
@ -105,7 +104,7 @@ class User < ActiveRecord::Base
after_save :badge_grant
after_save :expire_old_email_tokens
after_save :index_search
after_save :trigger_user_created_event
after_commit :trigger_user_created_event, on: :create
before_destroy do
# These tables don't have primary keys, so destroying them with activerecord is tricky:
@ -1078,13 +1077,8 @@ class User < ActiveRecord::Base
end
end
def setup_trigger_user_created_event
@trigger_user_created_event = self.new_record?
true
end
def trigger_user_created_event
DiscourseEvent.trigger(:user_created, self) if @trigger_user_created_event
DiscourseEvent.trigger(:user_created, self)
true
end