Ruby on rails Rails 4 Monkey补丁在执行类方法时引发异常
我有一个rails项目,用于身份验证。我想修改密码重置函数的行为,以便它不会创建新的重置令牌(如果已经存在)。我相信有关守则是:Ruby on rails Rails 4 Monkey补丁在执行类方法时引发异常,ruby-on-rails,ruby-on-rails-4,monkeypatching,sorcery,Ruby On Rails,Ruby On Rails 4,Monkeypatching,Sorcery,我有一个rails项目,用于身份验证。我想修改密码重置函数的行为,以便它不会创建新的重置令牌(如果已经存在)。我相信有关守则是: def deliver_reset_password_instructions! config = sorcery_config # hammering protection return false if config.reset_password_time_between_emails.present? && self.
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
我创建了以下猴子补丁来纠正此问题:
Sorcery::Model::Submodules::ResetPassword::InstanceMethods.module_eval do
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! unless reset_password_token.present?
send_reset_password_email! unless config.reset_password_mailer_disabled
end
end
end
我在开发过程中遇到以下错误:
用于#的未定义方法“巫术适配器”
我将非常感谢任何关于为什么会发生这种情况的见解或建议,如何创建一个功能补丁,或一个更好的方式进行整个事情。提前感谢。如何加载猴子补丁<初始化器中的code>prepend以前对我有效。我正在初始化器中加载它。如果我没有包含带有
self.class.sorcery\u适配器的行,那么补丁程序不会抛出异常。如果我将该块更改为放置“test msg”
,它将打印消息,而不是创建令牌。补丁似乎加载正确。self.class.name
在块中返回什么?self.class.name
返回Realtor
。Realtor是使用Sorcery进行身份验证的模型的名称。Sorcery\u适配器
作为类单例方法注入到ActiveRecord::Base
中,如所示。现在不知道为什么会丢失它,请通过删除.class
调用来尝试实例版本?