2012-01-09 12 views
2

には、既存のヘルパーを拡張コントローラーに追加することができます。以前はこのヘルパーは含まれていませんでした。既存のヘルパーを拡張コントローラー(Redmine Plugin Dev)に追加

例えば、私はtimelog_controller_patch.rbtimelog_controller.rbコントローラを拡張しました。次に、ヘルパークエリを追加しようとしましたが、これは私のパッチで使用したい機能をもたらします。

私は(私のtimelogがコントロールを拡張)私のパッチにヘルパーを追加した場合、私はいつも同じエラーを取得:

エラー:初期化されていない定数のRails ::プラグイン:: TimelogControllerPatch(NameError)

をここで私が行っている方法の例です:私は、元のコントローラに同じヘルパーを含む場合

module TimelogControllerPatch  
    def self.included(base) 
     base.send(:include, InstanceMethods) 
     base.class_eval do 
      alias_method_chain :index, :filters 
     end 
    end 
    module InstanceMethods 
     # Here, I include helper like this (I've noticed how the other controllers do it) 
     helper :queries 
     include QueriesHelper 

     def index_with_filters 
      # ... 
      # do stuff 
      # ... 
     end 
    end # module 
end # module patch 

しかし、すべてが(もちろん、これは正しい方法ではありません)正常に動作します。

誰かが私に間違っていることを教えてもらえますか?事前に

おかげで:)

答えて

4

helper方法は、それが正しく実行取得されていないモジュールにそれを置くことによって、コントローラのクラスで呼び出される必要があります。これは動作します:

module TimelogControllerPatch  
    def self.included(base) 
     base.send(:include, InstanceMethods) 
     base.class_eval do 
      alias_method_chain :index, :filters 
      # 
      # Anything you type in here is just like typing directly in the core 
      # source files and will be run when the controller class is loaded. 
      # 
      helper :queries 
      include QueriesHelper 

     end 
    end 
    module InstanceMethods 
     def index_with_filters 
      # ... 
      # do stuff 
      # ... 
     end 
    end # module 
end # module patch 

はGithubの上で私のプラグインのいずれかを見てお気軽に、私のパッチのほとんどはlib/plugin_name/patchesになります。私はヘルパーを追加する人がいることは知っていますが、今は見つけられません。 https://github.com/edavis10

P.あなたのパッチも必要とすることを忘れないでください。プラグインのlibディレクトリにない場合は、相対パスを使用します。

エリック・デイヴィス

+0

感謝を!それは魅力のように機能します!残念ながら、ドキュメントはかなり疎であり、私はこの問題の良い解決策を見つけることができませんでした。どうもありがとうございました。 –

0

パッチでそれを行うにはしたくない代わり場合:

Rails.configuration.to_prepare do 
    TimelogController.send(:helper, :queries) 
end 
関連する問題