设计,如何覆盖send_confirmation_instructions

时间:2012-10-11 02:44:54

标签: ruby-on-rails ruby-on-rails-3 devise

我正在尝试覆盖方法'send_confirmation_instructions',如下所示:

http://trackingrails.com/posts/devise-send-confirmation-mail-manually-or-delay-them

with:

def send_confirmation_instructions
    generate_confirmation_token! if self.confirmation_token.nil?
    ::Devise.mailer.delay.confirmation_instructions(self)
end

这似乎不再适用于最新版本的设计。设计文档显示如何覆盖控制器而不是模型。关于如何覆盖设计模型的任何建议?感谢

2 个答案:

答案 0 :(得分:7)

设置Devise时,告诉它正在使用哪种型号(例如用户);然后,许多/大部分方法都适用于该类。所以这就是你要覆盖的东西。

以下是来自lib/devise/models/authenticatable.rb的设计代码的评论,如果我正确阅读,几乎可以准确地描述您想要做什么。

  # This is an internal method called every time Devise needs
  # to send a notification/mail. This can be overriden if you
  # need to customize the e-mail delivery logic. For instance,
  # if you are using a queue to deliver e-mails (delayed job,
  # sidekiq, resque, etc), you must add the delivery to the queue
  # just after the transaction was committed. To achieve this,
  # you can override send_devise_notification to store the
  # deliveries until the after_commit callback is triggered:
  #
  #     class User
  #       devise :database_authenticatable, :confirmable
  #
  #       after_commit :send_pending_notifications
  #
  #       protected
  #
  #       def send_devise_notification(notification)
  #         pending_notifications << notification
  #       end
  #
  #       def send_pending_notifications
  #         pending_notifications.each do |n|
  #           devise_mailer.send(n, self).deliver
  #         end
  #       end
  #
  #       def pending_notifications
  #         @pending_notifications ||= []
  #       end
  #     end
  #
  def send_devise_notification(notification)
    devise_mailer.send(notification, self).deliver
  end

答案 1 :(得分:0)

为什么不使用devise-async

Usage

Devise >= 2.1.1

Include Devise::Async::Model to your Devise model

class User < ActiveRecord::Base
  devise :database_authenticatable, :confirmable # etc ...

  include Devise::Async::Model # should be below call to `devise`
end

Devise < 2.1.1

Set Devise::Async::Proxy as Devise's mailer in config/initializers/devise.rb:

# Configure the class responsible to send e-mails.
config.mailer = "Devise::Async::Proxy"

All

Set your queuing backend by creating config/initializers/devise_async.rb:

# Supported options: :resque, :sidekiq, :delayed_job
Devise::Async.backend = :resque