2018-05-31 22:22:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
module SiteSettings; end
|
|
|
|
|
|
|
|
# A cache for providing default value based on site locale
|
|
|
|
class SiteSettings::DefaultsProvider
|
2021-01-20 15:32:22 -05:00
|
|
|
DEFAULT_LOCALE = 'en'
|
2017-09-12 07:36:17 -04:00
|
|
|
|
2017-08-06 21:43:09 -04:00
|
|
|
def initialize(site_setting)
|
|
|
|
@site_setting = site_setting
|
|
|
|
@defaults = {}
|
|
|
|
@defaults[DEFAULT_LOCALE.to_sym] = {}
|
|
|
|
end
|
|
|
|
|
2018-05-31 22:22:47 -04:00
|
|
|
def load_setting(name_arg, value, locale_defaults)
|
2017-08-06 21:43:09 -04:00
|
|
|
name = name_arg.to_sym
|
|
|
|
@defaults[DEFAULT_LOCALE.to_sym][name] = value
|
|
|
|
|
2018-05-31 22:22:47 -04:00
|
|
|
if (locale_defaults)
|
|
|
|
locale_defaults.each do |locale, v|
|
2017-08-06 21:43:09 -04:00
|
|
|
locale = locale.to_sym
|
|
|
|
@defaults[locale] ||= {}
|
|
|
|
@defaults[locale][name] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_all
|
2018-05-31 22:22:47 -04:00
|
|
|
@site_setting.provider.all
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
2018-05-31 22:22:47 -04: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-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
2018-05-31 22:22:47 -04:00
|
|
|
def get(name, locale = DEFAULT_LOCALE)
|
2018-09-11 07:20:42 -04:00
|
|
|
value = @defaults.dig(locale.to_sym, name.to_sym)
|
|
|
|
return value unless value.nil?
|
|
|
|
|
|
|
|
@defaults.dig(DEFAULT_LOCALE.to_sym, name.to_sym)
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
2017-08-07 02:15:32 -04:00
|
|
|
alias [] get
|
2017-08-06 21:43:09 -04:00
|
|
|
|
|
|
|
# Used to override site settings in dev/test env
|
|
|
|
def set_regardless_of_locale(name, value)
|
|
|
|
name = name.to_sym
|
2018-05-31 22:22:47 -04:00
|
|
|
if name == :default_locale || @site_setting.has_setting?(name)
|
2017-08-06 21:43:09 -04: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-05-31 22:22:47 -04: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-06 21:43:09 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError.new("No setting named '#{name}' exists")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_setting?(name)
|
2018-05-31 22:22:47 -04:00
|
|
|
has_key?(name.to_sym) || has_key?("#{name.to_s}?".to_sym) || name.to_sym == :default_locale
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-07 02:15:32 -04:00
|
|
|
def has_key?(name)
|
2018-05-31 22:22:47 -04:00
|
|
|
@defaults[DEFAULT_LOCALE.to_sym].key?(name)
|
2017-08-06 22:14:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_db
|
|
|
|
RailsMultisite::ConnectionManagement.current_db
|
2017-08-06 21:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|