Last active
August 25, 2023 06:55
-
-
Save mustmodify/42962352ddc7d33991656470ccc42505 to your computer and use it in GitHub Desktop.
Refined Rails configuration used for a blog post
This file contains 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
application: | |
auto_call_queue_size: 18 | |
customer_service_number: '(202) 456-1111' | |
customer_service_email: "service@domain.com" | |
deliver_emails: yes | |
enable_employment_application: yes | |
enable_survey_request_emails: yes | |
enable_zone_maps: yes | |
sandbox_environment: no | |
yearweek_strategy: 1 | |
s3_bucket: <%= ENV['S3_BUCKET'] || 'test-bucket' %> | |
development: | |
domain: 'dev.domain.com' | |
send_emails: yes | |
track_ga: no | |
email_recipient_override: | |
- jw@domain.com | |
- otherdev@domain.com | |
test: | |
domain: 'localhost:3000' | |
send_emails: no | |
track_ga: no | |
demo: | |
domain: 'demo.domain.com' | |
sandbox_environment: no | |
send_emails: yes | |
email_recipient_override: | |
- demouser@domain.com | |
- salesperson@domain.com | |
production: | |
domain: 'www.domain.com' | |
backups_s3_bucket: my_cloud_backups | |
require_https: true | |
send_emails: yes |
This file contains 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
development: | |
mapquest_api_key: 2340971lkjnasdf98702o3i4j | |
twilio: | |
sid: 'alksdjrtkl;jn4wr' | |
token: '20j;alksdjrfp0[9ua354l' | |
sip_number: '+12345678901' | |
email_config: | |
username: 'developer@gmail.com' | |
password: <%= ENV['password'] %> |
This file contains 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
# config/initializers/config.rb | |
# assuming config/application.rb defines module MySoftware... | |
class MySoftware::Configuration | |
def initialize | |
@config=HashWithIndifferentAccess.new | |
end | |
def add( env, hash ) | |
if env.to_s == 'application' || env.to_s == Rails.env | |
hash.each do |key, value| | |
if Hash === value || HashWithIndifferentAccess === value | |
hash[key] = MyApplication::Configuration.new.add(env, value) | |
end | |
end | |
@config = @config.deep_merge( hash ) | |
end | |
self | |
end | |
def load( filepath ) | |
if File.exist?( filepath ) | |
content = YAML.load(ERB.new(IO.read(filepath)).result) | |
content.each do |env, data| | |
self.add( env, data ) | |
end | |
end | |
end | |
def load!( filepath ) | |
if File.exist?( filepath ) | |
self.load(filepath) | |
else | |
raise "File #{filepath} does not exist. Please create it to continue." | |
end | |
end | |
def method_missing( name ) | |
if( @config.has_key?(name) ) | |
@config[name] | |
else | |
super | |
end | |
end | |
end | |
MySoftware.config = MySoftware::Configuration.new | |
MySoftware.config.load!( "#{Rails.root}/config/config.yml" ) | |
MySoftware.config.load!( "#{Rails.root}/config/config.local.yml" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment