2013/08/01

Redmine plugin にて自動ロード対象のディレクトリを追加するには

どうやら Redmine はプラグインの lib は自動的にロードパスに登録するが、app 以下は controllers, models, helpers のみを autoload の対象にするようだ。
lib/redmine/plugin.rb にこんな記述があった。

# Adds the app/{controllers,helpers,models} directories of the plugin to the autoload path
Dir.glob File.expand_path(File.join(p.directory, 'app', '{controllers,helpers,models}')) do |dir|
ActiveSupport::Dependencies.autoload_paths += [dir]
end

例えば、フォームクラスを作って app/forms に配置したい場合は、init.rb に処理を追加してやる必要がある。
Redmine::Plugin.register :hoge do
# other plugin settings
# simple
ActiveSupport::Dependencies.autoload_paths += [File.dirname(__FILE__) + "/app/forms"]
# multiple
Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), "app", "{forms,services}"))) do |dir|
ActiveSupport::Dependencies.autoload_paths += [dir]
end
end
一つしか追加しないなら simple の方でいいだろうし、複数追加するなら plugin.rb をパクって multiple で書くのがいいんじゃないかな。

0 件のコメント :

コメントを投稿