Say I want to extend/override a method on something like ActionController.
Where should I put this "monkeypatch"? Currently I am putting it in config/initializers but Im thinking there is a better place.
Thanks
Say I want to extend/override a method on something like ActionController.
Where should I put this "monkeypatch"? Currently I am putting it in config/initializers but Im thinking there is a better place.
Thanks
Quoting BallaBall <balla@sogetthis.com>:
Say I want to extend/override a method on something like ActionController.
Where should I put this "monkeypatch"? Currently I am putting it in config/initializers but Im thinking there is a better place.
app/controllers/application_controller.rb
It inherits from ActionController::Base and all other controllers inherit from it.
HTH, Jeffrey
Thanks Jeffrey but I am aware of this. Like you said it inherits from ActionController::Base but where should this file be kept?
For example if I reopen the class like:
module ActionController class Base def override_some_method end end end
Where should the above code live? Like I said in my first post is that I would create a file name action_controller_overrides.rb and place that in config/initializers but I am unsure if that is the best place to put it.
Why not put it in app/controllers/application_controller.rb? Are you trying to override the method for all Rails applications? Unless I do not understand what you are trying to do, I see no reason to not put it in application_controller.rb. This is exactly what this class is for - methods common to all controllers.
Jeffrey
Quoting BallaBall <balla@sogetthis.com>:
Short write up at: http://www.therailsway.com/2009/4/6/controller-inheritance
Quoting Jeffrey L. Taylor <ror@abluz.dyndns.org>:
I meant this in a more general way. Where should I put overrides. The example I listed for ApplicationController was just an example.
As another example say I want to override something in Memcache.
class Memcache # Overrides go here end
Where would that go. Should it go in something like app/lib/, config/ initializers, config/overrides ?
You should never re-open a class like this, always use class_eval and instance_eval:
Memcache.class_eval do
def your_instance_method_here end
def self.your_class_method_here end
end
Using class_eval instead of "re-defining" a class as you did will require the class to be loaded BEFORE your code is run, which is usually what you're looking for.