sorceryでpassword reset機能を実装する

こちらも公式wiki通りやれば、大体できます。
Reset password · NoamB/sorcery Wiki

1か所だけ修正

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

が出るのでenvironmentsに以下を追加します。

# config/environments/development.rb
  config.action_mailer.default_url_options = { host: 'localhostt' }

メールの送信間隔について

通常ActionMailerの配信時にはログにメールの内容が出力されるのですが、
なぜか連続して実行した際にメール配信が行われていないようでした。

とりあえずソースを見てみると

# sorcery-0.9.1/lib/sorcery/model/submodules/reset_password.rb
          def deliver_reset_password_instructions!
            config = sorcery_config
            # hammering protection
            return false if config.reset_password_time_between_emails.present? && self.send(config.reset_password_email_sent_at_attribute_name) && self.send(config.reset_password_email_sent_at_attribute_name) > config.reset_password_time_between_emails.seconds.ago.utc
            self.class.sorcery_adapter.transaction do
              generate_reset_password_token!
              send_reset_password_email! unless config.reset_password_mailer_disabled
            end
          end

reset_password_time_between_emails をconfigから取得し
reset_password_email_sent_at よりその秒数分、時間が経過しているかを見ているようでした。
 
ということで、一時的な対応としては
config/initializers/sorcery.rb に適当な値をセットしてやると問題なく出力されるようになります。

# example
reset_password_time_between_emails = 1

Action Mailer Preview

Rails4.1の新機能Action Mailer Previewsとは - rochefort's blog
 
Rails4.1からActionMailerのPreviewが参照できるようになったのですが
公式wikiに特に記述がなかったので補足すると

# Rspecの場合は以下。
# spec/mailers/previews/user_mailer_preview.rb
  def reset_password_email
    user = User.find_by_id(1)
    UserMailer.reset_password_email(user)
  end

のようにdevelopment環境で利用出来るuserを適当に用意して
ActionMailerのmethodを呼んであげれば良いです。
 
あとは、以下から確認できます。
http://localhost:3000/rails/mailers

See Also

Railsの認証にsorceryを使う - rochefort's blog