rails-settings-cached 2.0.3 → 2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +84 -13
- data/lib/rails-settings/base.rb +5 -5
- data/lib/rails-settings/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08c5252dd50adb752c5f4271a8e8c6f9a33ed99688d1df97de143430aa8a3010'
|
4
|
+
data.tar.gz: 95cec20166fcf229ffe6f7b6bb70477e5139b297012b685a7ace320ff956639d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01d00c7d1681bdf3e3510a8397b5c7e3569672783260be3ef5fe835e5b51b7fb7fe15e9d328bcd92816100b23c4d8441e47602aa9ef3e56b2d77594df269375a
|
7
|
+
data.tar.gz: eec0ac9dfe44f09abdf800c2c968a6c15bb29be0dfb98176fda26c277ec82b4a6753a024069064bbfe8845d5768b121f30b3f024b44e045229e375e43c1c0b27
|
data/README.md
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
## Rails Settings Cached
|
2
2
|
|
3
3
|
This a plugin that makes managing a table of
|
4
|
-
global key, value pairs easy. Think of it like a global Hash stored in your database,
|
4
|
+
а global key, value pairs easy. Think of it like a global Hash stored in your database,
|
5
5
|
that uses simple ActiveRecord like methods for manipulation. Keep track of any global
|
6
|
-
setting that you
|
6
|
+
setting that you don't want to hard code into your rails app. You can store any kind
|
7
7
|
of object. Strings, numbers, arrays, or any object.
|
8
8
|
|
9
9
|
> 🚨 BREAK CHANGES WARNING:
|
10
|
-
> rails-settings-cached 2.x has
|
11
|
-
> When you
|
10
|
+
> rails-settings-cached 2.x has redesigned the API, the new version will compatible with the stored setting values by an older version.
|
11
|
+
> When you want to upgrade 2.x, you must read the README again, and follow guides to change your Setting model.
|
12
|
+
> 0.x stable branch: https://github.com/huacnlee/rails-settings-cached/tree/0.x
|
12
13
|
|
13
14
|
## Status
|
14
15
|
|
@@ -19,7 +20,7 @@ of object. Strings, numbers, arrays, or any object.
|
|
19
20
|
Edit your Gemfile:
|
20
21
|
|
21
22
|
```ruby
|
22
|
-
gem "rails-settings-cached"
|
23
|
+
gem "rails-settings-cached", "~> 2.0"
|
23
24
|
```
|
24
25
|
|
25
26
|
Generate your settings:
|
@@ -59,7 +60,7 @@ class Setting < RailsSettings::Base
|
|
59
60
|
end
|
60
61
|
```
|
61
62
|
|
62
|
-
You must use `field` method to statement the setting keys,
|
63
|
+
You must use `field` method to statement the setting keys, otherwise you can't use it.
|
63
64
|
|
64
65
|
Now just put that migration in the database with:
|
65
66
|
|
@@ -69,7 +70,7 @@ rake db:migrate
|
|
69
70
|
|
70
71
|
## Usage
|
71
72
|
|
72
|
-
The syntax is easy. First,
|
73
|
+
The syntax is easy. First, let's create some settings to keep track of:
|
73
74
|
|
74
75
|
```ruby
|
75
76
|
irb > Setting.host
|
@@ -132,7 +133,7 @@ irb > Setting.notification_options
|
|
132
133
|
|
133
134
|
## Readonly field
|
134
135
|
|
135
|
-
|
136
|
+
Sometimes you may need to use Setting before Rails is initialized, for example `config/devise.rb`
|
136
137
|
|
137
138
|
```rb
|
138
139
|
Devise.setup do |config|
|
@@ -162,9 +163,9 @@ Setting.host -> Check Cache -> Exist - Get value of key for cache -> Return
|
|
162
163
|
Return default value or nil
|
163
164
|
```
|
164
165
|
|
165
|
-
In each Setting keys call, we will load the cache/db and save in Thread.current
|
166
|
+
In each Setting keys call, we will load the cache/db and save in `Thread.current` to avoid hit cache/db.
|
166
167
|
|
167
|
-
Each key update will
|
168
|
+
Each key update will expire the cache, so do not add some frequent update key.
|
168
169
|
|
169
170
|
## Change cache key
|
170
171
|
|
@@ -189,9 +190,9 @@ end
|
|
189
190
|
|
190
191
|
-----
|
191
192
|
|
192
|
-
## How to manage Settings in admin interface?
|
193
|
+
## How to manage Settings in the admin interface?
|
193
194
|
|
194
|
-
If you want create an admin interface to editing the Settings, you can try methods in
|
195
|
+
If you want to create an admin interface to editing the Settings, you can try methods in following:
|
195
196
|
|
196
197
|
config/routes.rb
|
197
198
|
|
@@ -260,6 +261,76 @@ app/views/admin/settings/show.html.erb
|
|
260
261
|
<% end %>
|
261
262
|
```
|
262
263
|
|
263
|
-
##
|
264
|
+
## Backward compatible to support 0.x scoped settings
|
265
|
+
|
266
|
+
You may used the scoped setting feature in 0.x version. Before you upgrade rails-settings-cached 2.x, you must follow this guide to backward compatible it.
|
267
|
+
|
268
|
+
For example:
|
269
|
+
|
270
|
+
```rb
|
271
|
+
class User < ApplicationRecord
|
272
|
+
include RailsSettings::Extend
|
273
|
+
end
|
274
|
+
|
275
|
+
@user.settings.color = "red"
|
276
|
+
@user.settings.foo = 123
|
277
|
+
```
|
278
|
+
|
279
|
+
create `app/models/concerns/scoped_setting.rb`
|
280
|
+
|
281
|
+
```rb
|
282
|
+
module ScopedSetting
|
283
|
+
extend ActiveSupport::Concern
|
284
|
+
|
285
|
+
included do
|
286
|
+
has_many :settings, as: :thing
|
287
|
+
end
|
288
|
+
|
289
|
+
class_methods do
|
290
|
+
def scoped_field(name, default: nil)
|
291
|
+
define_method(name) do
|
292
|
+
obj = settings.where(var: name).take || settings.new(var: name, value: default)
|
293
|
+
obj.value
|
294
|
+
end
|
295
|
+
|
296
|
+
define_method("#{name}=") do |val|
|
297
|
+
record = settings.where(var: name).take || settings.new(var: name)
|
298
|
+
record.value = val
|
299
|
+
record.save!
|
300
|
+
|
301
|
+
val
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
```
|
307
|
+
|
308
|
+
Now include it for your model:
|
309
|
+
|
310
|
+
```rb
|
311
|
+
class User < ApplicationRecord
|
312
|
+
include ScopedSetting
|
313
|
+
|
314
|
+
scoped_field :color, default: ""
|
315
|
+
scoped_field :foo, default: 0
|
316
|
+
end
|
317
|
+
```
|
318
|
+
|
319
|
+
Now you must to find project with ".setting." for replace with:
|
320
|
+
|
321
|
+
Same values will fetch from the `settings` table.
|
322
|
+
|
323
|
+
```rb
|
324
|
+
@user.color = "red"
|
325
|
+
@user.color # => "red"
|
326
|
+
@user.foo = 123
|
327
|
+
@user.foo # =>
|
328
|
+
```
|
329
|
+
|
330
|
+
## Use cases:
|
264
331
|
|
265
332
|
- [ruby-china/ruby-china](https://github.com/ruby-china/ruby-china)
|
333
|
+
- [thebluedoc/bluedoc](https://github.com/thebluedoc/bluedoc/blob/master/app/models/setting.rb)
|
334
|
+
- [tootsuite/mastodon](https://github.com/tootsuite/mastodon)
|
335
|
+
- [helpyio/helpy](https://github.com/helpyio/helpy)
|
336
|
+
|
data/lib/rails-settings/base.rb
CHANGED
@@ -46,11 +46,11 @@ module RailsSettings
|
|
46
46
|
private
|
47
47
|
def _define_field(key, default: nil, type: :string, readonly: false)
|
48
48
|
if readonly
|
49
|
-
|
49
|
+
define_singleton_method(key) do
|
50
50
|
self.send(:_covert_string_to_typeof_value, type, default)
|
51
51
|
end
|
52
52
|
else
|
53
|
-
|
53
|
+
define_singleton_method(key) do
|
54
54
|
val = self.send(:_value_of, key)
|
55
55
|
result = nil
|
56
56
|
if !val.nil?
|
@@ -65,7 +65,7 @@ module RailsSettings
|
|
65
65
|
result
|
66
66
|
end
|
67
67
|
|
68
|
-
|
68
|
+
define_singleton_method("#{key}=") do |value|
|
69
69
|
var_name = key.to_s
|
70
70
|
|
71
71
|
record = find_by(var: var_name) || new(var: var_name)
|
@@ -79,7 +79,7 @@ module RailsSettings
|
|
79
79
|
end
|
80
80
|
|
81
81
|
if type == :boolean
|
82
|
-
|
82
|
+
define_singleton_method("#{key}?") do
|
83
83
|
self.send(key)
|
84
84
|
end
|
85
85
|
end
|
@@ -115,7 +115,7 @@ module RailsSettings
|
|
115
115
|
end
|
116
116
|
|
117
117
|
def _all_settings
|
118
|
-
raise "You
|
118
|
+
raise "You cannot use settings before Rails initialize." unless rails_initialized?
|
119
119
|
RequestStore.store[:rails_settings_all_settings] ||= begin
|
120
120
|
Rails.cache.fetch(self.cache_key, expires_in: 1.week) do
|
121
121
|
vars = unscoped.select("var, value")
|
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.0.
|
4
|
+
version: 2.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
147
|
- !ruby/object:Gem::Version
|
148
148
|
version: '0'
|
149
149
|
requirements: []
|
150
|
-
rubygems_version: 3.0.
|
150
|
+
rubygems_version: 3.0.3
|
151
151
|
signing_key:
|
152
152
|
specification_version: 4
|
153
153
|
summary: Settings plugin for Rails that makes managing a table of global keys.
|