I've been working on engines lately and I can't understand why a class in an engine can't be monkey patched. Here's an example:
In the engine:
class MyEngine::MyController < ApplicationController
def index
@stuff = 1
end
def show
... something happens ...
end
end
In the app including the engine:
class MyEngine::MyController < ApplicationController
def index
@stuff = 2
end
end
In this example I would expect for only the index method to be overriden, but it seems the entire class is overriden. Is there any reason for this behaviour? Is this a feature or a bug?
Probably because of the way how Rails is looking for constants ? I would guess that it does not read the file from engine at all, and just reads the file from your app.
Also, even if I change the load order of the app and engine and load the engine first the same thing happens. This way the engine’s file has already been loaded and then it is completly overriden.
Maybe I’m missing something here, but this is the behavior I have experienced.
I just clicked thru those slides the other day too. Having developed engines locally bundled to other applications, the part he talks about continuous development, is a dream. I found that in Bundler 1.2 you can config a repo for local usage without changing the Gemfile too.
As I’ve said, even if I change the loading order it still won’t work.
I’ll try class_eval but still I don’t understand why the monkey patching does not work. Is it a feature of the engine that it has an all or nothing overriding of classes?