Refactor Setting (#3302)

* @object is not needed

* Remove unneeded dependencies

* Do not call private method

* Prefer #respond_to_missing? over #respond_to?

`#respond_to?` doesn't support `User.settings.method(:method_name)`

* Use find_or_initialize_by instead of
th-downstream
alpaca-tc 8 years ago committed by Eugen Rochko
parent b69a1f599a
commit 0931218b3f

@ -2,8 +2,6 @@
module Settings module Settings
module Extend module Extend
extend ActiveSupport::Concern
def settings def settings
@settings ||= ScopedSettings.new(self) @settings ||= ScopedSettings.new(self)
end end

@ -21,7 +21,7 @@ module Settings
end end
# rubocop:enable Style/MethodMissing # rubocop:enable Style/MethodMissing
def respond_to?(_method_name, _include_private = false) def respond_to_missing?(*)
true true
end end
@ -29,7 +29,7 @@ module Settings
vars = thing_scoped vars = thing_scoped
records = vars.map { |r| [r.var, r] }.to_h records = vars.map { |r| [r.var, r] }.to_h
Setting.send(:default_settings).each do |key, default_value| Setting.default_settings.each do |key, default_value|
next if records.key?(key) || default_value.is_a?(Hash) next if records.key?(key) || default_value.is_a?(Hash)
records[key] = Setting.new(var: key, value: default_value) records[key] = Setting.new(var: key, value: default_value)
end end
@ -39,9 +39,8 @@ module Settings
def []=(key, value) def []=(key, value)
key = key.to_s key = key.to_s
record = thing_scoped.find_by(var: key) || thing_scoped.new(var: key) record = thing_scoped.find_or_initialize_by(var: key)
record.value = value record.update!(value: value)
record.save!
Rails.cache.write(Setting.cache_key(key, @object), value) Rails.cache.write(Setting.cache_key(key, @object), value)
value value
@ -51,11 +50,11 @@ module Settings
Rails.cache.fetch(Setting.cache_key(key, @object)) do Rails.cache.fetch(Setting.cache_key(key, @object)) do
db_val = thing_scoped.find_by(var: key.to_s) db_val = thing_scoped.find_by(var: key.to_s)
if db_val if db_val
default_value = Setting.send(:default_settings)[key] default_value = Setting.default_settings[key]
return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash) return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash)
db_val.value db_val.value
else else
Setting.send(:default_settings)[key] Setting.default_settings[key]
end end
end end
end end

@ -23,7 +23,7 @@ class Setting < RailsSettings::Base
def [](key) def [](key)
return super(key) unless rails_initialized? return super(key) unless rails_initialized?
val = Rails.cache.fetch(cache_key(key, @object)) do val = Rails.cache.fetch(cache_key(key, nil)) do
db_val = object(key) db_val = object(key)
if db_val if db_val
@ -50,8 +50,6 @@ class Setting < RailsSettings::Base
records records
end end
private
def default_settings def default_settings
return {} unless RailsSettings::Default.enabled? return {} unless RailsSettings::Default.enabled?
RailsSettings::Default.instance RailsSettings::Default.instance

Loading…
Cancel
Save