måndag 29 juni 2009

Prettyfied niftyfied app config

In case you did'nt know already, Ryan Bates (of RailsCasts fame) has released a great collection of generators called
nifty-generators. Among a few other generators, there's one for generating an app config file for storing site wide configuration
options which you access like so:

APP_CONFIG[:config_option]

It's great, but not very pretty, I would much rather prefer something like:

AppConfig.config_option

Not that big of a difference but it fells a little bit nicer though, so here's how you go about accomplishing that goal.

Change the contents of the file config/initializers/load_app_config.rb from this:

raw_config = File.read(RAILS_ROOT + "/config/app_config.yml")
APP_CONFIG = YAML.load(raw_config)[RAILS_ENV].symbolize_keys

to this:

require 'ostruct'
require 'yaml'

raw_config = File.read(RAILS_ROOT+"/config/app_config.yml")
config = OpenStruct.new(YAML.load(raw_config))
::AppConfig = OpenStruct.new(config.send(RAILS_ENV))

Inga kommentarer:

Skicka en kommentar