2018-06-01 10:22:47 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-07 09:43:09 +08:00
|
|
|
module SiteSettings; end
|
|
|
|
|
|
|
|
# A cache for providing default value based on site locale
|
|
|
|
class SiteSettings::DefaultsProvider
|
2018-06-01 10:22:47 +08:00
|
|
|
DEFAULT_LOCALE = 'en'
|
2017-09-12 19:36:17 +08:00
|
|
|
|
2017-08-07 09:43:09 +08:00
|
|
|
def initialize(site_setting)
|
|
|
|
@site_setting = site_setting
|
|
|
|
@defaults = {}
|
|
|
|
@defaults[DEFAULT_LOCALE.to_sym] = {}
|
|
|
|
end
|
|
|
|
|
2018-06-01 10:22:47 +08:00
|
|
|
def load_setting(name_arg, value, locale_defaults)
|
2017-08-07 09:43:09 +08:00
|
|
|
name = name_arg.to_sym
|
|
|
|
@defaults[DEFAULT_LOCALE.to_sym][name] = value
|
|
|
|
|
2018-06-01 10:22:47 +08:00
|
|
|
if (locale_defaults)
|
|
|
|
locale_defaults.each do |locale, v|
|
2017-08-07 09:43:09 +08:00
|
|
|
locale = locale.to_sym
|
|
|
|
@defaults[locale] ||= {}
|
|
|
|
@defaults[locale][name] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_all
|
2018-06-01 10:22:47 +08:00
|
|
|
@site_setting.provider.all
|
2017-08-07 09:43:09 +08:00
|
|
|
end
|
|
|
|
|
2018-06-01 10:22:47 +08:00
|
|
|
def all(locale = nil)
|
|
|
|
if locale
|
|
|
|
@defaults[DEFAULT_LOCALE.to_sym].merge(@defaults[locale.to_sym] || {})
|
|
|
|
else
|
|
|
|
@defaults[DEFAULT_LOCALE.to_sym].dup
|
|
|
|
end
|
2017-08-07 09:43:09 +08:00
|
|
|
end
|
|
|
|
|
2018-06-01 10:22:47 +08:00
|
|
|
def get(name, locale = DEFAULT_LOCALE)
|
|
|
|
@defaults.dig(locale.to_sym, name.to_sym) ||
|
2017-08-07 14:15:32 +08:00
|
|
|
@defaults.dig(DEFAULT_LOCALE.to_sym, name.to_sym)
|
2017-08-07 09:43:09 +08:00
|
|
|
end
|
2017-08-07 14:15:32 +08:00
|
|
|
alias [] get
|
2017-08-07 09:43:09 +08:00
|
|
|
|
|
|
|
# Used to override site settings in dev/test env
|
|
|
|
def set_regardless_of_locale(name, value)
|
|
|
|
name = name.to_sym
|
2018-06-01 10:22:47 +08:00
|
|
|
if name == :default_locale || @site_setting.has_setting?(name)
|
2017-08-07 09:43:09 +08:00
|
|
|
@defaults.each { |_, hash| hash.delete(name) }
|
|
|
|
@defaults[DEFAULT_LOCALE.to_sym][name] = value
|
|
|
|
value, type = @site_setting.type_supervisor.to_db_value(name, value)
|
2018-06-01 10:22:47 +08:00
|
|
|
@defaults[SiteSetting.default_locale.to_sym] ||= {}
|
|
|
|
@defaults[SiteSetting.default_locale.to_sym][name] = @site_setting.type_supervisor.to_rb_value(name, value, type)
|
2017-08-07 09:43:09 +08:00
|
|
|
else
|
|
|
|
raise ArgumentError.new("No setting named '#{name}' exists")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_setting?(name)
|
2018-06-01 10:22:47 +08:00
|
|
|
has_key?(name.to_sym) || has_key?("#{name.to_s}?".to_sym) || name.to_sym == :default_locale
|
2017-08-07 09:43:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-07 14:15:32 +08:00
|
|
|
def has_key?(name)
|
2018-06-01 10:22:47 +08:00
|
|
|
@defaults[DEFAULT_LOCALE.to_sym].key?(name)
|
2017-08-07 10:14:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_db
|
|
|
|
RailsMultisite::ConnectionManagement.current_db
|
2017-08-07 09:43:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|