Custom Configuration Info in Rails
July 25th, 2007
I think most applications deal with some kind of configuration parameters like admin emails, directory paths or whatever. Sometimes having a bunch of constants in environment.rb is fine, but when you have several developers working on a project and each of them may have their own values and can add new parameters you need a clean solution to handle that.
The common practise is to have an “example” config file under version control (like database.example.yml in mephisto) which is used by all developers to create their local files with custom values. That works OK until someone adds a new parameter to the example file and some careless developer (me) doesn’t notice it in the svn logs, forgets to update his local file and gets a broken application. So I thought that I need a file under version control with default values which is actually used by the application and an ability to have another file with local values that will override the default.
I’ve googled for a ready-made solution and found this one which I liked, but as I’ve just said I also wanted to override the default settings in a local file and also wanted yml file to be parsed with ERB so that I could use Ruby in it (for example if I needed to use RAILS_ROOT to define some path). And so I’ve ended up with my first rails plugin :)
The plugin allows you to have a config.yml file with a “common” section that contains all configuration parameters with their default values and a section for each Rails environment (like development, test, production or your custom one) where you can override the values from the common section. Just put this file under version control, update it with new parameters and all developers will have it with the next update. If someone needs his own specific values, he can simply create a config.local.yml file and override any value there, again having a “common” section and a section for each environment. Nothing is mandatory (files, sections) – you just have what you really need.
An example of a config file:
common:
admin_email: admin@domain.com
xml_rpc_url: http://domain.com:8000/
media_path: <%= RAILS_ROOT %>/tmp/media
development:
xml_rpc_url: http://localhost:8000/
test:
xml_rpc_url: http://localhost:8008/
Then, in the application you can use the config parameters like this:
Conf.xml_rpc_url
So it means that you’ve got a Conf object which holds all the configuration parameters defined. It doesn’t allow to change the values in the application code, BTW (so that a typo in a if statement won’t cause weird errors :)).
You can give it a try if you like:
script/plugin install http://tools.assembla.com/svn/rails_plugins/app_config
Comments and suggestions are welcome of course ;)
Update: I’ve renamed the configuration variable from Config to Conf.
August 13th, 2007 at 09:06 AM Nice plugin! :)
August 23rd, 2007 at 07:12 PM It meets my needs! Thanks a lot.
August 23rd, 2007 at 07:16 PM The var name 'Config' conflicts with active_scafflod plugin. So I had to rename it something else like SiteConfig
June 8th, 2008 at 01:20 PM Buy Viagra Online
Buy Cialis Online
Buy Propecia
Buy Zithromax
Buy Acomplia