I am using a Rails engine as a gem in my app. The engine has PostsController with a number of methods and I would like to extend the controller logic in my main
You can use Ruby's send() method to inject your code into the controller at the time the engine is created...
# lib/cool_engine/engine.rb
module CoolEngine
class Engine < ::Rails::Engine
isolate_namespace CoolEngine
initializer "cool_engine.load_helpers" do |app|
# You can inject magic into all your controllers...
ActionController::Base.send :include, CoolEngine::ActionControllerExtensions
ActionController::Base.send :include, CoolEngine::FooBar
# ...or add special sauce to models...
ActiveRecord::Base.send :include, CoolEngine::ActiveRecordExtensions
ActiveRecord::Base.send :include, CoolEngine::MoreStuff
# ...even provide a base set of helpers
ApplicationHelper.send :include, CoolEngine::Helpers
end
end
end
This method spares you from having to redefine the controller inheritance within your main app.