设计,如何覆盖send_confirmation_instructions

AnA*_*ice 6 ruby-on-rails devise ruby-on-rails-3

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

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

有:

def send_confirmation_instructions
    generate_confirmation_token! if self.confirmation_token.nil?
    ::Devise.mailer.delay.confirmation_instructions(self)
end
Run Code Online (Sandbox Code Playgroud)

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

Tom*_*son 7

当您设置Devise时,您可以告诉它正在处理哪个模型(例如User); 然后,许多/大部分方法都适用于该类.所以这就是你要覆盖的东西.

以下是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
Run Code Online (Sandbox Code Playgroud)