]>
cat aescling's git repositories - mastodon.git/blob - app/lib/settings/scoped_settings.rb
1 # frozen_string_literal: true
5 DEFAULTING_TO_UNSCOPED
= %w(
10 def initialize(object
)
14 # rubocop:disable Style/MethodMissing
15 def method_missing(method
, *args
)
16 method_name
= method
.to_s
17 # set a value for a variable
18 if method_name
[-1] == '='
19 var_name
= method_name
.sub('=', '')
21 self[var_name
] = value
27 # rubocop:enable Style/MethodMissing
29 def respond_to_missing
?(*)
35 records
= vars
.map
{ |r
| [r
.var
, r
] }.to_h
37 Setting
.default_settings
.each
do |key
, default_value
|
38 next if records
.key
?(key
) || default_value
.is_a
?(Hash
)
39 records
[key
] = Setting
.new(var
: key
, value
: default_value
)
47 record
= thing_scoped
.find_or_initialize_by(var
: key
)
48 record
.update!
(value
: value
)
50 Rails
.cache
.write(Setting
.cache_key(key
, @object), value
)
55 Rails
.cache
.fetch(Setting
.cache_key(key
, @object)) do
56 db_val
= thing_scoped
.find_by(var
: key
.to_s
)
58 default_value
= ScopedSettings
.default_settings
[key
]
59 return default_value
.with_indifferent_access
.merge!
(db_val
.value
) if default_value
.is_a
?(Hash
)
62 ScopedSettings
.default_settings
[key
]
69 defaulting
= DEFAULTING_TO_UNSCOPED
.map
{ |k
| [k
, Setting
[k
]] }.to_h
70 Setting
.default_settings
.merge!
(defaulting
)
77 Setting
.unscoped
.where(thing_type
: @object.class.base_class
.to_s
, thing_id
: @object.id
)
This page took 0.079519 seconds and 4 git commands to generate.