rails-settings-cached 2.0.3 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -2
- data/README.md +75 -44
- data/lib/generators/settings/install_generator.rb +10 -5
- data/lib/rails-settings/base.rb +29 -18
- data/lib/rails-settings/version.rb +1 -1
- metadata +13 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85279c41b179aa65d668a48d4d18e73b5fe886be9cd8235dc2acdb54c3beb5da
|
4
|
+
data.tar.gz: 516991953aa7698cf86511512d863b7903d1ec5ad648a69c386501413a75ac86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 742596dc7e47c3d31ea64ecf54d96cae5885938076c2704aebe5f3e94701627631d19a8480f24ef97dc1861a8e1a1cab33c6f3aefd56ff755f88ef47c1380401
|
7
|
+
data.tar.gz: aa7b9a3ba2443a7ac6cb12150c196247bfd543e086b4eea1199f83eca1035ddefc1bcb32ae5d01cf9c3448dcd2f5c651dd9a73c8dab22600d28f202ef7d1f4a6
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,43 +1,28 @@
|
|
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
|
-
|
10
|
-
> rails-settings-cached 2.x has redesign the API, the new version will compatible with the stored setting values by older version.
|
11
|
-
> When you wants to upgrade 2.x, you must read the README again, and follow guides to change your Setting model.
|
12
|
-
|
13
|
-
## Status
|
14
|
-
|
15
|
-
[![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![CI Status](https://travis-ci.org/huacnlee/rails-settings-cached.svg)](http://travis-ci.org/huacnlee/rails-settings-cached) [![Code Climate](https://codeclimate.com/github/huacnlee/rails-settings-cached/badges/gpa.svg)](https://codeclimate.com/github/huacnlee/rails-settings-cached) [![codecov.io](https://codecov.io/github/huacnlee/rails-settings-cached/coverage.svg?branch=master)](https://codecov.io/github/huacnlee/rails-settings-cached?branch=master)
|
9
|
+
[![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![CI Status](https://travis-ci.org/huacnlee/rails-settings-cached.svg)](http://travis-ci.org/huacnlee/rails-settings-cached) [![codecov.io](https://codecov.io/github/huacnlee/rails-settings-cached/coverage.svg?branch=master)](https://codecov.io/github/huacnlee/rails-settings-cached?branch=master)
|
16
10
|
|
17
|
-
##
|
11
|
+
## Installation
|
18
12
|
|
19
13
|
Edit your Gemfile:
|
20
14
|
|
21
|
-
```
|
22
|
-
|
15
|
+
```bash
|
16
|
+
$ bundle add rails-settings-cached
|
23
17
|
```
|
24
18
|
|
25
19
|
Generate your settings:
|
26
20
|
|
27
21
|
```bash
|
28
22
|
$ rails g settings:install
|
29
|
-
```
|
30
|
-
|
31
|
-
If you want custom model name:
|
32
23
|
|
33
|
-
|
34
|
-
$ rails g settings:install
|
35
|
-
```
|
36
|
-
|
37
|
-
Or use a custom name:
|
38
|
-
|
39
|
-
```bash
|
40
|
-
$ rails g settings:install SiteConfig
|
24
|
+
# Or use a custom name:
|
25
|
+
$ rails g settings:install AppConfig
|
41
26
|
```
|
42
27
|
|
43
28
|
You will get `app/models/setting.rb`
|
@@ -45,31 +30,40 @@ You will get `app/models/setting.rb`
|
|
45
30
|
```rb
|
46
31
|
class Setting < RailsSettings::Base
|
47
32
|
# cache_prefix { "v1" }
|
48
|
-
|
33
|
+
field :app_name, default: "Rails Settings Cache Demo"
|
49
34
|
field :host, default: "http://example.com"
|
35
|
+
field :default_locale, default: "zh-CN"
|
50
36
|
field :readonly_item, type: :integer, default: 100, readonly: true
|
51
37
|
field :user_limits, type: :integer, default: 20
|
38
|
+
field :exchange_rate, type: :float, default: 0.123
|
52
39
|
field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
|
53
|
-
field :captcha_enable, type: :boolean, default:
|
40
|
+
field :captcha_enable, type: :boolean, default: true
|
41
|
+
|
42
|
+
# Override array separator, default: /[\n,]/ split with \n or comma.
|
43
|
+
field :tips, type: :array, separator: /[\n]+/
|
44
|
+
|
54
45
|
field :notification_options, type: :hash, default: {
|
55
46
|
send_all: true,
|
56
47
|
logging: true,
|
57
48
|
sender_email: "foo@bar.com"
|
58
49
|
}
|
50
|
+
|
51
|
+
# lambda default value
|
52
|
+
field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }
|
59
53
|
end
|
60
54
|
```
|
61
55
|
|
62
|
-
You must use `field` method to statement the setting keys,
|
56
|
+
You must use `field` method to statement the setting keys, otherwise you can't use it.
|
63
57
|
|
64
58
|
Now just put that migration in the database with:
|
65
59
|
|
66
60
|
```bash
|
67
|
-
|
61
|
+
$ rails db:migrate
|
68
62
|
```
|
69
63
|
|
70
64
|
## Usage
|
71
65
|
|
72
|
-
The syntax is easy.
|
66
|
+
The syntax is easy. First, let's create some settings to keep track of:
|
73
67
|
|
74
68
|
```ruby
|
75
69
|
irb > Setting.host
|
@@ -132,7 +126,7 @@ irb > Setting.notification_options
|
|
132
126
|
|
133
127
|
## Readonly field
|
134
128
|
|
135
|
-
|
129
|
+
Sometimes you may need to use Setting before Rails is initialized, for example `config/devise.rb`
|
136
130
|
|
137
131
|
```rb
|
138
132
|
Devise.setup do |config|
|
@@ -162,9 +156,9 @@ Setting.host -> Check Cache -> Exist - Get value of key for cache -> Return
|
|
162
156
|
Return default value or nil
|
163
157
|
```
|
164
158
|
|
165
|
-
In each Setting keys call, we will load the cache/db and save in
|
159
|
+
In each Setting keys call, we will load the cache/db and save in [RequestStore](https://github.com/steveklabnik/request_store) to avoid hit cache/db.
|
166
160
|
|
167
|
-
Each key update will
|
161
|
+
Each key update will expire the cache, so do not add some frequent update key.
|
168
162
|
|
169
163
|
## Change cache key
|
170
164
|
|
@@ -189,15 +183,15 @@ end
|
|
189
183
|
|
190
184
|
-----
|
191
185
|
|
192
|
-
## How to manage Settings in admin interface?
|
186
|
+
## How to manage Settings in the admin interface?
|
193
187
|
|
194
|
-
If you want create an admin interface to editing the Settings, you can try methods in
|
188
|
+
If you want to create an admin interface to editing the Settings, you can try methods in following:
|
195
189
|
|
196
190
|
config/routes.rb
|
197
191
|
|
198
192
|
```rb
|
199
193
|
namespace :admin do
|
200
|
-
|
194
|
+
resource :settings
|
201
195
|
end
|
202
196
|
```
|
203
197
|
|
@@ -209,18 +203,13 @@ module Admin
|
|
209
203
|
class SettingsController < ApplicationController
|
210
204
|
before_action :get_setting, only: [:edit, :update]
|
211
205
|
|
212
|
-
def show
|
213
|
-
end
|
214
|
-
|
215
206
|
def create
|
216
207
|
setting_params.keys.each do |key|
|
217
|
-
next if key.to_s == "site_logo"
|
218
208
|
Setting.send("#{key}=", setting_params[key].strip) unless setting_params[key].nil?
|
219
209
|
end
|
220
|
-
redirect_to
|
210
|
+
redirect_to settings_path, notice: "Setting was successfully updated."
|
221
211
|
end
|
222
212
|
|
223
|
-
|
224
213
|
private
|
225
214
|
def setting_params
|
226
215
|
params.require(:setting).permit(:host, :user_limits, :admin_emails,
|
@@ -256,10 +245,52 @@ app/views/admin/settings/show.html.erb
|
|
256
245
|
<%= f.text_area :notification_options, value: YAML.dump(Setting.notification_options), class: "form-control", style: "height: 180px;" %>
|
257
246
|
<div class="form-text">
|
258
247
|
Use YAML format to config the SMTP_html
|
259
|
-
</
|
248
|
+
</div>
|
249
|
+
</div>
|
250
|
+
|
251
|
+
<div>
|
252
|
+
<%= f.submit 'Update Settings' %>
|
253
|
+
</div>
|
260
254
|
<% end %>
|
261
255
|
```
|
262
256
|
|
263
|
-
##
|
257
|
+
## Scoped Settings
|
258
|
+
|
259
|
+
> 🚨 BREAK CHANGES WARNING:
|
260
|
+
> rails-settings-cached 2.x has redesigned the API, the new version will compatible with the stored setting values by an older version.
|
261
|
+
> When you want to upgrade 2.x, you must read the README again, and follow guides to change your Setting model.
|
262
|
+
> 0.x stable branch: https://github.com/huacnlee/rails-settings-cached/tree/0.x
|
263
|
+
|
264
|
+
- [Backward compatible to support 0.x scoped settings](docs/backward-compatible-to-scoped-settings.md)
|
265
|
+
|
266
|
+
For new project / new user of rails-settings-cached. The [ActiveRecord::AttributeMethods::Serialization](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize) is best choice.
|
267
|
+
|
268
|
+
> This is reason of why rails-settings-cached 2.x removed **Scoped Settings** feature.
|
269
|
+
|
270
|
+
For example:
|
271
|
+
|
272
|
+
We wants a preferences setting for user.
|
273
|
+
|
274
|
+
```rb
|
275
|
+
class User < ActiveRecord::Base
|
276
|
+
serialize :preferences
|
277
|
+
end
|
278
|
+
|
279
|
+
@user = User.new
|
280
|
+
@user.preferences[:receive_emails] = true
|
281
|
+
@user.preferences[:public_email] = true
|
282
|
+
@user.save
|
283
|
+
```
|
284
|
+
|
285
|
+
## Use cases:
|
286
|
+
|
287
|
+
- [ruby-china/homeland](https://github.com/ruby-china/homeland) - master
|
288
|
+
- [forem/forem](https://github.com/forem/forem) - 2.x
|
289
|
+
- [siwapp/siwapp](https://github.com/siwapp/siwapp) - 2.x
|
290
|
+
- [aidewoode/black_candy](https://github.com/aidewoode/black_candy) - 2.x
|
291
|
+
- [thebluedoc/bluedoc](https://github.com/thebluedoc/bluedoc/blob/master/app/models/setting.rb) - 2.x
|
292
|
+
- [tootsuite/mastodon](https://github.com/tootsuite/mastodon) - 0.6.x
|
293
|
+
- [helpyio/helpy](https://github.com/helpyio/helpy) - 0.5.x
|
294
|
+
|
264
295
|
|
265
|
-
|
296
|
+
And more than [1K repositories](https://github.com/huacnlee/rails-settings-cached/network/dependents) used.
|
@@ -3,14 +3,15 @@
|
|
3
3
|
require "rails/generators"
|
4
4
|
require "rails/generators/migration"
|
5
5
|
|
6
|
-
module
|
6
|
+
module RailsSettings
|
7
7
|
class InstallGenerator < Rails::Generators::NamedBase
|
8
|
+
namespace "settings:install"
|
8
9
|
desc "Generate RailsSettings files."
|
9
10
|
include Rails::Generators::Migration
|
10
11
|
|
11
12
|
argument :name, type: :string, default: "setting"
|
12
13
|
|
13
|
-
source_root File.expand_path("
|
14
|
+
source_root File.expand_path("templates", __dir__)
|
14
15
|
|
15
16
|
@@migrations = false
|
16
17
|
|
@@ -32,12 +33,16 @@ module Settings
|
|
32
33
|
migration_template "migration.rb", "db/migrate/create_settings.rb", migration_version: migration_version
|
33
34
|
end
|
34
35
|
|
35
|
-
def
|
36
|
-
Rails
|
36
|
+
def rails_version_major
|
37
|
+
Rails::VERSION::MAJOR
|
38
|
+
end
|
39
|
+
|
40
|
+
def rails_version_minor
|
41
|
+
Rails::VERSION::MINOR
|
37
42
|
end
|
38
43
|
|
39
44
|
def migration_version
|
40
|
-
"[#{
|
45
|
+
"[#{rails_version_major}.#{rails_version_minor}]" if rails_version_major >= 5
|
41
46
|
end
|
42
47
|
end
|
43
48
|
end
|
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 = /[\
|
9
|
+
SEPARATOR_REGEXP = /[\n,;]+/
|
10
10
|
self.table_name = table_name_prefix + "settings"
|
11
11
|
|
12
12
|
# get the value field, YAML decoded
|
@@ -30,7 +30,9 @@ module RailsSettings
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def field(key, **opts)
|
33
|
-
|
33
|
+
@keys ||= []
|
34
|
+
@keys << key.to_s
|
35
|
+
_define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator])
|
34
36
|
end
|
35
37
|
|
36
38
|
def cache_prefix(&block)
|
@@ -43,14 +45,19 @@ module RailsSettings
|
|
43
45
|
scope.join("/")
|
44
46
|
end
|
45
47
|
|
48
|
+
def keys
|
49
|
+
@keys
|
50
|
+
end
|
51
|
+
|
46
52
|
private
|
47
|
-
|
53
|
+
|
54
|
+
def _define_field(key, default: nil, type: :string, readonly: false, separator: nil)
|
48
55
|
if readonly
|
49
|
-
|
50
|
-
self.send(:
|
56
|
+
define_singleton_method(key) do
|
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
|
54
61
|
val = self.send(:_value_of, key)
|
55
62
|
result = nil
|
56
63
|
if !val.nil?
|
@@ -60,16 +67,16 @@ 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
|
67
74
|
|
68
|
-
|
75
|
+
define_singleton_method("#{key}=") do |value|
|
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!
|
@@ -79,26 +86,30 @@ module RailsSettings
|
|
79
86
|
end
|
80
87
|
|
81
88
|
if type == :boolean
|
82
|
-
|
89
|
+
define_singleton_method("#{key}?") do
|
83
90
|
self.send(key)
|
84
91
|
end
|
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
|
@@ -115,7 +126,7 @@ module RailsSettings
|
|
115
126
|
end
|
116
127
|
|
117
128
|
def _all_settings
|
118
|
-
raise "You
|
129
|
+
raise "You cannot use settings before Rails initialize." unless rails_initialized?
|
119
130
|
RequestStore.store[:rails_settings_all_settings] ||= begin
|
120
131
|
Rails.cache.fetch(self.cache_key, expires_in: 1.week) do
|
121
132
|
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.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-10-14 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
|
- - ">="
|
@@ -130,9 +130,10 @@ files:
|
|
130
130
|
- lib/rails-settings/railtie.rb
|
131
131
|
- lib/rails-settings/version.rb
|
132
132
|
homepage: https://github.com/huacnlee/rails-settings-cached
|
133
|
-
licenses:
|
133
|
+
licenses:
|
134
|
+
- MIT
|
134
135
|
metadata: {}
|
135
|
-
post_install_message:
|
136
|
+
post_install_message:
|
136
137
|
rdoc_options: []
|
137
138
|
require_paths:
|
138
139
|
- lib
|
@@ -147,8 +148,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
148
|
- !ruby/object:Gem::Version
|
148
149
|
version: '0'
|
149
150
|
requirements: []
|
150
|
-
rubygems_version: 3.0.
|
151
|
-
signing_key:
|
151
|
+
rubygems_version: 3.0.3
|
152
|
+
signing_key:
|
152
153
|
specification_version: 4
|
153
154
|
summary: Settings plugin for Rails that makes managing a table of global keys.
|
154
155
|
test_files: []
|