2012-08-24 19:52:38 +02:00
|
|
|
class FoodsoftConfig
|
|
|
|
mattr_accessor :scope, :config
|
2014-03-28 12:52:11 +01:00
|
|
|
APP_CONFIG_FILE = ENV['FOODSOFT_APP_CONFIG'] || 'config/app_config.yml'
|
|
|
|
# Rails.logger isn't ready yet - and we don't want to litter rspec invocation with this msg
|
|
|
|
puts "-> Loading app configuration from #{APP_CONFIG_FILE}" unless defined? RSpec
|
|
|
|
APP_CONFIG = YAML.load(File.read(File.expand_path(APP_CONFIG_FILE, Rails.root)))
|
2012-08-24 19:52:38 +02:00
|
|
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
def init
|
|
|
|
# Load initial config from development or production
|
|
|
|
set_config Rails.env
|
|
|
|
# Overwrite scope to have a better namescope than 'production'
|
|
|
|
self.scope = config[:default_scope] or raise "No default_scope is set"
|
2013-09-20 22:40:13 +02:00
|
|
|
# Set defaults for backward-compatibility
|
|
|
|
set_missing
|
2012-08-24 19:52:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set config and database connection for specific foodcoop
|
|
|
|
# Only needed in multi coop mode
|
|
|
|
def select_foodcoop(foodcoop)
|
|
|
|
set_config foodcoop
|
|
|
|
setup_database
|
|
|
|
end
|
|
|
|
|
|
|
|
# Provides a nice accessor for config values
|
|
|
|
# FoodsoftConfig[:name] # => 'FC Test'
|
|
|
|
def [](key)
|
|
|
|
config[key]
|
|
|
|
end
|
|
|
|
|
2012-08-27 08:24:25 +02:00
|
|
|
# Loop through each foodcoop and executes the given block after setup config and database
|
|
|
|
def each_coop
|
2014-05-30 12:13:09 +02:00
|
|
|
if config[:multi_coop_install]
|
|
|
|
APP_CONFIG.keys.reject { |coop| coop =~ /^(default|development|test|production)$/ }.each do |coop|
|
|
|
|
select_foodcoop coop
|
|
|
|
yield coop
|
|
|
|
end
|
|
|
|
else
|
|
|
|
yield config[:default_scope]
|
2012-08-27 08:24:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-24 19:52:38 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_config(foodcoop)
|
|
|
|
raise "No config for this environment (#{foodcoop}) available!" if APP_CONFIG[foodcoop].nil?
|
|
|
|
self.config = APP_CONFIG[foodcoop].symbolize_keys
|
|
|
|
self.scope = foodcoop
|
2014-04-25 17:34:35 +02:00
|
|
|
set_missing
|
2012-08-24 19:52:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_database
|
|
|
|
database_config = ActiveRecord::Base.configurations[Rails.env]
|
2014-04-05 10:41:45 +02:00
|
|
|
database_config = database_config.merge(config[:database]) if config[:database].present?
|
2012-08-24 19:52:38 +02:00
|
|
|
ActiveRecord::Base.establish_connection(database_config)
|
|
|
|
end
|
|
|
|
|
2013-09-20 22:40:13 +02:00
|
|
|
# When new options are introduced, put backward-compatible defaults here, so that
|
|
|
|
# configuration files that haven't been updated, still work as they did.
|
|
|
|
def set_missing
|
|
|
|
config.replace({
|
2014-02-25 10:53:28 +01:00
|
|
|
use_nick: true,
|
2014-06-06 17:50:20 +02:00
|
|
|
use_apple_points: true,
|
|
|
|
foodsoft_url: 'https://github.com/foodcoops/foodsoft'
|
2013-09-20 22:40:13 +02:00
|
|
|
}.merge(config))
|
|
|
|
end
|
|
|
|
|
2014-04-04 12:25:57 +02:00
|
|
|
# reload original configuration file, e.g. in between tests
|
|
|
|
def reload!(filename = APP_CONFIG_FILE)
|
|
|
|
APP_CONFIG.clear.merge! YAML.load(File.read(File.expand_path(filename, Rails.root)))
|
|
|
|
init
|
|
|
|
end
|
|
|
|
|
2012-08-24 19:52:38 +02:00
|
|
|
end
|
2013-09-20 22:40:13 +02:00
|
|
|
end
|