Skip to content

Commit 8719586

Browse files
committed
Fix #1251 devise mailer with async.
1 parent 218d988 commit 8719586

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

app/models/user/deviseable.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ module Deviseable
99
attr_accessor :omniauth_provider, :omniauth_uid
1010

1111
devise :database_authenticatable, :registerable, :recoverable, :lockable,
12-
:rememberable, :trackable, :validatable, :omniauthable
12+
:rememberable, :trackable, :validatable, :omniauthable
1313

1414
after_create :bind_omniauth_on_create
15-
end
1615

17-
def password_required?
18-
(authorizations.empty? || !password.blank?) && super
19-
end
16+
# Override Devise to send mails with async
17+
def send_devise_notification(notification, *args)
18+
devise_mailer.send(notification, self, *args).deliver_later
19+
end
2020

21-
# Override Devise to send mails with async
22-
def send_devise_notification(notification, *args)
23-
devise_mailer.send(notification, self, *args).deliver_later
21+
# Override Devise password_required?
22+
def password_required?
23+
(authorizations.empty? || !password.blank?) && super
24+
end
2425
end
2526

2627
def bind?(provider)
@@ -95,7 +96,7 @@ def new_from_provider_data(provider, uid, data)
9596
return user
9697
end
9798

98-
Rails.logger.warn("User.create_from_hash 失败,#{user.errors.inspect}")
99+
Rails.logger.warn("User.create_from_hash error: #{user.errors.inspect}")
99100
return nil
100101
end
101102
end

test/models/user/deviseable_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require "test_helper"
44

55
class User::DeviseableTest < ActiveSupport::TestCase
6+
include ActiveJob::TestHelper
7+
68
class Monkey < ApplicationRecord
79
include User::Deviseable
810
end
@@ -62,4 +64,12 @@ class Monkey < ApplicationRecord
6264
description = data["description"] = "A newbie Ruby developer"
6365
assert_equal description, Monkey.new_from_provider_data(nil, nil, data).tagline
6466
end
67+
68+
test "async mailer" do
69+
user = create(:user)
70+
71+
assert_performed_jobs 1 do
72+
user.send(:send_devise_notification, :reset_password_instructions, "foobar")
73+
end
74+
end
6575
end

0 commit comments

Comments
 (0)