Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise, how to override send_confirmation_instructions

I'm trying to override the method 'send_confirmation_instructions' as shown here:

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

This seems to no longer work with the latest version of devise. The devise docs show how to override a controller but not a model. Any suggestions on how to override a devise model? Thanks

like image 270
AnApprentice Avatar asked Oct 11 '12 02:10

AnApprentice


1 Answers

When you set up Devise, you tell it which model it's working on (e.g. User); many/most of its methods then apply to that class. So that's where you'll want to override stuff.

Here's a comment from the Devise code at lib/devise/models/authenticatable.rb that describes almost exactly what you want to do, if I am reading correctly.

  # 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
like image 129
Tom Harrison Avatar answered Sep 30 '22 15:09

Tom Harrison