As it stands now, you can't reopen Engine classes contained within the engine's /app
directory by simply adding the same class in the parent app's /app
dir. For example:
/my_engine/app/controllers/users_controller.rb
/my_app/app/controllers/users_controller.rb
The file from my_engine
will not even load if there is a file with the same name in the parent app. More details here:
http://www.cowboycoded.com/2011/02/28/why-you-cant-reopen-rails-3-engine-classes-from-the-parent-app/
I am looking for a workaround that will allow me to drop the same filename/class in the same path as the parent app, and reopen instead of overwrite the class. Maybe I am missing something obvious. I am able to make this work with a separate file (different filename) that uses class_eval, but I am not really happy with that solution. Any ideas on an elegant solution for this?
I am also wondering if there is a reason behind this restriction, or is it just a result of how rails loads files (see included link) and not intentional. It seems to me that changing the load behavior of engines to allow reopening classes in this manner would be a good feature in rails. I know it confused me at first, and I am sure other developers will struggle with this problem as well.
In Rails 3.2.2 / Ruby 1.9 turn on reloading of plugins, then require the class in the engine using require_dependency
before reopening the class and adding functionality. This works even in development environment (i.e class reloading).
# development.rb
config.reload_plugins = true
# app/controllers/my_engine/documents_controller.rb
require_dependency MyEngine::Engine.root.join('app', 'controllers', 'my_engine', 'documents_controller').to_s
module MyEngine
class DocumentsController
def show
render :text => 'different'
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With