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-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org>:> > 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. On Aug 22, 6:57 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> Quoting BallaBall <ba...-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org>: > > > 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
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-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org>:> > 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. > > On Aug 22, 6:57 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > Quoting BallaBall <ba...-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org>: > > > > > 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.
Short write up at: http://www.therailsway.com/2009/4/6/controller-inheritance Quoting Jeffrey L. Taylor <ror-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org>:> > 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.
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 ? On Aug 22, 5:54 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> 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 <ba...-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org>: > > > > > 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. > > > On Aug 22, 6:57 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > > Quoting BallaBall <ba...-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org>: > > > > > 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.
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. - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Mon, Aug 24, 2009 at 1:30 PM, BallaBall<balla-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org> wrote:> > 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 ? > > On Aug 22, 5:54 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: >> 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 <ba...-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org>: >> >> >> >> > 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. >> >> > On Aug 22, 6:57 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: >> > > Quoting BallaBall <ba...-xpohCx3Vo1WakBO8gow8eQ@public.gmane.org>: >> >> > > > 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. > > >