Created
December 11, 2011 18:51
-
-
Save igor-alexandrov/1462080 to your computer and use it in GitHub Desktop.
Multiple configuration files in SettingsLogic
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
class Settings < Settingslogic | |
source "#{Rails.root}/config/application.yml" | |
if Rails.env == 'development' | |
namespace 'development-' + Socket.gethostname.gsub(/\W/, '-').gsub(/[-]{2,}/, '-') | |
else | |
namespace Rails.env | |
end | |
# load local settings | |
begin | |
hash = YAML.load(ERB.new(File.read("#{Rails.root}/config/application_local.yml")).result)[Rails.env] | |
instance.deep_merge!(hash) | |
rescue => e | |
end | |
end |
Additionally, I think you should also remember to define the reload!
method so we can ensure local settings will be keep loaded though user have refresh settings from application.yml
. The version after refactor will be like:
class Settings < Settingslogic
def self.load_local_settings
if File.exist?("#{Rails.root}/config/application_local.yml")
puts '===> Local application configuration file loaded.'
instance.deep_merge!(Settings.new("#{Rails.root}/config/application_local.yml"))
else
puts '===> Local application configuration file no found.'
end
end
load_local_settings # load when first time load Settings
def self.reload!
super
load_local_settings # load local settings each time refreshing settings
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would not it be simpler to just write :
Thanks for your answer.