I found this great RailsCast about making a global configuration. And
further offered through the Nifty Generators
(script/generate nifty_config
) However I wanted to further customize it by
allowing global configurations regardless of the environment.
For example this is the original idea: # config/config.yml development: perform_authentication: false
test:
perform_authentication: false
production:
perform_authentication: true
username: admin
password: secret
This would require that there be a lot of repetition. What I wanted was a way to write a global variable once and still keep differences for the environments. I eventually thought of this solution to manipulate the hash after the fact (avoiding the warnings that a constant variable being changed).
The sample file config.yml
shows the new format while
load_config.rb
is the code to make it all happen.
This will produce the following config when RAILS_ENV is development: APP_CONFIG[:global_var1] #=> "foo" APP_CONFIG[:global_var2] #=> "bar" APP_CONFIG[:env_var] #=> "foobar"
In a rails application you can put load_config.rb
into
config/initializers
directory.
typo error:
bang at the end