]> cat aescling's git repositories - mastodon.git/commitdiff
Extend rails-settings-cached to merge db-saved hash values with defaults
authorEugen Rochko <eugen@zeonfederated.com>
Fri, 13 Jan 2017 01:42:22 +0000 (02:42 +0100)
committerEugen Rochko <eugen@zeonfederated.com>
Fri, 13 Jan 2017 01:42:22 +0000 (02:42 +0100)
app/controllers/api/v1/accounts_controller.rb
app/lib/settings/extend.rb [new file with mode: 0644]
app/lib/settings/scoped_settings.rb [new file with mode: 0644]
app/models/setting.rb
app/models/user.rb
db/schema.rb

index 05ff806c582f6a4128897ec43e7cb90c5d6f8643..bd52dfb2c533c0f90977290f7f8dbad957bc4867 100644 (file)
@@ -96,7 +96,7 @@ class Api::V1::AccountsController < ApiController
     limit = params[:limit] ? [DEFAULT_ACCOUNTS_LIMIT, params[:limit].to_i].min : DEFAULT_ACCOUNTS_LIMIT
     @accounts = SearchService.new.call(params[:q], limit, params[:resolve] == 'true')
 
-    set_account_counters_maps(@accounts)
+    set_account_counters_maps(@accounts) unless @accounts.nil?
 
     render action: :index
   end
diff --git a/app/lib/settings/extend.rb b/app/lib/settings/extend.rb
new file mode 100644 (file)
index 0000000..7241a12
--- /dev/null
@@ -0,0 +1,9 @@
+module Settings
+  module Extend
+       extend ActiveSupport::Concern
+
+    def settings
+      ScopedSettings.for_thing(self)
+    end
+  end
+end
\ No newline at end of file
diff --git a/app/lib/settings/scoped_settings.rb b/app/lib/settings/scoped_settings.rb
new file mode 100644 (file)
index 0000000..f8f22a9
--- /dev/null
@@ -0,0 +1,12 @@
+module Settings
+  class ScopedSettings < ::Setting
+    def self.for_thing(object)
+      @object = object
+      self
+    end
+
+    def self.thing_scoped
+      unscoped.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id)
+    end
+  end
+end
\ No newline at end of file
index 0a429a62b26eaa0792756e3882f5042d2705cbd8..f3c65c0548cffdbc4d92bc54d8f0bef018f8b731 100644 (file)
@@ -9,6 +9,26 @@ class Setting < RailsSettings::Base
   end
 
   class << self
+
+    def [](key)
+      return super(key) unless rails_initialized?
+      
+      val = Rails.cache.fetch(cache_key(key, @object)) do
+        db_val = object(key)
+
+        if db_val
+          default_value = default_settings[key]
+
+          return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash)
+          db_val.value
+        else
+          default_settings[key]
+        end
+      end
+      
+      val
+    end
+
     def all_as_records
       vars    = thing_scoped
       records = vars.map { |r| [r.var, r] }.to_h
index bf7d04d7c5d5596e148de8057a4a91b298898711..71d3ee0b8551132eb5175b5b1c83b2de2db1c285 100644 (file)
@@ -1,7 +1,7 @@
 # frozen_string_literal: true
 
 class User < ApplicationRecord
-  include RailsSettings::Extend
+  include Settings::Extend
 
   devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
 
index 1cd1258db8bc72ad024369d94bad104607253df8..f1bd752c9a6585322bc95c6d60d05ea202200190 100644 (file)
@@ -169,74 +169,6 @@ ActiveRecord::Schema.define(version: 20170112154826) do
     t.index ["topic", "callback"], name: "index_pubsubhubbub_subscriptions_on_topic_and_callback", unique: true, using: :btree
   end
 
-  create_table "push_devices", force: :cascade do |t|
-    t.string   "service",    default: "", null: false
-    t.string   "token",      default: "", null: false
-    t.integer  "account",                 null: false
-    t.datetime "created_at",              null: false
-    t.datetime "updated_at",              null: false
-    t.index ["service", "token"], name: "index_push_devices_on_service_and_token", unique: true, using: :btree
-  end
-
-  create_table "rpush_apps", force: :cascade do |t|
-    t.string   "name",                                null: false
-    t.string   "environment"
-    t.text     "certificate"
-    t.string   "password"
-    t.integer  "connections",             default: 1, null: false
-    t.datetime "created_at",                          null: false
-    t.datetime "updated_at",                          null: false
-    t.string   "type",                                null: false
-    t.string   "auth_key"
-    t.string   "client_id"
-    t.string   "client_secret"
-    t.string   "access_token"
-    t.datetime "access_token_expiration"
-  end
-
-  create_table "rpush_feedback", force: :cascade do |t|
-    t.string   "device_token", limit: 64, null: false
-    t.datetime "failed_at",               null: false
-    t.datetime "created_at",              null: false
-    t.datetime "updated_at",              null: false
-    t.integer  "app_id"
-    t.index ["device_token"], name: "index_rpush_feedback_on_device_token", using: :btree
-  end
-
-  create_table "rpush_notifications", force: :cascade do |t|
-    t.integer  "badge"
-    t.string   "device_token",      limit: 64
-    t.string   "sound",                        default: "default"
-    t.text     "alert"
-    t.text     "data"
-    t.integer  "expiry",                       default: 86400
-    t.boolean  "delivered",                    default: false,     null: false
-    t.datetime "delivered_at"
-    t.boolean  "failed",                       default: false,     null: false
-    t.datetime "failed_at"
-    t.integer  "error_code"
-    t.text     "error_description"
-    t.datetime "deliver_after"
-    t.datetime "created_at",                                       null: false
-    t.datetime "updated_at",                                       null: false
-    t.boolean  "alert_is_json",                default: false
-    t.string   "type",                                             null: false
-    t.string   "collapse_key"
-    t.boolean  "delay_while_idle",             default: false,     null: false
-    t.text     "registration_ids"
-    t.integer  "app_id",                                           null: false
-    t.integer  "retries",                      default: 0
-    t.string   "uri"
-    t.datetime "fail_after"
-    t.boolean  "processing",                   default: false,     null: false
-    t.integer  "priority"
-    t.text     "url_args"
-    t.string   "category"
-    t.boolean  "content_available",            default: false
-    t.text     "notification"
-    t.index ["delivered", "failed"], name: "index_rpush_notifications_multi", where: "((NOT delivered) AND (NOT failed))", using: :btree
-  end
-
   create_table "settings", force: :cascade do |t|
     t.string   "var",        null: false
     t.text     "value"
@@ -259,6 +191,7 @@ ActiveRecord::Schema.define(version: 20170112154826) do
     t.boolean  "sensitive",              default: false
     t.integer  "visibility",             default: 0,     null: false
     t.integer  "in_reply_to_account_id"
+    t.string   "conversation_uri"
     t.index ["account_id"], name: "index_statuses_on_account_id", using: :btree
     t.index ["in_reply_to_id"], name: "index_statuses_on_in_reply_to_id", using: :btree
     t.index ["reblog_of_id"], name: "index_statuses_on_reblog_of_id", using: :btree