Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Read custom config from settings.yml into a Rails 3 app

This is some sample code with no dependencies.

I've later extracted this into a separate library called choices.

# needs the "hashie" gem in Gemfile
require 'erb'
module Movies
class Application < Rails::Application
# read from "settings.yml" and optional "settings.local.yml"
settings ='../settings.yml', __FILE__))).result
mash =[Rails.env.to_s])
optional_file = File.expand_path('../settings.local.yml', __FILE__)
if File.exists? optional_file
optional_settings =
optional_values = YAML::load(optional_settings)[Rails.env.to_s]
mash.update optional_values if optional_values
mash.each do |key, value|
config.send("#{key}=", value)
# now we can use those settings happily
initializer "MongoMapper connect" do
MongoMapper.config = { Rails.env => config.mongodb }
config.middleware.use Twitter::Login,
:consumer_key => config.twitter.consumer_key, :secret => config.twitter.secret
config.facebook_client =, config.facebook.secret,
:user_fields => %w[link name email website timezone movies])

This comment has been minimized.

Copy link

millisami commented Dec 22, 2011

Sample yml files attached would be great!


This comment has been minimized.

Copy link
Owner Author

mislav commented Dec 25, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.