Redmineのプラグインのためにalias_method_chainを経由してクラスメソッドをラップ
-
21-09-2019 - |
質問
私はこの問題は、一般的なRailsの問題であるかの特定のRedmineのかどうかわからないです。
クラスメソッドtry_to_loginを持つクラスのユーザーがあります。私は、そのメソッドをラップして、追加の機能を提供するためにmethod_alias_chainを含むモジュールを書きました。私はコンソールに入るとtry_to_login呼び出す場合、これは正常に動作します。私のラッパーが実行され、すべてが正常です。しかし、私は、サーバー上でこれを実行するだけでバニラメソッドが呼び出されます。ラッパーは触れたことはありません。私は確かに、それは行為に呼び出されていることをバニラ方法に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)
また、プラグインがロードされたときに、このモジュールが必要である。
解決
どのようにモジュールを必要としていますか?あなたが開発モードで実行している場合は、Userクラスは、あなたのパッチとalias_method_chainをクリアだろう最初の要求の後にリロードすることができます。
あなたは(すべてのコードのリロードを実行します)ディスパッチャの内側にパッチを行うことによって、それを回避することができます:
require 'dispatcher'
Dispatcher.to_prepare do
Issue.send(:include, MyMooPatch)
end
参考:<のhref = "http://theadmin.org/articles/2009/04/13/how-to-modify-core-redmine-classes-from-a-plugin/" のrel = "nofollowをnoreferrer 「> http://theadmin.org/articles/2009/04/13/how-to-modify-core-redmine-classes-from-a-plugin/ の