Skip to content

Instantly share code, notes, and snippets.

Embed
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 = ERB.new(IO.read(File.expand_path('../settings.yml', __FILE__))).result
mash = Hashie::Mash.new(YAML::load(settings)[Rails.env.to_s])
optional_file = File.expand_path('../settings.local.yml', __FILE__)
if File.exists? optional_file
optional_settings = ERB.new(IO.read(optional_file)).result
optional_values = YAML::load(optional_settings)[Rails.env.to_s]
mash.update optional_values if optional_values
end
mash.each do |key, value|
config.send("#{key}=", value)
end
# now we can use those settings happily
initializer "MongoMapper connect" do
MongoMapper.config = { Rails.env => config.mongodb }
MongoMapper.connect(Rails.env)
end
config.middleware.use Twitter::Login,
:consumer_key => config.twitter.consumer_key, :secret => config.twitter.secret
config.facebook_client = Facebook::Client.new(config.facebook.app_id, config.facebook.secret,
:user_fields => %w[link name email website timezone movies])
end
end
@millisami

This comment has been minimized.

Copy link

commented Dec 22, 2011

Sample yml files attached would be great!

@mislav

This comment has been minimized.

Copy link
Owner Author

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.