Class: Releaf::Settings
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Releaf::Settings
show all
- Defined in:
- lib/releaf/settings.rb
Defined Under Namespace
Classes: FormBuilder, NormalizeValue, Register, SettingNotFound, TableBuilder
Constant Summary
collapse
- @@registry =
{}.with_indifferent_access
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.[](var_name) ⇒ Object
get a setting value by [] notation
139
140
141
142
143
144
145
|
# File 'lib/releaf/settings.rb', line 139
def [](key)
return super(key) unless rails_initialized?
val = Rails.cache.fetch(cache_key(key, @object)) do
super(key)
end
val
end
|
.[]=(var_name, value) ⇒ Object
set a setting value by [] notation
90
91
92
93
94
|
# File 'lib/releaf/settings.rb', line 90
def []=(var_name, value)
super
Rails.cache.write(cache_key(var_name, @object), value)
value
end
|
.cache_key(var_name, scope_object) ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/releaf/settings.rb', line 73
def cache_key(var_name, scope_object)
scope = ["rails_settings_cached"]
scope << @cache_prefix.call if @cache_prefix
scope << "#{scope_object.class.name}-#{scope_object.id}" if scope_object
scope << var_name.to_s
scope.join("/")
end
|
.cache_prefix(&block) ⇒ Object
69
70
71
|
# File 'lib/releaf/settings.rb', line 69
def cache_prefix(&block)
@cache_prefix = block
end
|
.destroy(var_name) ⇒ Object
destroy the specified settings record
114
115
116
117
118
119
120
121
|
# File 'lib/releaf/settings.rb', line 114
def destroy(var_name)
var_name = var_name.to_s
obj = object(var_name)
raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?
obj.destroy
true
end
|
.get_all(starting_with = nil) ⇒ Object
retrieve all settings as a hash (optionally starting with a given namespace)
124
125
126
127
128
129
130
131
|
# File 'lib/releaf/settings.rb', line 124
def get_all(starting_with = nil)
vars = thing_scoped.select("var, value")
vars = vars.where("var LIKE '#{starting_with}%'") if starting_with
result = {}
vars.each { |record| result[record.var] = record.value }
result.reverse_merge!(default_settings(starting_with))
result.with_indifferent_access
end
|
.merge!(var_name, hash_value) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/releaf/settings.rb', line 155
def merge!(var_name, hash_value)
raise ArgumentError unless hash_value.is_a?(Hash)
old_value = self[var_name] || {}
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
new_value = old_value.merge(hash_value)
self[var_name] = new_value if new_value != old_value
new_value
end
|
.method_missing(method, *args) ⇒ Object
get or set a variable with the variable as the called method rubocop:disable Style/MethodMissing
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/releaf/settings.rb', line 98
def method_missing(method, *args)
method_name = method.to_s
super(method, *args)
rescue NoMethodError
if method_name[-1] == "="
var_name = method_name.sub("=", "")
value = args.first
self[var_name] = value
else
self[method_name]
end
end
|
.object(var_name) ⇒ Object
167
168
169
170
|
# File 'lib/releaf/settings.rb', line 167
def object(var_name)
return nil unless table_exists?
thing_scoped.where(var: var_name.to_s).first
end
|
.rails_initialized? ⇒ Boolean
180
181
182
|
# File 'lib/releaf/settings.rb', line 180
def rails_initialized?
Rails.application && Rails.application.initialized?
end
|
.register(*args) ⇒ Object
.register_scoped ⇒ Object
52
53
54
|
# File 'lib/releaf/settings.rb', line 52
def self.register_scoped
where(var: registered_keys)
end
|
.registered_keys ⇒ Object
56
57
58
|
# File 'lib/releaf/settings.rb', line 56
def self.registered_keys
@@registry.keys
end
|
.source(filename) ⇒ Object
176
177
178
|
# File 'lib/releaf/settings.rb', line 176
def source(filename)
Default.source(filename)
end
|
.supported_types ⇒ Object
64
65
66
|
# File 'lib/releaf/settings.rb', line 64
def self.supported_types
[:boolean, :date, :time, :datetime, :integer, :float, :decimal, :email, :text, :textarea, :richtext]
end
|
.thing_scoped ⇒ Object
172
173
174
|
# File 'lib/releaf/settings.rb', line 172
def thing_scoped
unscoped.where("thing_type is NULL and thing_id is NULL")
end
|
.where(sql = nil) ⇒ Object
133
134
135
136
|
# File 'lib/releaf/settings.rb', line 133
def where(sql = nil)
vars = thing_scoped.where(sql) if sql
vars
end
|
Instance Method Details
#cache_key ⇒ Object
27
28
29
|
# File 'lib/releaf/settings.rb', line 27
def cache_key
self.class.cache_key(var, thing)
end
|
#description ⇒ Object
44
45
46
|
# File 'lib/releaf/settings.rb', line 44
def description
metadata[:description]
end
|
#expire_cache ⇒ Object
23
24
25
|
# File 'lib/releaf/settings.rb', line 23
def expire_cache
Rails.cache.delete(cache_key)
end
|
40
41
42
|
# File 'lib/releaf/settings.rb', line 40
def input_type
metadata[:type] || :text
end
|
48
49
50
|
# File 'lib/releaf/settings.rb', line 48
def metadata
self.class.registry.fetch(var, {})
end
|
#releaf_title ⇒ Object
36
37
38
|
# File 'lib/releaf/settings.rb', line 36
def releaf_title
var
end
|
#rewrite_cache ⇒ Object
19
20
21
|
# File 'lib/releaf/settings.rb', line 19
def rewrite_cache
Rails.cache.write(cache_key, value)
end
|
#value ⇒ Object
get the value field, YAML decoded
10
11
12
|
# File 'lib/releaf/settings.rb', line 10
def value
YAML.load(self[:value], permitted_classes: [DateTime, Time, Date]) if self[:value].present?
end
|
#value=(new_value) ⇒ Object
set the value field, YAML encoded
15
16
17
|
# File 'lib/releaf/settings.rb', line 15
def value=(new_value)
self[:value] = new_value.to_yaml
end
|