I'm writing a Rails plugin to extend a Rails engine. Namely MyPlugin has MyEngine as a dependency.
On my Rails engine I have a MyEngine::Foo model.
I'd like to add new methods to this model so I created a file in my plugin app/models/my_engine/foo.rb which has the following code:
module MyEngine
class Foo
def sayhi
puts "hi"
end
end
end
If I enter the Rails console on the plugin dummy application I can find MyEngine::Foo, but runnning MyEngine::Foo.new.sayhi returns
NoMethodError: undefined method `sayhi'
Why MyPlugin cannot see the updates to MyEngine::Foo model? Where am I wrong?
Ok, found out. To make
MyPluginaware and able to modifyMyEnginemodels the engine must be required on the pluginengine.rblike so:In order to extend
MyEngine::Foomodel I then had to create a filelib/my_engine/foo_extension.rb:And require it in
config/initializers/my_engine_extensions.rbNow from
MyPluginI can:See ActiveSupport Concern documentation for more details.