-
-
Save millisami/1509846 to your computer and use it in GitHub Desktop.
Read custom config from settings.yml into a Rails 3 app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment