Hi all, This is a problem I''ve approached so many times and always worked around, that now I want to solve it once and for all. Say I have something like this: --- class X < ActiveRecord::Base acts_as_wacky end module Wackinator class ControlAllWackos @@wackos = [] def self.kill_wackos @@wackos.each(&:kill) end end def acts_as_wacky ControlAllWackos.wackos << self # do stuff end end --- I know that code won''t actually run, but hopefully it serves to illustrate my point. Which is that since Rails loads models with load_missing_constant, you can''t count on X having been defined, and therefore you can''t count on ControlAllWackos.wackos being accurate. Ok, so there''s a couple possible solutions: - load all your models right away with Dir.glob(File.join(RAILS_ROOT, ''app'', ''models'', ''*.rb'')).each{|f|require f} this is problematic with cache_classes in development mode, because cache_classes does some voodoo around unloading classes, and you end up with lots of errors. - statically define @@wackos: module Wackinator class ControlAllWackos @@wackos = %w(X Y Z) def self.kill_wackos @@wackos.map(&:classify).each(&:kill) end end def acts_as_wacky # do stuff end end which is difficult to maintain, and ugly too. This is kind of a chicken-and-egg problem, and I don''t anticipate finding a better solution than the static definition. But I thought some of ye ruby wizzards might know some arcana that might be of use here. Thanks! -Ian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I. E. Smith-Heisters wrote:> > --- > class X < ActiveRecord::Base > acts_as_wacky > end > > module Wackinator > class ControlAllWackos > @@wackos = [] > def self.kill_wackos > @@wackos.each(&:kill) > end > end > > def acts_as_wacky > ControlAllWackos.wackos << self > # do stuff > end > end > --- >require ''wackinator'' class X < ActiveRecord::Base hth ilan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3/7/08, Ilan Berci <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I. E. Smith-Heisters wrote: > > > > > --- > > class X < ActiveRecord::Base > > acts_as_wacky > > end > > > > module Wackinator > > class ControlAllWackos > > @@wackos = [] > > def self.kill_wackos > > @@wackos.each(&:kill) > > end > > end > > > > def acts_as_wacky > > ControlAllWackos.wackos << self > > # do stuff > > end > > end > > --- > > > > > require ''wackinator'' > class X < ActiveRecord::Base > > hth > > ilanBut that code won''t be evaluated until the model X is used somewhere, which is totally indeterminate. So if you boot your app and right away call Wackinator::ControlAllWackos.kill_wackos, chances are X *hasn''t* been loaded and @@wackos will be empty. Or am I missing something? Thanks, Ian> -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What about using the Dir.glob trick only through initializers for production.rb? On Mar 7, 7:19 pm, "Ian Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote:> On 3/7/08, Ilan Berci <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > > I. E. Smith-Heisters wrote: > > > > --- > > > class X < ActiveRecord::Base > > > acts_as_wacky > > > end > > > > module Wackinator > > > class ControlAllWackos > > > @@wackos = [] > > > def self.kill_wackos > > > @@wackos.each(&:kill) > > > end > > > end > > > > def acts_as_wacky > > > ControlAllWackos.wackos << self > > > # do stuff > > > end > > > end > > > --- > > > require ''wackinator'' > > class X < ActiveRecord::Base > > > hth > > > ilan > > But that code won''t be evaluated until the model X is used somewhere, > which is totally indeterminate. So if you boot your app and right away > call Wackinator::ControlAllWackos.kill_wackos, chances are X *hasn''t* > been loaded and @@wackos will be empty. Or am I missing something? > > Thanks, > Ian > > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hm, yes... but then you can''t test your code. Given that choice, I''d rather have a hard-to-maintain static list. Perhaps if I look further into *why* things don''t work in development I can find a way of fixing it. But that still wouldn''t address the possibility of there being a model defined somewhere else than app/models, which I guess is a sufficiently rare case to ignore. On 3/8/08, AndyV <arv7-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote:> > What about using the Dir.glob trick only through initializers for > production.rb? > > On Mar 7, 7:19 pm, "Ian Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote: > > > On 3/7/08, Ilan Berci <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > > > > > > > > I. E. Smith-Heisters wrote: > > > > > > --- > > > > class X < ActiveRecord::Base > > > > acts_as_wacky > > > > end > > > > > > module Wackinator > > > > class ControlAllWackos > > > > @@wackos = [] > > > > def self.kill_wackos > > > > @@wackos.each(&:kill) > > > > end > > > > end > > > > > > def acts_as_wacky > > > > ControlAllWackos.wackos << self > > > > # do stuff > > > > end > > > > end > > > > --- > > > > > require ''wackinator'' > > > class X < ActiveRecord::Base > > > > > hth > > > > > ilan > > > > But that code won''t be evaluated until the model X is used somewhere, > > which is totally indeterminate. So if you boot your app and right away > > call Wackinator::ControlAllWackos.kill_wackos, chances are X *hasn''t* > > been loaded and @@wackos will be empty. Or am I missing something? > > > > Thanks, > > Ian > > > > > -- > > > > Posted viahttp://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok, looking into cache_classes led me to this solution, which seems to work: in config/environment.rb: silence_warnings do Dir.glob(File.join(RAILS_ROOT, ''app'', ''models'', ''*.rb'')).each{|m|require_or_load m} end elsewhere: module Wackinator class ControlAllWackos @@wackos = [] def self.kill_wackos @@wackos.each(&:kill) end end def acts_as_wacky ControlAllWackos.wackos << self # do stuff end end The important part is that you have to use require_or_load instead of require so that eagerly loading all the models works in development mode. From there you can do whatever you want, in my case I''m registering all my wackos so that they''re available in ControlAllWackos.wackos. Work for me at the moment... -Ian On Sat, Mar 8, 2008 at 4:22 PM, Ian Smith-Heisters <i@0x09.com> wrote:> Hm, yes... but then you can''t test your code. Given that choice, I''d > rather have a hard-to-maintain static list. Perhaps if I look further > into *why* things don''t work in development I can find a way of fixing > it. But that still wouldn''t address the possibility of there being a > model defined somewhere else than app/models, which I guess is a > sufficiently rare case to ignore. > > > > On 3/8/08, AndyV <arv7-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote: > > > > What about using the Dir.glob trick only through initializers for > > production.rb? > > > > On Mar 7, 7:19 pm, "Ian Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote: > > > > > On 3/7/08, Ilan Berci <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > > > > > > > > > > > > > > I. E. Smith-Heisters wrote: > > > > > > > > --- > > > > > class X < ActiveRecord::Base > > > > > acts_as_wacky > > > > > end > > > > > > > > module Wackinator > > > > > class ControlAllWackos > > > > > @@wackos = [] > > > > > def self.kill_wackos > > > > > @@wackos.each(&:kill) > > > > > end > > > > > end > > > > > > > > def acts_as_wacky > > > > > ControlAllWackos.wackos << self > > > > > # do stuff > > > > > end > > > > > end > > > > > --- > > > > > > > require ''wackinator'' > > > > class X < ActiveRecord::Base > > > > > > > hth > > > > > > > ilan > > > > > > But that code won''t be evaluated until the model X is used somewhere, > > > which is totally indeterminate. So if you boot your app and right away > > > call Wackinator::ControlAllWackos.kill_wackos, chances are X *hasn''t* > > > been loaded and @@wackos will be empty. Or am I missing something? > > > > > > Thanks, > > > Ian > > > > > > > -- > > > > > > Posted viahttp://www.ruby-forum.com/. > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Very interesting... thanks for the follow up. On Mar 10, 2:00 pm, "Ian Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote:> Ok, looking into cache_classes led me to this solution, which seems to work: > > in config/environment.rb: > > silence_warnings do > Dir.glob(File.join(RAILS_ROOT, ''app'', ''models'', > ''*.rb'')).each{|m|require_or_load m} > end > > elsewhere: > > module Wackinator > class ControlAllWackos > @@wackos = [] > def self.kill_wackos > @@wackos.each(&:kill) > end > end > > def acts_as_wacky > ControlAllWackos.wackos << self > # do stuff > end > end > > The important part is that you have to use require_or_load instead of > require so that eagerly loading all the models works in development > mode. From there you can do whatever you want, in my case I''m > registering all my wackos so that they''re available in > ControlAllWackos.wackos. > > Work for me at the moment... > > -Ian > > On Sat, Mar 8, 2008 at 4:22 PM, Ian Smith-Heisters <i...-ZMJO0lPjCCA@public.gmane.org> wrote: > > Hm, yes... but then you can''t test your code. Given that choice, I''d > > rather have a hard-to-maintain static list. Perhaps if I look further > > into *why* things don''t work in development I can find a way of fixing > > it. But that still wouldn''t address the possibility of there being a > > model defined somewhere else than app/models, which I guess is a > > sufficiently rare case to ignore. > > > On 3/8/08, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote: > > > > What about using the Dir.glob trick only through initializers for > > > production.rb? > > > > On Mar 7, 7:19 pm, "Ian Smith-Heisters" <i...-ZMJO0lPjCCA@public.gmane.org> wrote: > > > > > On 3/7/08, Ilan Berci <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > I. E. Smith-Heisters wrote: > > > > > > > --- > > > > > > class X < ActiveRecord::Base > > > > > > acts_as_wacky > > > > > > end > > > > > > > module Wackinator > > > > > > class ControlAllWackos > > > > > > @@wackos = [] > > > > > > def self.kill_wackos > > > > > > @@wackos.each(&:kill) > > > > > > end > > > > > > end > > > > > > > def acts_as_wacky > > > > > > ControlAllWackos.wackos << self > > > > > > # do stuff > > > > > > end > > > > > > end > > > > > > --- > > > > > > require ''wackinator'' > > > > > class X < ActiveRecord::Base > > > > > > hth > > > > > > ilan > > > > > But that code won''t be evaluated until the model X is used somewhere, > > > > which is totally indeterminate. So if you boot your app and right away > > > > call Wackinator::ControlAllWackos.kill_wackos, chances are X *hasn''t* > > > > been loaded and @@wackos will be empty. Or am I missing something? > > > > > Thanks, > > > > Ian > > > > > > -- > > > > > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---