Rails 4 - 设计 - 经批准可确认

Rails 4 - Devise - Confirmable subject to approval

我正在努力学习如何阅读 ruby文档。我确信在尝试使用这些文档时我错过了一些基本的基本知识,但就目前而言,我很沮丧,因为向我提供的信息却没有任何形式的可理解的用户指南.

我想修改我的设计,以便用户可以指定他们想要加入的组 - 在这种情况下,他们的电子邮件更改为组织电子邮件的组织,等待组织所有者的确认。

我认为这个函数(我从下面的设计 ruby 文档中复制了它)会有所帮助。

#postpone_email_change_until_confirmation_and_regenerate_confirmation_token ⇒ Object (protected)

253
# File 'lib/devise/models/confirmable.rb', line 247

def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
  @reconfirmation_required = true
  self.unconfirmed_email = self.email
  self.email = self.email_was
  self.confirmation_token = nil
  generate_confirmation_token
end

我的问题是我不明白它是如何工作的或如何将它整合到我的应用程序中。

我是否将它添加到我的用户模型(只需复制和粘贴),然后在我的用户控制器中添加一行用于更新操作,以便电子邮件的任何更改都执行方法中列出的每个步骤?

如果我这样做,我应该给控制器调用起什么名字?

在user.rb我试过:

 def postpone_email_change_until_confirmation_and_regenerate_confirmation_token
      @reconfirmation_required = true
      self.unconfirmed_email = self.email
      self.email = self.email_was
      self.confirmation_token = nil
      generate_confirmation_token
    end

在用户控制器-更新操作中,我试过:

if @user.update(user_params)
user.postpone_email_change_until_confirmation_and_regenerate_confirmation_token

我显然试图错误地执行此操作。我不清楚我应该如何使用此功能。

谁能看出我哪里出错了?

Confirmable 关注该方法。 所以,你只需要在 user.rb

中定义它

例如

# Extensions
devise :database_authenticatable, :registerable, :confirmable,
       :recoverable, :rememberable, :trackable, :validatable, 
       :lastseenable, :timeoutable

考虑 :confirmable 符号。

然后如果您要更改电子邮件 before_updateafter_commit 如果所有条件都为 return 真值

将调用回调

您不能从任何地方调用受保护的实例方法。

In case of protected methods, you can call them from the scope of any object belonging to the same class. This means that when you pass a Person object p1 as an argument to a method on another Person object p2, you can access all the protected methods of p1 within that method.

来自 here

module Confirmable
  extend ActiveSupport::Concern

  included do
    # ...
    after_commit :send_on_create_confirmation_instructions, on: :create, if: :send_confirmation_notification?
    after_commit :send_reconfirmation_instructions, on: :update, if: :reconfirmation_required?
    #...
    before_update :postpone_email_change_until_confirmation_and_regenerate_confirmation_token, if: :postpone_email_change?
  end

here是调用回调的地方

然后devise会发送再确认邮件到user.unconfirmed_email

您可能需要覆盖重新确认说明电子邮件。这是一个例子:

# app/mailers/devise/mailer.rb

if defined?(ActionMailer)
  class Devise::Mailer < Devise.parent_mailer.constantize
    include Devise::Mailers::Helpers

    def confirmation_instructions(record, token, opts = {})
      @token = token
      if record.pending_reconfirmation?
        devise_mail(record, :reconfirmation_instructions, opts)
      else
        devise_mail(record, :confirmation_instructions, opts)
      end
    end
  end
end

和邮件视图

# app/views/devise/reconfirmation_instructions.text.erb

Hello, <%= @resource.fullname %>!

Please reconfirm your new email: <%= your_url_with_@token" %>

--
WBR, support team

希望对您有所帮助。

更新

启用 confirmation/reconfirmation 所需要做的只是向 User 模型添加所需的策略

devise :database_authenticatable, :registerable, :confirmable,
       :recoverable, :rememberable, :trackable, :validatable, 
       :lastseenable, :timeoutable

并覆盖设计邮件程序

# app/mailers/devise/mailer.rb

if defined?(ActionMailer)
  class Devise::Mailer < Devise.parent_mailer.constantize
    include Devise::Mailers::Helpers

    def confirmation_instructions(record, token, opts = {})
      @token = token
      if record.pending_reconfirmation?
        devise_mail(record, :reconfirmation_instructions, opts)
      else
        devise_mail(record, :confirmation_instructions, opts)
      end
    end
  end
end