releaf-core 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 682a49cfe8ce543d290aa1adb1ed44ccffb2bee92d4aa8fc96860ed9f4e13bf1
4
- data.tar.gz: 3c432539dff5918c336d99cda6b4daedfb79f7ab0bd8a5e965a7fd282cf0ef9b
3
+ metadata.gz: cf0ce248be934f51e65b01bf42bf8fa3d46c82163f5dee629d42536e480c1dbf
4
+ data.tar.gz: 49b27b2b95128d445129d4c3d4fad7e1410ea750ef1c3235daecb0bb845ca95c
5
5
  SHA512:
6
- metadata.gz: d58c6ead24e86e9e7b07aba858d727288032d5901628d869e6930d6e57a89b655648083ef2cf7ee5c87acde18e60b44fd5bdc307b711b7152e1a60deafea624d
7
- data.tar.gz: 69271b23142584ca4b004f6be2d486e39101e7e3dd104717190441e741afa3afab0e7d130b2e55be901dbc8ddf28b05a8a1971d7ddfe06387180a2188f9cb661
6
+ metadata.gz: 886526531af17482e95918a74c28de9cc261d32543a6c0978556743a9bded4debbcebc1c1e420321b493dfb2124ac47e6aec03d9ec3fd9773ba3502774dbc3f7
7
+ data.tar.gz: e974d509669fe47308c49ddbbdb8c86d54a7c035af8dc1eb84e3dd16aa8261fe7a09d8484766d2c4c88d07f8f9bcbd15339da60cb7a13b92b829e4d43de97a04
@@ -1,4 +1,32 @@
1
- class Releaf::Settings < RailsSettings::Base
1
+ class Releaf::Settings < ActiveRecord::Base
2
+
3
+ self.table_name = table_name_prefix + "settings"
4
+
5
+ class SettingNotFound < RuntimeError; end
6
+
7
+ belongs_to :thing, polymorphic: true, optional: true
8
+
9
+ # get the value field, YAML decoded
10
+ def value
11
+ YAML.load(self[:value], permitted_classes: [DateTime, Time, Date]) if self[:value].present?
12
+ end
13
+
14
+ # set the value field, YAML encoded
15
+ def value=(new_value)
16
+ self[:value] = new_value.to_yaml
17
+ end
18
+
19
+ def rewrite_cache
20
+ Rails.cache.write(cache_key, value)
21
+ end
22
+
23
+ def expire_cache
24
+ Rails.cache.delete(cache_key)
25
+ end
26
+
27
+ def cache_key
28
+ self.class.cache_key(var, thing)
29
+ end
2
30
 
3
31
  scope :registered, -> { where(var: registered_keys, thing_type: nil, thing_id: nil).order(:var) }
4
32
 
@@ -36,4 +64,129 @@ class Releaf::Settings < RailsSettings::Base
36
64
  def self.supported_types
37
65
  [:boolean, :date, :time, :datetime, :integer, :float, :decimal, :email, :text, :textarea, :richtext]
38
66
  end
