rails-settings-cached 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -9
- data/lib/rails-settings/base.rb +22 -11
- data/lib/rails-settings/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 765057576ddcdb19d9f394f1844950f03e8c70944abd9b104e70824f60f7e0e5
|
4
|
+
data.tar.gz: 0d2a2c4aa7560d96bee3a0c1a0d50f7cb89bffd5c650ae47f2bd6169c0d12b6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 908f7ae3d8e01710f9df4cb32a77b2302129107bcd34254cb5237fc5f5b98f2e4007fe315751ab3fd7e2212e14a0db133f796f994b0bd6a1cee48768717a01a9
|
7
|
+
data.tar.gz: fe611e7af4ac5ca72a98040c63b0475833878ac6f11b82953fd80458bb6b96311fff7fc174f890171f466f724be809cd334bc184b0cb0c8c7b3ae94f5fbee28f
|
data/README.md
CHANGED
@@ -50,6 +50,7 @@ class Setting < RailsSettings::Base
|
|
50
50
|
field :host, default: "http://example.com"
|
51
51
|
field :readonly_item, type: :integer, default: 100, readonly: true
|
52
52
|
field :user_limits, type: :integer, default: 20
|
53
|
+
field :exchange_rate, type: :float, default: 0.123
|
53
54
|
field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
|
54
55
|
# Override array separator, default: /[\n,]/ split with \n or comma.
|
55
56
|
field :tips, type: :array, separator: /[\n]+/
|
@@ -212,18 +213,13 @@ module Admin
|
|
212
213
|
class SettingsController < ApplicationController
|
213
214
|
before_action :get_setting, only: [:edit, :update]
|
214
215
|
|
215
|
-
def show
|
216
|
-
end
|
217
|
-
|
218
216
|
def create
|
219
217
|
setting_params.keys.each do |key|
|
220
|
-
next if key.to_s == "site_logo"
|
221
218
|
Setting.send("#{key}=", setting_params[key].strip) unless setting_params[key].nil?
|
222
219
|
end
|
223
|
-
redirect_to
|
220
|
+
redirect_to settings_path, notice: "Setting was successfully updated."
|
224
221
|
end
|
225
222
|
|
226
|
-
|
227
223
|
private
|
228
224
|
def setting_params
|
229
225
|
params.require(:setting).permit(:host, :user_limits, :admin_emails,
|
@@ -259,7 +255,8 @@ app/views/admin/settings/show.html.erb
|
|
259
255
|
<%= f.text_area :notification_options, value: YAML.dump(Setting.notification_options), class: "form-control", style: "height: 180px;" %>
|
260
256
|
<div class="form-text">
|
261
257
|
Use YAML format to config the SMTP_html
|
262
|
-
</
|
258
|
+
</div>
|
259
|
+
</div>
|
263
260
|
<% end %>
|
264
261
|
```
|
265
262
|
|
@@ -331,8 +328,7 @@ Same values will fetch from the `settings` table.
|
|
331
328
|
|
332
329
|
## Use cases:
|
333
330
|
|
334
|
-
- [ruby-china/
|
331
|
+
- [ruby-china/homeland](https://github.com/ruby-china/homeland)
|
335
332
|
- [thebluedoc/bluedoc](https://github.com/thebluedoc/bluedoc/blob/master/app/models/setting.rb)
|
336
333
|
- [tootsuite/mastodon](https://github.com/tootsuite/mastodon)
|
337
334
|
- [helpyio/helpy](https://github.com/helpyio/helpy)
|
338
|
-
|
data/lib/rails-settings/base.rb
CHANGED
@@ -6,7 +6,7 @@ module RailsSettings
|
|
6
6
|
class Base < ActiveRecord::Base
|
7
7
|
class SettingNotFound < RuntimeError; end
|
8
8
|
|
9
|
-
SEPARATOR_REGEXP = /[\n
|
9
|
+
SEPARATOR_REGEXP = /[\n,;]+/
|
10
10
|
self.table_name = table_name_prefix + "settings"
|
11
11
|
|
12
12
|
# get the value field, YAML decoded
|
@@ -30,6 +30,8 @@ module RailsSettings
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def field(key, **opts)
|
33
|
+
@keys ||= []
|
34
|
+
@keys << key.to_s
|
33
35
|
_define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator])
|
34
36
|
end
|
35
37
|
|
@@ -43,11 +45,16 @@ module RailsSettings
|
|
43
45
|
scope.join("/")
|
44
46
|
end
|
45
47
|
|
48
|
+
def keys
|
49
|
+
@keys
|
50
|
+
end
|
51
|
+
|
46
52
|
private
|
53
|
+
|
47
54
|
def _define_field(key, default: nil, type: :string, readonly: false, separator: nil)
|
48
55
|
if readonly
|
49
56
|
define_singleton_method(key) do
|
50
|
-
self.send(:
|
57
|
+
self.send(:_convert_string_to_typeof_value, type, default, separator: separator)
|
51
58
|
end
|
52
59
|
else
|
53
60
|
define_singleton_method(key) do
|
@@ -60,7 +67,7 @@ module RailsSettings
|
|
60
67
|
result = default.call if default.is_a?(Proc)
|
61
68
|
end
|
62
69
|
|
63
|
-
result = self.send(:
|
70
|
+
result = self.send(:_convert_string_to_typeof_value, type, result, separator: separator)
|
64
71
|
|
65
72
|
result
|
66
73
|
end
|
@@ -69,7 +76,7 @@ module RailsSettings
|
|
69
76
|
var_name = key.to_s
|
70
77
|
|
71
78
|
record = find_by(var: var_name) || new(var: var_name)
|
72
|
-
value = self.send(:
|
79
|
+
value = self.send(:_convert_string_to_typeof_value, type, value, separator: separator)
|
73
80
|
|
74
81
|
record.value = value
|
75
82
|
record.save!
|
@@ -85,20 +92,24 @@ module RailsSettings
|
|
85
92
|
end
|
86
93
|
end
|
87
94
|
|
88
|
-
def
|
89
|
-
return value unless
|
95
|
+
def _convert_string_to_typeof_value(type, value, separator: nil)
|
96
|
+
return value unless [String, Integer, Float, BigDecimal].include?(value.class)
|
90
97
|
|
91
98
|
case type
|
92
99
|
when :boolean
|
93
|
-
|
100
|
+
value == "true" || value == "1" || value == 1 || value == true
|
94
101
|
when :array
|
95
|
-
|
102
|
+
value.split(separator || SEPARATOR_REGEXP).reject { |str| str.empty? }.map(&:strip)
|
96
103
|
when :hash
|
97
|
-
value = YAML.load(value).
|
104
|
+
value = YAML.load(value).to_h rescue eval(value).to_h rescue {}
|
98
105
|
value.deep_stringify_keys!
|
99
|
-
|
106
|
+
value
|
100
107
|
when :integer
|
101
|
-
|
108
|
+
value.to_i
|
109
|
+
when :float
|
110
|
+
value.to_f
|
111
|
+
when :big_decimal
|
112
|
+
value.to_d
|
102
113
|
else
|
103
114
|
value
|
104
115
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-settings-cached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: codecov
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rubocop
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: simplecov
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -95,7 +95,7 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: sqlite3
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|