Reputation: 5781
I have included a gem (engine) into my application and called the appropriate install task... I have found that some of the functionality (that lives in the gem's lib/ folder) isn't quite how I would like it.
Is it possible to override (bits and pieces) of their method with my own? I have tried to create the same file in my application's lib/ folder (same directory structure), however, my method doesn't get called.
Edit:
I guess my question should be (and I'll edit it)... Say the Engine (Gem) provides me the following Module -- Gem::TheirModule is it possible for me to add the same Gem::TheirModule to my lib folder (include it in application.rb) and assume if I have defined a duplicate method that my method will be called and not theirs?
Thanks.
Upvotes: 0
Views: 1459
Reputation: 52347
As it is stated in the documentation on engines reopening the class and overriding the methods will allow you to expect your implementation to take over the engine's one.
Engine model and controller classes can be extended by open classing them in the main Rails application (since model and controller classes are just Ruby classes that inherit Rails specific functionality). Open classing an Engine class redefines it for use in the main application. This is usually implemented by using the decorator pattern.
For simple class modifications, use
Class#class_eval
. For complex class modifications, consider usingActiveSupport::Concern
.
Upvotes: 1
Reputation: 1324
Rails does not automatically require files in the /lib folder. You need to require them explicitly.
You can require these files in config/application.rb
.
Check out the module_eval
and class_eval
methods: They let you change or add functionality of existing modules/classes (of course you can just reopen the class and puts you changes there).
Upvotes: 2