2013-11-07 04:47:26 +08:00
|
|
|
module SiteSettings; end
|
|
|
|
|
|
|
|
class SiteSettings::YamlLoader
|
|
|
|
|
|
|
|
def initialize(file)
|
|
|
|
@file = file
|
|
|
|
end
|
|
|
|
|
2014-10-03 13:53:01 +08:00
|
|
|
def env_val(value)
|
|
|
|
if value.is_a?(Hash)
|
|
|
|
value.has_key?(Rails.env) ? value[Rails.env] : value['default']
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-07 04:47:26 +08:00
|
|
|
def load
|
|
|
|
yaml = YAML.load_file(@file)
|
2015-04-18 19:53:53 +08:00
|
|
|
yaml.each_key do |category|
|
2013-11-07 04:47:26 +08:00
|
|
|
yaml[category].each do |setting_name, hash|
|
|
|
|
if hash.is_a?(Hash)
|
|
|
|
# Get default value for the site setting:
|
2014-10-03 13:53:01 +08:00
|
|
|
value = env_val(hash.delete('default'))
|
2013-11-07 04:47:26 +08:00
|
|
|
|
2014-10-03 13:53:01 +08:00
|
|
|
if hash.key?('hidden')
|
|
|
|
hash['hidden'] = env_val(hash.delete('hidden'))
|
2013-11-07 04:47:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
yield category, setting_name, value, hash.symbolize_keys!
|
|
|
|
else
|
|
|
|
# Simplest case. site_setting_name: 'default value'
|
|
|
|
yield category, setting_name, hash, {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-03 13:53:01 +08:00
|
|
|
end
|