我不确定这个问题是一般的Rails问题还是特定的Redmine。
有一个类User,它有一个类方法try_to_login。我编写了一个包含method_alias_chain的模块来包装该方法并提供其他功能。如果我进入控制台并调用try_to_login,这样可以正常工作。我的包装将被执行,一切都很好。但是,当我在服务器上运行它时,只调用vanilla方法。永远不会碰到包装纸。我在vanilla方法中添加了一个logger命令,以确保它被调用。
以下是代码的简化版本:
require_dependency 'principal'
require_dependency 'user'
require 'login_attempt_count'
module UserLoginAttemptLimiterPatch
def self.included(base)
base.extend ClassMethods
base.class_eval do
class << self
alias_method_chain :try_to_login, :attempt_limit
end
end
end
module ClassMethods
def try_to_login_with_attempt_limit(login, password)
user = try_to_login_without_attempt_limit login, password
#stuff here gets called via console but not via browser
user
end
def authentication_failed(login)
#important code here
end
end
end
User.send(:include, UserLoginAttemptLimiterPatch)
此外,在加载插件时需要此模块。
答案 0 :(得分:3)
您是如何要求模块的?如果您在开发模式下运行,可以在第一个请求之后重新加载User类,这将清除您的补丁和alias_method_chain。
你可以通过在Dispatcher中运行补丁来解决它(在每次重载代码时运行):
require 'dispatcher'
Dispatcher.to_prepare do
Issue.send(:include, MyMooPatch)
end
参考:http://theadmin.org/articles/2009/04/13/how-to-modify-core-redmine-classes-from-a-plugin/