2012-10-11 9 views
6

próbuję zastąpić metodą 'send_confirmation_instructions', jak pokazano poniżej:opracowania, jak zastąpić send_confirmation_instructions

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

z:

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

To wydaje się nie działać z najnowsza wersja programu. Dokumenty programistyczne pokazują, jak zastąpić kontroler, ale nie model. Wszelkie sugestie, jak zastąpić projektować? Dzięki

Odpowiedz

7

Po skonfigurowaniu Devise, możesz powiedzieć, który model działa (np. User); wiele/większość jego metod stosuje się wtedy do tej klasy. A więc tam właśnie chcesz zastąpić rzeczy.

Oto komentarz kodu Devise pod numerem lib/devise/models/authenticatable.rb, który opisuje prawie dokładnie to, co chcesz zrobić, jeśli dobrze czytam.

# 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 
+0

Dzięki, więc mówisz, dodaj "send_devise_notification" do mojego pliku user.rb? Próbowałem tego i nie zostałem wywołany ... – AnApprentice

+0

Tak, nadpisuj 'send_devise_notification' w swoim modelu użytkownika. Udało mi się przechwycić powiadomienie (właśnie wysłałem coś do dziennika, aby to sprawdzić) z aktualną wersją Devise. Ale aby wszystko działało, przeczytaj komentarz - nie jest to tylko kwestia zdefiniowania metody, musisz również dodać filtr 'after_commit', aby zbudować kolejkę z opóźnionym zadaniem (lub czymkolwiek innym). –

+0

Dzięki, ale nie w tym miejscu jest "send_confirmation_instructions"? i muszę zmodyfikować tę metodę "send_confirmation_instructions" – AnApprentice

0

Dlaczego nie skorzystać 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 
+0

Ponieważ dev-async nie przechowuje wirtualnych atrybutów ustawionych dla użytkownika (attr_accessor). – AnApprentice

+0

Jeśli chcesz użyć najnowszej wersji, nie jest to już opcja, jak widać w [ich dokumentach] (https://github.com/mhfs/devise-async#devise--40), które nie obsługują Wymyśl> = 4.0 –