about summary refs log tree commit diff
path: root/app/lib/settings
diff options
context:
space:
mode:
authormasarakki <masaki182@gmail.com>2017-05-25 12:18:08 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-25 05:18:08 +0200
commit3b59f9c6c254e34d95d917857ccbe79f6e1104b0 (patch)
tree79bcf272e8a0f4924b7eef5dce54f2b6dd6d6906 /app/lib/settings
parent135bdd149e7170d0b91ffcb66f2de8154fe7a066 (diff)
fix-cache-returns-nil (#3213)
Diffstat (limited to 'app/lib/settings')
-rw-r--r--app/lib/settings/extend.rb2
-rw-r--r--app/lib/settings/scoped_settings.rb65
2 files changed, 61 insertions, 6 deletions
diff --git a/app/lib/settings/extend.rb b/app/lib/settings/extend.rb
index 407c3480f..884b49874 100644
--- a/app/lib/settings/extend.rb
+++ b/app/lib/settings/extend.rb
@@ -5,7 +5,7 @@ module Settings
     extend ActiveSupport::Concern
 
     def settings
-      ScopedSettings.for_thing(self)
+      @settings ||= ScopedSettings.new(self)
     end
   end
 end
diff --git a/app/lib/settings/scoped_settings.rb b/app/lib/settings/scoped_settings.rb
index 82b70d128..68f97b5ae 100644
--- a/app/lib/settings/scoped_settings.rb
+++ b/app/lib/settings/scoped_settings.rb
@@ -1,14 +1,69 @@
 # frozen_string_literal: true
 
 module Settings
-  class ScopedSettings < ::Setting
-    def self.for_thing(object)
+  class ScopedSettings
+    def initialize(object)
       @object = object
-      self
     end
 
-    def self.thing_scoped
-      unscoped.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id)
+    # rubocop:disable Style/MethodMissing
+    def method_missing(method, *args)
+      method_name = method.to_s
+      # set a value for a variable
+      if method_name[-1] == '='
+        var_name = method_name.sub('=', '')
+        value = args.first
+        self[var_name] = value
+      else
+        # retrieve a value
+        self[method_name]
+      end
+    end
+    # rubocop:enable Style/MethodMissing
+
+    def respond_to?(_method_name, _include_private = false)
+      true
+    end
+
+    def all_as_records
+      vars = thing_scoped
+      records = vars.map { |r| [r.var, r] }.to_h
+
+      Setting.send(: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
+
+      records
+    end
+
+    def []=(key, value)
+      key = key.to_s
+      record = thing_scoped.find_by(var: key) || thing_scoped.new(var: key)
+      record.value = value
+      record.save!
+
+      Rails.cache.write(Setting.cache_key(key, @object), value)
+      value
+    end
+
+    def [](key)
+      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]
+          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]
+        end
+      end
+    end
+
+    protected
+
+    def thing_scoped
+      Setting.unscoped.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id)
     end
   end
 end