about summary refs log tree commit diff
path: root/app/lib/settings
diff options
context:
space:
mode:
authoralpaca-tc <alpaca-tc@alpaca.tc>2017-05-25 21:14:36 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-25 14:14:36 +0200
commit08dce5e60792112ca8df4aa5de3663fd7ad51e77 (patch)
treec007e13c212b1c2f1a5e75937500b5196017604e /app/lib/settings
parent2469fd1cdcbb3778878a33a7879d5f1fa563c476 (diff)
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
Diffstat (limited to 'app/lib/settings')
-rw-r--r--app/lib/settings/extend.rb2
-rw-r--r--app/lib/settings/scoped_settings.rb13
2 files changed, 6 insertions, 9 deletions
diff --git a/app/lib/settings/extend.rb b/app/lib/settings/extend.rb
index 884b49874..5fb2c8aae 100644
--- a/app/lib/settings/extend.rb
+++ b/app/lib/settings/extend.rb
@@ -2,8 +2,6 @@
 
 module Settings
   module Extend
-    extend ActiveSupport::Concern
-
     def settings
       @settings ||= ScopedSettings.new(self)
     end
diff --git a/app/lib/settings/scoped_settings.rb b/app/lib/settings/scoped_settings.rb
index 68f97b5ae..de4af3009 100644
--- a/app/lib/settings/scoped_settings.rb
+++ b/app/lib/settings/scoped_settings.rb
@@ -21,7 +21,7 @@ module Settings
     end
     # rubocop:enable Style/MethodMissing
 
-    def respond_to?(_method_name, _include_private = false)
+    def respond_to_missing?(*)
       true
     end
 
@@ -29,7 +29,7 @@ module Settings
       vars = thing_scoped
       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)
         records[key] = Setting.new(var: key, value: default_value)
       end
@@ -39,9 +39,8 @@ module Settings
 
     def []=(key, value)
       key = key.to_s
-      record = thing_scoped.find_by(var: key) || thing_scoped.new(var: key)
-      record.value = value
-      record.save!
+      record = thing_scoped.find_or_initialize_by(var: key)
+      record.update!(value: value)
 
       Rails.cache.write(Setting.cache_key(key, @object), value)
       value
@@ -51,11 +50,11 @@ module Settings
       Rails.cache.fetch(Setting.cache_key(key, @object)) do
         db_val = thing_scoped.find_by(var: key.to_s)
         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)
           db_val.value
         else
-          Setting.send(:default_settings)[key]
+          Setting.default_settings[key]
         end
       end
     end