Skip to content

Instantly share code, notes, and snippets.

@benaskins
Created November 14, 2011 05:14
Show Gist options
  • Save benaskins/1363296 to your computer and use it in GitHub Desktop.
Save benaskins/1363296 to your computer and use it in GitHub Desktop.
# Use https://github.com/binarylogic/settingslogic to load application configuration from config/settings.yml.
# If the file isn't present we assume we're in the production environment and attempt to load the configuration
# from environment variables.
#
# Settings can only be one deep, and the convention is that the uppercase representation of the setting
# key will be the env variable name.
#
# e.g. The following yaml:
#
# twitter_username: compliance_hound
#
# is equivalent to:
#
# ENV['TWITTER_USERNAME'] = compliance_hound
#
# and both will result in:
#
# Settings.new.twitter_username # => 'compliance_hound'
#
# One caveat is that there can be no nesting of keys in config/settings.yml unless we establish a convention
# for deriving environment variable names from nested keys.
class Settings < Settingslogic
source Rails.root + 'config/settings.yml'
# Keys added to config/settings.yml should also be added here for Heroku's sake
Keys = %w(secret_token)
def initialize
if self.class.source.exist?
super
else
load_from_environment
end
end
def key_to_env_hash
Keys.inject({}) do |h, k|
h[k] = ENV[k.upcase]
h
end
end
def load_from_environment
self.replace key_to_env_hash
create_accessors!
end
def configure_heroku
key_value_pairs = Keys.map { |k| "#{k.upcase}=#{send(k)}" }.join(" ")
heroku("config:add #{key_value_pairs}")
end
# The heroku_app key must be set in config/settings.yml in order for this to operate on the correct app
def heroku(command)
`heroku #{command} --app #{heroku_app}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment