Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mislav
Created August 24, 2010 17:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mislav/547954 to your computer and use it in GitHub Desktop.
Save mislav/547954 to your computer and use it in GitHub Desktop.
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
@mislav
Copy link
Author

mislav commented Dec 25, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment