Skip to content

Instantly share code, notes, and snippets.

@bmc
Created October 28, 2011 01:35
Show Gist options
  • Save bmc/1321419 to your computer and use it in GitHub Desktop.
Save bmc/1321419 to your computer and use it in GitHub Desktop.
Rails 3 local configuration
# Load a config-local.yml configuration file, which has special
# configuration directives per environment.
require 'rails'
CONFIG_LOCAL = 'config-local.yml'
OPTIONS = {
:listen_port => 3000,
# Default email addresses.
:email => {
# Default sender for outgoing emails.
:default_sender => '[email protected]',
# Customer service email address (where people can send email)
:customer_service => '[email protected]',
# If non-nil, this email address overrides the user-supplied
# address for registration emails. This is largely for
# pre-deployment: It allows us to intercept any attempt to
# register an account.
:registration_intercept => nil
},
# SMTP server settings, etc.
:smtp_settings => {
:server => 'smtp.example.com',
:port => 587,
:domain => 'example.com',
:auth_type => :plain,
:auth_user => '[email protected]',
:auth_password => 'xxxxxxxx',
:use_tls => true
},
# Public, private keys for reCaptcha.
:recaptcha => {
:public_key => "NotARealKey-kAWBdoYHBJfB1_sadf893jddk47v",
:private_key => "NotARealKey-AACXOHDuMwq6QTY_L-9723lsuiTyT"
}
}
# Recursively map a hash's keys to symbols. Used to map the parsed YAML
# into a symbol-keyed hash, like the above.
def fix_config_hash(h)
h2 = {}
h.each do |k, v|
v = fix_config_hash(v) if v.class == Hash
h2[k.to_sym] = v
end
h2
end
local_config_path = File.expand_path(CONFIG_LOCAL, File.dirname(__FILE__))
if File.exists? local_config_path
require 'erb'
# Open the file as an ERB template.
template = ERB.new File.open(local_config_path).read
# Load the template as a YAML document.
cfg = YAML::load template.result(binding)
_local_config = fix_config_hash(cfg[Rails.env] || {})
else
_local_config = {}
end
# Save the configuration in a global, accessible from the application.
LocalConfig = OPTIONS.merge _local_config
# adjust port. See
# http://stackoverflow.com/questions/3842818#6539193
require 'rails/commands/server'
module Rails
class Server
alias :default_options_alias :default_options
def default_options
default_options_alias.merge!(:Port => LocalConfig[:listen_port])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment