Skip to content
Easy external settings for your app
Find file
Latest commit 6b66b09 Nov 15, 2013 @mislav v0.4.0
Failed to load latest commit information.
lib Enable choices in Rails engines Nov 15, 2013
test raise a more appropriate IndexError for missing environment Jul 18, 2013
.gitignore the choice is yours Oct 23, 2010
Gemfile add rake to the bundle Jul 18, 2013
choices.gemspec v0.4.0 Nov 15, 2013

Choices for Rails

Easy-peasy external settings for your Rails app.

# Gemfile
gem 'choices'

In your app initializer block:

config.from_file 'settings.yml'

This will read configuration from "config/settings.yml" and, additionally, "settings.local.yml" if it exists. You should check the main file into version control, but not the ".local" file which is to be used for per-machine configuration: tweaks in development or private keys in production, for example.

# .gitignore

Configuration files can contain ERB; this is useful for reading in dynamic config such as from environment variables:

# settings.yml
defaults: &defaults
  secret_token: <%= ENV['COOKIE_SECRET'] %>
  heroku: <%= !!ENV['HEROKU_TYPE'] %>
    uri: <%= ENV['MONGOHQ_URL'] %>

  <<: *defaults

test: &testing
  <<: *defaults
  secret_token: <%= "banana" * 5 %>
    database: myapp_test

  <<: *testing

The ".local" file can contain overrides for your development environment:

# settings.local.yml
    database: myapp_dev

Finally, the config keys can be read in your app as such:

Rails.configuration.heroku              #=> false
Rails.configuration.mongodb.database    #=> "myapp_dev"
Something went wrong with that request. Please try again.