Hi All, Is there a way to tell Rail to load my class at start up. my problem is I have a Field class which has children classes : DateField, MoneyField ...etc (and might be more in the future) I also have Template class which can have many Fields I want to display a list of available Fields type when I create new Template. What I was trying to do, and doesnt work :(, is using call back Class$inherited on the Field Class someting like @@field_type_list = [] def Field.inherited(sub) @@field_type_list.push({''name''=>sub.display_name,''type''=>sub}) end def Field.display_name raise NotImplementedError("#{self.class.name}#m1() is not implemented.") end def Field.field_type_list return @@field_type_list end -------------- and from my helper I call Field.field_type_list to get all the subclasses However Rail only load a class when it needed, so it left my field_type_list empty so I need a way to load my sub classes at start up make sure they''re available to be used. Can anyone help please, or suggest better design for this Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
You could try putting the following lines at the *bottom* of field.rb: require ''date_field'' require ''money_field'' ... etc. I''m not sure whether this will work properly with the Rails "reloading" feature, but it''s worth a try. -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks But that way, I''ll need to know what Field Type I''m dealing with. I want to be able to let others define their own Field Type later on. pretty much like a plugin.. I wonder if there''s a way to do that, but in the meantime , I think I''m gonna go with this Thank you -- 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 -~----------~----~----~----~------~----~------~--~---
Oops... It doesnt work, because it required a child class which inherited from itself ... it gave me a uninitilized constant Field error -- 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 -~----------~----~----~----~------~----~------~--~---
Did you put the "require" lines at the bottom of the file like I suggested, or at the top? Obviously you have to require the derived classes AFTER you''ve defined the base class. If you already did this then Ruby might be choking on the circular dependencies between the files, but I just did a quick test and it seems to work ok for me. Another option would be to put the "requires" in environment.rb (with instructions for other users of your plugin to do the same). Or you could put your requires in field.rb but instruct your users to put additional requires in a location of their choice, like environment.rb. Or you could even maintain a list of which field types are available in the database. -- 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 -~----------~----~----~----~------~----~------~--~---
Snow Man wrote:> Did you put the "require" lines at the bottom of the file like I > suggested, or at the top? Obviously you have to require the derived > classes AFTER you''ve defined the base class. If you already did this > then Ruby might be choking on the circular dependencies between the > files, but I just did a quick test and it seems to work ok for me. > > Another option would be to put the "requires" in environment.rb (with > instructions for other users of your plugin to do the same). Or you > could put your requires in field.rb but instruct your users to put > additional requires in a location of their choice, like environment.rb. > > Or you could even maintain a list of which field types are available in > the database.yeah, I put it at the bottom, not really sure what happened. I ended up put in in application.rb (controller) and its fine now. still think that I would need to refactor this later :D Thanks for your help -- 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 -~----------~----~----~----~------~----~------~--~---