Forum: Ruby on Rails preload class at start up

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Vinh T. (Guest)
on 2007-01-13 05:26
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
Snow M. (Guest)
on 2007-01-13 06:36
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.
Vinh T. (Guest)
on 2007-01-14 03:09
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
Vinh T. (Guest)
on 2007-01-14 03:15
Oops...
It doesnt work, because it required a child class which inherited from
itself ...
it gave me a uninitilized constant Field error
Snow M. (Guest)
on 2007-01-14 03:33
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.
Vinh T. (Guest)
on 2007-01-14 13:47
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
This topic is locked and can not be replied to.