アプリの宝石としてRailsエンジンを使用しています。エンジンにはPostsController
いくつかのメソッドがあり、メインアプリのコントローラーロジックを拡張したいと思います。たとえば、いくつかのメソッドを追加します。メインアプリで作成PostsController
しただけでは、エンジンのコントローラーが読み込まれません。
問題の解決策が提案されています変更に基づいて機能を拡張するRailsエンジンActiveSupport::Dependencies#require_or_load
これを行う唯一の/正しい方法ですか?はいの場合、そのコードはどこに配置しますか?
編集1:
これは、Andrius forRails2.xによって提案されたコードです。
module ActiveSupport::Dependencies
alias_method :require_or_load_without_multiple, :require_or_load
def require_or_load(file_name, const_path = nil)
if file_name.starts_with?(RAILS_ROOT + '/app')
relative_name = file_name.gsub(RAILS_ROOT, '')
@engine_paths ||= Rails::Initializer.new(Rails.configuration).plugin_loader.engines.collect {|plugin| plugin.directory }
@engine_paths.each do |path|
engine_file = File.join(path, relative_name)
require_or_load_without_multiple(engine_file, const_path) if File.file?(engine_file)
end
end
require_or_load_without_multiple(file_name, const_path)
end
end