Joe
2010-Jul-06 18:27 UTC
Class loading in development mode best practice / recommendation
In my project, I have a user model. The user model can be assigned chores. Chores have an associated class object (that go by the name ''Service''). In the admin interface for users, the administrator can assign these Chores to a user, and when they do this, they select the Service for the Chore from a dropdown. My problem is getting the service list. All service classes inherit from Services::Base (MyService < Services::Base) I want to provide the list of available services in that dropdown, but when in development mode, the classes are unloaded and reloaded with each request. Thus, using Object.subclasses_of( Services::Base ) will not reliably get me a list of available services, as the classes have probably been unloaded by the time the code is reached. Alternatively, I wanted to register available services via an initializer, like: add_service MyService, but this depends on storing values in a class, which also fails, for the same reason, as far as I can tell (because the classes are being unloaded after each request). Note, both of these methods work fine in test and production mode. If there is any other information I can provide, please let me know. Thanks all for the help. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jul-07 08:25 UTC
Re: Class loading in development mode best practice / recommendation
On Jul 6, 7:27 pm, Joe <loret...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In my project, I have a user model. The user model can be assigned > chores. Chores have an associated class object (that go by the name > ''Service''). In the admin interface for users, the administrator can > assign these Chores to a user, and when they do this, they select the > Service for the Chore from a dropdown. My problem is getting the > service list. > > All service classes inherit from Services::Base (MyService < > Services::Base) I want to provide the list of available services in > that dropdown, but when in development mode, the classes are unloaded > and reloaded with each request. Thus, using > Object.subclasses_of( Services::Base ) will not reliably get me a list > of available services, as the classes have probably been unloaded by > the time the code is reached. >I usually stick a bunch of require_dependency calls at the bottom of the file containing the base class that loads the various subclasses Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Joe
2010-Jul-07 14:52 UTC
Re: Class loading in development mode best practice / recommendation
On Jul 7, 3:25 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I usually stick a bunch of require_dependency calls at the bottom of > the file containing the base class that loads the various subclasses > > FredThanks for the suggestion, Fred. That makes sense, and I would solve the problem in this case. Long term, I''m thinking of moving the framework out of the project and into a gem. At that point, of course, it would be unrealistic to include the require_dependency lines. Is there some other place you can think of that I might put the code so that it''s called with each request, but not a part of the gem itself? Thanks, Joe -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jul-08 07:11 UTC
Re: Class loading in development mode best practice / recommendation
On Jul 7, 3:52 pm, Joe <loret...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for the suggestion, Fred. That makes sense, and I would solve > the problem in this case. > > Long term, I''m thinking of moving the framework out of the project and > into a gem. At that point, of course, it would be unrealistic to > include the require_dependency lines. Is there some other place you > can think of that I might put the code so that it''s called with each > request, but not a part of the gem itself? >If you set config.to_prepare to a block then it''s called before each request (in development mode). Also code from gems isn''t reloaded between requests Fred> Thanks, > Joe-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.