I'm using the configatron gem for a new Rails app that is backed up by ActiveRecord. Some of my configatron settings are set in a file and some are pulled from DB, as they will change from time to time, here are a couple开发者_如何学Python of lines from my configatron.rb
configatron.app.uptime.start = Time.now
configatron.email.signature = Setting.where(:keyname => "email_signature").first.value.to_s unless Setting.where(:keyname => "email_signature").first.nil?
Since this app sends multiple emails from multiple mailers - that is a good way to keep this global config in one place, plus it reduces db lookups for signature. If for some reason site admin decides to change it - they can do it through web admin interface that will update my settings table ( tied to Setting model).
This is all jolly & good, however what is the best place to store configatron.rb? Right now it's sitting in my initializers folder. Which means it will load once on application startup - which is good, however if one of the settings changes - site admin decides to tweak email signature to mention a new promotional website - in order for the change to take effect - I would need to restart app ( running passenger - so it trivial to do touch tmp/restart.txt
from code). However that means other configatron settings that I don't wont to reset ( such as my uptime start timestamp) will be reset as well.
So what is a better place to move my configatron.rb and load from so that it would allow for loading once on startup and then changing some configs without and app restart?
Thanks.
i think that putting it in an initializer is the right place to store it.
if you want to update the configuration without restarting the application, you would normally setup a "watchdog". some process that pulls the database regulary to check for configuration updates. you could also implement some kind of callback that pushes changed configurations to your app.
when i put configatron to initializer, Rails environment variables load before loading configatron settings. As effect, middleware loaded in Rails configuration (config/environments/production.rb) was unable to use configatron variables.
so i ended up loading it as first thing in environment.rb instead
精彩评论