67
+
68
+ class << self
69
+ def cache_prefix(&block)
70
+ @cache_prefix = block
71
+ end
72
+
73
+ def cache_key(var_name, scope_object)
74
+ scope = ["rails_settings_cached"]
75
+ scope << @cache_prefix.call if @cache_prefix
76
+ scope << "#{scope_object.class.name}-#{scope_object.id}" if scope_object
77
+ scope << var_name.to_s
78
+ scope.join("/")
79
+ end
80
+
81
+ def [](key)
82
+ return super(key) unless rails_initialized?
83
+ val = Rails.cache.fetch(cache_key(key, @object)) do
84
+ super(key)
85
+ end
86
+ val
87
+ end
88
+
89
+ # set a setting value by [] notation
90
+ def []=(var_name, value)
91
+ super
92
+ Rails.cache.write(cache_key(var_name, @object), value)
93
+ value
94
+ end
95
+
96
+ # get or set a variable with the variable as the called method
97
+ # rubocop:disable Style/MethodMissing
98
+ def method_missing(method, *args)
99
+ method_name = method.to_s
100
+ super(method, *args)
101
+ rescue NoMethodError
102
+ # set a value for a variable
103
+ if method_name[-1] == "="
104
+ var_name = method_name.sub("=", "")
105
+ value = args.first
106
+ self[var_name] = value
107
+ else
108
+ # retrieve a value
109
+ self[method_name]
110
+ end
111
+ end
112
+
113
+ # destroy the specified settings record
114
+ def destroy(var_name)
115
+ var_name = var_name.to_s
116
+ obj = object(var_name)
117
+ raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?
118
+
119
+ obj.destroy
120
+ true
121
+ end
122
+
123
+ # retrieve all settings as a hash (optionally starting with a given namespace)
124
+ def get_all(starting_with = nil)
125
+ vars = thing_scoped.select("var, value")
126
+ vars = vars.where("var LIKE '#{starting_with}%'") if starting_with
127
+ result = {}
128
+ vars.each { |record| result[record.var] = record.value }
129
+ result.reverse_merge!(default_settings(starting_with))
130
+ result.with_indifferent_access
131
+ end
132
+
133
+ def where(sql = nil)
134
+ vars = thing_scoped.where(sql) if sql
135
+ vars
136
+ end
137
+
138
+ # get a setting value by [] notation
139
+ def [](var_name)
140
+ val = object(var_name)
141
+ return val.value if val
142
+ end
143
+
144
+ # set a setting value by [] notation
145
+ def []=(var_name, value)
146
+ var_name = var_name.to_s
147
+
148
+ record = object(var_name) || thing_scoped.new(var: var_name)
149
+ record.value = value
150
+ record.save!
151
+
152
+ value
153
+ end
154
+
155
+ def merge!(var_name, hash_value)
156
+ raise ArgumentError unless hash_value.is_a?(Hash)
157
+
158
+ old_value = self[var_name] || {}
159
+ raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
160
+
161
+ new_value = old_value.merge(hash_value)
162
+ self[var_name] = new_value if new_value != old_value
163
+
164
+ new_value
165
+ end
166
+
167
+ def object(var_name)
168
+ return nil unless table_exists?
169
+ thing_scoped.where(var: var_name.to_s).first
170
+ end
171
+
172
+ def thing_scoped
173
+ unscoped.where("thing_type is NULL and thing_id is NULL")
174
+ end
175
+
176
+ def source(filename)
177
+ Default.source(filename)
178
+ end
179
+
180
+ def rails_initialized?
181
+ Rails.application && Rails.application.initialized?
182
+ end
183
+
184
+ private
185
+
186
+ def default_settings(starting_with = nil)
187
+ return {} unless Default.enabled?
188
+ return Default.instance if starting_with.nil?
189
+ Default.instance.select { |key, _| key.to_s.start_with?(starting_with) }
190
+ end
191
+ end
39
192
  end
@@ -1,8 +1,16 @@
1
1
  module Releaf::Root
2
2
  class SettingsManager
3
+ CAST_MAP = {
4
+ "false" => false,
5
+ "true" => true,
6
+ }
3
7
 
4
8
  def self.read(controller:, key:)
5
- controller.send(:cookies)[key]
9
+ value = controller.send(:cookies)[key]
10
+
11
+ return CAST_MAP[value] if CAST_MAP.key? value
12
+
13
+ value
6
14
  end
7
15
 
8
16
  def self.write(controller:, key:, value:)
data/lib/releaf-core.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'rails-settings-cached'
2
1
  require 'ckeditor_rails'
3
2
  require 'will_paginate'
4
3
  require 'font-awesome-rails'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: releaf-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - CubeSystems
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-19 00:00:00.000000000 Z
11
+ date: 2022-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -179,33 +179,33 @@ dependencies:
179
179
  - !ruby/object:Gem::Version
180
180
  version: '1.4'
181
181
  - !ruby/object:Gem::Dependency
182
- name: dragonfly
182
+ name: net-smtp
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
185
  - - "~>"
186
186
  - !ruby/object:Gem::Version
187
- version: '1.0'
187
+ version: '0.3'
188
188
  type: :runtime
189
189
  prerelease: false
190
190
  version_requirements: !ruby/object:Gem::Requirement
191
191
  requirements:
192
192
  - - "~>"
193
193
  - !ruby/object:Gem::Version
194
- version: '1.0'
194
+ version: '0.3'
195
195
  - !ruby/object:Gem::Dependency
196
- name: rails-settings-cached
196
+ name: dragonfly
197
197
  requirement: !ruby/object:Gem::Requirement
198
198
  requirements:
199
199
  - - "~>"
200
200
  - !ruby/object:Gem::Version
201
- version: '0.4'
201
+ version: '1.0'
202
202
  type: :runtime
203
203
  prerelease: false
204
204
  version_requirements: !ruby/object:Gem::Requirement
205
205
  requirements:
206
206
  - - "~>"
207
207
  - !ruby/object:Gem::Version
208
- version: '0.4'
208
+ version: '1.0'
209
209
  - !ruby/object:Gem::Dependency
210
210
  name: ckeditor_rails
211
211
  requirement: !ruby/object:Gem::Requirement
@@ -929,7 +929,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
929
929
  - !ruby/object:Gem::Version
930
930
  version: '0'
931
931
  requirements: []
932
- rubygems_version: 3.2.6
932
+ rubygems_version: 3.3.7
933
933
  signing_key:
934
934
  specification_version: 4
935
935
  summary: core gem for releaf