Krishna
2009-Aug-10 13:08 UTC
ruby include in the plugin ''init'' works only in console mode
Hi, I have a plugin. in which there is a module. In the init of the plugin I have a statement like #ActiveRecord::Base.send (:include, Module::Plugin) Thing.send (:include, Module::Plugin) #Thing is a model That however does not work.. and while debugging I find that any call inside the Thing instance methods (rails development mode) returns a method not found..However the same works when I use the script/ console.. Is there some rails quirk here? The include inside the ActiveRecord::Base works but I dont want to include the module all over..
Ilan Berci
2009-Aug-10 13:17 UTC
Re: ruby include in the plugin ''init'' works only in console mode
Krishna wrote:> That however does not work.. and while debugging I find that any call > inside the Thing instance methods (rails development mode) returns a > method not found..However the same works when I use the script/ > console.. > > Is there some rails quirk here? The include inside the > ActiveRecord::Base works but I dont want to include the module all > over..Take a look at rails initializers.. your Thing model is not loaded at the time your plugins are loaded.. a good intro is: http://ryandaigle.com/articles/2007/2/23/what-s-new-in-edge-rails-stop-littering-your-evnrionment-rb-with-custom-initializations -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Aug-10 13:38 UTC
Re: ruby include in the plugin ''init'' works only in console mode
On Aug 10, 2:08 pm, Krishna <krishna.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That however does not work.. and while debugging I find that any call > inside the Thing instance methods (rails development mode) returns a > method not found..However the same works when I use the script/ > console.. > > Is there some rails quirk here? The include inside the > ActiveRecord::Base works but I dont want to include the module all > over..In development, after requests your model gets reloaded, and the reloaded copy never has include called on it. One way of circumventing things is for the model to request all this itself, with the fairly common pattern class Foo acts_as_your_plugin end Fred
James Adam
2009-Aug-10 14:29 UTC
Re: ruby include in the plugin ''init'' works only in console mode
On Aug 10, 2:38 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 10, 2:08 pm, Krishna <krishna.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > That however does not work.. and while debugging I find that any call > > inside the Thing instance methods (rails development mode) returns a > > method not found..However the same works when I use the script/ > > console.. > > > Is there some rails quirk here? The include inside the > > ActiveRecord::Base works but I dont want to include the module all > > over.. > > In development, after requests your model gets reloaded, and the > reloaded copy never has include called on it. One way of circumventing > things is for the model to request all this itself, with the fairly > common pattern > > class Foo > acts_as_your_plugin > endAnother way is to wrap your init.rb code in a to_prepare block config.to_prepare do Thing.send(:include, Module::Plugin) end This will ensure that this code is run before each request in development mode, and before the first request in production mode. - James
Krishna
2009-Aug-11 10:53 UTC
Re: ruby include in the plugin ''init'' works only in console mode
Thanks..but where do I add the config.to_prepare code? Or should I just wrap the code in the plugin/init.rb within the config.to_prepare? If yes,does it delay this till the Thing module is loaded or does it forcibly load the Thing module? On Aug 10, 7:29 pm, James Adam <ja...-W7EZ1sq+8wJWk0Htik3J/w@public.gmane.org> wrote:> On Aug 10, 2:38 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On Aug 10, 2:08 pm, Krishna <krishna.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > That however does not work.. and while debugging I find that any call > > > inside the Thing instance methods (rails development mode) returns a > > > method not found..However the same works when I use the script/ > > > console.. > > > > Is there some rails quirk here? The include inside the > > > ActiveRecord::Base works but I dont want to include the module all > > > over.. > > > In development, after requests your model gets reloaded, and the > > reloaded copy never has include called on it. One way of circumventing > > things is for the model to request all this itself, with the fairly > > common pattern > > > class Foo > > acts_as_your_plugin > > end > > Another way is to wrap your init.rb code in a to_prepare block > > config.to_prepare do > Thing.send(:include, Module::Plugin) > end > > This will ensure that this code is run before each request in > development mode, and before the first request in production mode. > > - James