From 2939e9898b1e0e7da6db802a00e594be4c85499d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 13 Jan 2017 02:42:22 +0100 Subject: Extend rails-settings-cached to merge db-saved hash values with defaults --- app/controllers/api/v1/accounts_controller.rb | 2 +- app/lib/settings/extend.rb | 9 +++++++++ app/lib/settings/scoped_settings.rb | 12 ++++++++++++ app/models/setting.rb | 20 ++++++++++++++++++++ app/models/user.rb | 2 +- 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 app/lib/settings/extend.rb create mode 100644 app/lib/settings/scoped_settings.rb (limited to 'app') diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 05ff806c5..bd52dfb2c 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -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 index 000000000..7241a1221 --- /dev/null +++ b/app/lib/settings/extend.rb @@ -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 index 000000000..f8f22a91b --- /dev/null +++ b/app/lib/settings/scoped_settings.rb @@ -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 diff --git a/app/models/setting.rb b/app/models/setting.rb index 0a429a62b..f3c65c054 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index bf7d04d7c..71d3ee0b8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 -- cgit