Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@igor-alexandrov
Created December 11, 2011 18:51
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igor-alexandrov/1462080 to your computer and use it in GitHub Desktop.
Save igor-alexandrov/1462080 to your computer and use it in GitHub Desktop.
Multiple configuration files in SettingsLogic
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
@zitooon
Copy link

zitooon commented Mar 30, 2012

Would not it be simpler to just write :

class Settings < Settingslogic
  source "#{Rails.root}/config/application.yml"
  namespace Rails.env

  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

Thanks for your answer.

@Martin91
Copy link

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