I’m building a Rails 3 app where users can select one of a number of templates and build a little website. I’m trying to initialize all of the available templates when the application starts. That’s essentially a file that let’s me define them all, then calls Template.add(template) for each one of them, which in turn stores them in @@templates and I can access them via Template.find(name).
The problem is that in development mode, the initializer I have making the calls to Template.add are getting loaded on the first request, then wiped out on reload!. I’ve read about config.to_prepare, but it isn’t working for me, likely because I’m requiring the templates like this:
# template config at app/templates/template_name/template_name.rb
path = File.expand_path(Rails.root + 'app/templates')
Dir[File.join(path, '/*')].each do |template|
name = File.basename(template)
require File.join(path, name, "#{name}.rb")
end
What can I do to either reload these files after reload! or keep them from getting trashed at all? Also, if you have a recommendation for a better way to handle this, I’m all ears. I’m still getting my footing with Rails, especially in regards to configuration stuff.
I’m temporarily avoiding this problem by loading the data in the class. Save me from this ugly, non-modular nastiness.
I think you ought to be able to do this with
config.to_prepareif you move your calls toTemplate.addout of thetemplate_name.rbfiles (I am assume that is where they are now), and into your loading code, just after therequire. For example, consider adding this class method to your Template class, and then maybe even calling it from the class itself when it first gets loaded:Then remove the
Template.add(name)code from thetemplate_name.rbfiles where they are registering themselves. (Again, I am assuming this is where that currently happens – I am not quite clear on this from your original post.) Or perhaps you just want to make sureTemplate.addis idempotent and silently discards duplicates.Lastly, add the following to
config/environments/development.rb:That will reload all your templates after the
Templateclass reloads.I haven’t tried this, but with the call to
Template.load_allI suggested at the end of theTemplateclass, you may not even need theconfig.to_preparepart.