ActiveJob , . - send_devise_notification , ,
class User < ApplicationRecord
devise :database_authenticatable, :confirmable
after_commit :send_pending_devise_notifications
protected
def send_devise_notification(notification, *args)
if new_record? || changed?
pending_devise_notifications << [notification, args]
else
render_and_send_devise_message(notification, *args)
end
end
private
def send_pending_devise_notifications
pending_devise_notifications.each do |notification, args|
render_and_send_devise_message(notification, *args)
end
pending_devise_notifications.clear
end
def pending_devise_notifications
@pending_devise_notifications ||= []
end
def render_and_send_devise_message(notification, *args)
message = devise_mailer.send(notification, self, *args)
if message.respond_to?(:deliver_later)
message.deliver_later
elsif message.respond_to?(:deliver_now)
message.deliver_now
else
message.deliver
end
end
end