rails-settings-cached 2.6.0 → 2.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +47 -19
- data/lib/rails-settings/base.rb +24 -8
- data/lib/rails-settings/middleware.rb +14 -0
- data/lib/rails-settings/railtie.rb +4 -0
- data/lib/rails-settings/request_cache.rb +30 -1
- data/lib/rails-settings/version.rb +1 -1
- data/lib/rails-settings-cached.rb +1 -0
- metadata +5 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0405e5cfca63f64b56ba7e95817c52803184afc191c837ef312df41e368dd9e4
|
4
|
+
data.tar.gz: 54a968cf325c616142394892943ffdd6a9893234acc98c381122f878696e1547
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 171e47f07b71053d5b1218118992b3e5723f3118abe5a2b31a7b799f05354fac3e4ec28734ddf33790dead573948f2b59ff811cc0edec18ee688dedbd3d724f0
|
7
|
+
data.tar.gz: 5f8aa09f8980dfb7b100aaa1ac2c5fdb65185511c7fb21e96343e29b4d63280fc5e2ef1ce5a930e69dcd543ce5bd0c4bb0fb15185db72ca3c7276b09ee27bf1e
|
data/README.md
CHANGED
@@ -2,11 +2,9 @@
|
|
2
2
|
|
3
3
|
The best solution for store global settings in Rails applications.
|
4
4
|
|
5
|
-
This gem will make managing a table of а global key, value pairs easy. Think of it like a global Hash stored in your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you don't want to hard code into your
|
5
|
+
This gem will make managing a table of а global key, value pairs easy. Think of it like a global Hash stored in your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you don't want to hard code into your Rails application.
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
[![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![build](https://github.com/huacnlee/rails-settings-cached/workflows/build/badge.svg)](https://github.com/huacnlee/rails-settings-cached/actions?query=workflow%3Abuild) [![codecov.io](https://codecov.io/github/huacnlee/rails-settings-cached/coverage.svg?branch=master)](https://codecov.io/github/huacnlee/rails-settings-cached?branch=master)
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![build](https://github.com/huacnlee/rails-settings-cached/workflows/build/badge.svg)](https://github.com/huacnlee/rails-settings-cached/actions?query=workflow%3Abuild)
|
10
8
|
|
11
9
|
## Installation
|
12
10
|
|
@@ -30,17 +28,24 @@ You will get `app/models/setting.rb`
|
|
30
28
|
```rb
|
31
29
|
class Setting < RailsSettings::Base
|
32
30
|
# cache_prefix { "v1" }
|
33
|
-
field :app_name, default: "Rails Settings", validates: { presence: true, length: { in: 2..20 } }
|
34
|
-
field :host, default: "http://example.com", readonly: true
|
35
|
-
field :default_locale, default: "zh-CN", validates: { presence: true, inclusion: { in: %w[zh-CN en jp] } }
|
36
|
-
field :readonly_item, type: :integer, default: 100, readonly: true
|
37
|
-
field :user_limits, type: :integer, default: 20
|
38
|
-
field :exchange_rate, type: :float, default: 0.123
|
39
|
-
field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
|
40
|
-
field :captcha_enable, type: :boolean, default: true
|
41
31
|
|
42
|
-
|
43
|
-
|
32
|
+
scope :application do
|
33
|
+
field :app_name, default: "Rails Settings", validates: { presence: true, length: { in: 2..20 } }
|
34
|
+
field :host, default: "http://example.com", readonly: true
|
35
|
+
field :default_locale, default: "zh-CN", validates: { presence: true, inclusion: { in: %w[zh-CN en jp] } }, option_values: %w[en zh-CN jp], help_text: "Bla bla ..."
|
36
|
+
field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
|
37
|
+
|
38
|
+
# lambda default value
|
39
|
+
field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }, validates: { length: { maximum: 255 } }
|
40
|
+
# Override array separator, default: /[\n,]/ split with \n or comma.
|
41
|
+
field :tips, type: :array, separator: /[\n]+/
|
42
|
+
end
|
43
|
+
|
44
|
+
scope :limits do
|
45
|
+
field :user_limits, type: :integer, default: 20
|
46
|
+
field :exchange_rate, type: :float, default: 0.123
|
47
|
+
field :captcha_enable, type: :boolean, default: true
|
48
|
+
end
|
44
49
|
|
45
50
|
field :notification_options, type: :hash, default: {
|
46
51
|
send_all: true,
|
@@ -48,13 +53,14 @@ class Setting < RailsSettings::Base
|
|
48
53
|
sender_email: "foo@bar.com"
|
49
54
|
}
|
50
55
|
|
51
|
-
|
52
|
-
field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }, validates: { length: { maximum: 255 } }
|
56
|
+
field :readonly_item, type: :integer, default: 100, readonly: true
|
53
57
|
end
|
54
58
|
```
|
55
59
|
|
56
60
|
You must use the `field` method to statement the setting keys, otherwise you can't use it.
|
57
61
|
|
62
|
+
The `scope` method allows you to group the keys for admin UI.
|
63
|
+
|
58
64
|
Now just put that migration in the database with:
|
59
65
|
|
60
66
|
```bash
|
@@ -143,11 +149,29 @@ Settng.editable_keys
|
|
143
149
|
Setting.readonly_keys
|
144
150
|
=> ["host", "readonly_item"]
|
145
151
|
|
146
|
-
# Get
|
152
|
+
# Get field
|
147
153
|
Setting.get_field("host")
|
148
|
-
=> { key: "host", type: :string, default: "http://example.com", readonly: true }
|
154
|
+
=> { scope: :application, key: "host", type: :string, default: "http://example.com", readonly: true }
|
149
155
|
Setting.get_field("app_name")
|
150
|
-
=> { key: "app_name", type: :string, default: "Rails Settings", readonly: false }
|
156
|
+
=> { scope: :application, key: "app_name", type: :string, default: "Rails Settings", readonly: false }
|
157
|
+
Setting.get_field(:user_limits)
|
158
|
+
=> { scope: :limits, key: "user_limits", type: :integer, default: 20, readonly: false }
|
159
|
+
# Get field options
|
160
|
+
Setting.get_field("default_locale")[:options]
|
161
|
+
=> { option_values: %w[en zh-CN jp], help_text: "Bla bla ..." }
|
162
|
+
```
|
163
|
+
|
164
|
+
#### Get All defined fields
|
165
|
+
|
166
|
+
> version 2.7.0+
|
167
|
+
|
168
|
+
You can use `defined_fields` method to get all defined fields in Setting.
|
169
|
+
|
170
|
+
```rb
|
171
|
+
# Get editable fields and group by scope
|
172
|
+
editable_fields = Setting.defined_fields
|
173
|
+
.select { |field| !field[:readonly] }
|
174
|
+
.group_by { |field| field[:scope] }
|
151
175
|
```
|
152
176
|
|
153
177
|
## Validations
|
@@ -257,6 +281,10 @@ Rails.application.configure do
|
|
257
281
|
end
|
258
282
|
```
|
259
283
|
|
284
|
+
TIP: You also can follow this file to rewrite ActionMailer's `mail` method for configuration Mail options from Setting after Rails booted.
|
285
|
+
|
286
|
+
https://github.com/ruby-china/homeland/blob/main/app/mailers/application_mailer.rb#L19
|
287
|
+
|
260
288
|
## Caching flow:
|
261
289
|
|
262
290
|
```
|
data/lib/rails-settings/base.rb
CHANGED
@@ -34,8 +34,20 @@ module RailsSettings
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def field(key, **opts)
|
37
|
-
_define_field(key,
|
38
|
-
|
37
|
+
_define_field(key, **opts)
|
38
|
+
end
|
39
|
+
|
40
|
+
alias_method :_rails_scope, :scope
|
41
|
+
def scope(*args, &block)
|
42
|
+
name = args.shift
|
43
|
+
body = args.shift
|
44
|
+
if body.respond_to?(:call)
|
45
|
+
return _rails_scope(name, body, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
@scope = name.to_sym
|
49
|
+
yield block
|
50
|
+
@scope = nil
|
39
51
|
end
|
40
52
|
|
41
53
|
def get_field(key)
|
@@ -47,9 +59,9 @@ module RailsSettings
|
|
47
59
|
end
|
48
60
|
|
49
61
|
def cache_key
|
50
|
-
|
51
|
-
|
52
|
-
|
62
|
+
key_parts = ["rails-settings-cached"]
|
63
|
+
key_parts << @cache_prefix.call if @cache_prefix
|
64
|
+
key_parts.join("/")
|
53
65
|
end
|
54
66
|
|
55
67
|
def keys
|
@@ -64,19 +76,23 @@ module RailsSettings
|
|
64
76
|
@defined_fields.select { |field| field[:readonly] }.map { |field| field[:key] }
|
65
77
|
end
|
66
78
|
|
79
|
+
attr_reader :defined_fields
|
80
|
+
|
67
81
|
private
|
68
82
|
|
69
|
-
def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil)
|
83
|
+
def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil, **opts)
|
70
84
|
key = key.to_s
|
71
85
|
|
72
86
|
raise ProcetedKeyError.new(key) if PROTECTED_KEYS.include?(key)
|
73
87
|
|
74
88
|
@defined_fields ||= []
|
75
89
|
@defined_fields << {
|
90
|
+
scope: @scope,
|
76
91
|
key: key,
|
77
92
|
default: default,
|
78
93
|
type: type || :string,
|
79
|
-
readonly: readonly.nil? ? false : readonly
|
94
|
+
readonly: readonly.nil? ? false : readonly,
|
95
|
+
options: opts
|
80
96
|
}
|
81
97
|
|
82
98
|
if readonly
|
@@ -184,7 +200,7 @@ module RailsSettings
|
|
184
200
|
end
|
185
201
|
|
186
202
|
def _all_settings
|
187
|
-
RequestCache.
|
203
|
+
RequestCache.all_settings ||= Rails.cache.fetch(cache_key, expires_in: 1.week) do
|
188
204
|
vars = unscoped.select("var, value")
|
189
205
|
result = {}
|
190
206
|
vars.each { |record| result[record.var] = record.value }
|
@@ -5,5 +5,9 @@ module RailsSettings
|
|
5
5
|
initializer "rails_settings.active_record.initialization" do
|
6
6
|
RailsSettings::Base.after_commit :clear_cache, on: %i[create update destroy]
|
7
7
|
end
|
8
|
+
|
9
|
+
initializer "rails_settings.configure_rails_initialization" do |app|
|
10
|
+
app.middleware.use RailsSettings::Middleware
|
11
|
+
end
|
8
12
|
end
|
9
13
|
end
|
@@ -1,9 +1,36 @@
|
|
1
1
|
module RailsSettings
|
2
|
+
module RequestCacheGetter
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def enable!
|
7
|
+
Thread.current[:rails_settings_request_cache_enable] = true
|
8
|
+
end
|
9
|
+
|
10
|
+
def disable!
|
11
|
+
Thread.current[:rails_settings_request_cache_enable] = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def enabled?
|
15
|
+
Thread.current[:rails_settings_request_cache_enable]
|
16
|
+
end
|
17
|
+
|
18
|
+
def all_settings
|
19
|
+
enabled? ? settings : nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def all_settings=(val)
|
23
|
+
self.settings = val
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
2
28
|
if defined? ActiveSupport::CurrentAttributes
|
3
29
|
# For storage all settings in Current, it will reset after per request completed.
|
4
30
|
# Base on ActiveSupport::CurrentAttributes
|
5
31
|
# https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
|
6
32
|
class RequestCache < ActiveSupport::CurrentAttributes
|
33
|
+
include RequestCacheGetter
|
7
34
|
attribute :settings
|
8
35
|
end
|
9
36
|
else
|
@@ -12,6 +39,8 @@ module RailsSettings
|
|
12
39
|
require "request_store"
|
13
40
|
|
14
41
|
class RequestCache
|
42
|
+
include RequestCacheGetter
|
43
|
+
|
15
44
|
class << self
|
16
45
|
def reset
|
17
46
|
self.settings = nil
|
@@ -22,7 +51,7 @@ module RailsSettings
|
|
22
51
|
end
|
23
52
|
|
24
53
|
def settings=(val)
|
25
|
-
RequestStore.store[:rails_settings_all_settings]
|
54
|
+
RequestStore.store[:rails_settings_all_settings] = val
|
26
55
|
end
|
27
56
|
end
|
28
57
|
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.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 5.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: pg
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: pg
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: rubocop
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,20 +66,6 @@ dependencies:
|
|
80
66
|
- - ">="
|
81
67
|
- !ruby/object:Gem::Version
|
82
68
|
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: simplecov
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
69
|
- !ruby/object:Gem::Dependency
|
98
70
|
name: sqlite3
|
99
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -127,6 +99,7 @@ files:
|
|
127
99
|
- lib/generators/settings/templates/model.rb
|
128
100
|
- lib/rails-settings-cached.rb
|
129
101
|
- lib/rails-settings/base.rb
|
102
|
+
- lib/rails-settings/middleware.rb
|
130
103
|
- lib/rails-settings/railtie.rb
|
131
104
|
- lib/rails-settings/request_cache.rb
|
132
105
|
- lib/rails-settings/version.rb
|
@@ -149,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
122
|
- !ruby/object:Gem::Version
|
150
123
|
version: '0'
|
151
124
|
requirements: []
|
152
|
-
rubygems_version: 3.
|
125
|
+
rubygems_version: 3.1.4
|
153
126
|
signing_key:
|
154
127
|
specification_version: 4
|
155
128
|
summary: The best solution for store global settings in Rails applications.
|