Application Configuration

While working on the Testimonial application, I needed a way to store application specific configuration details — the parts that are specific to a deployment: site name, url, etc. A quick search turned up the ApplicationConfiguration plugin which does exactly that, using YAML. It’s based on the technique described by Dmytro Shtefluk and uses OpenStruct to parse the config file and supports RAILS_ENV specific settings.Borrowing Dmytryo’s example:

common:
  support_email: admin@myhost.com
  root_url: myhost.com
  photos_max_number: 6
	
production:
  email_exceptions: true
	
development:
  root_url: localhost:3000
  photos_max_number: 10

In your application, it’s pretty simple to get at these values. Each value specified is stored as a member in the ::AppConfig structure:

AppConfig.photos_max_number
AppConfig.email_exceptions

I did run into one problem using the plugin; it uses config as the name of the structure, which conflicts with Rails, or some other gem that I’m using. I edited the plugin source and changed the name of the struct to AppConfig and now I’m happily keeping my configuration data DRY.

Comments are closed.