rails-settings-cached 2.3.2 → 2.4.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 +58 -22
- data/lib/rails-settings-cached.rb +1 -0
- data/lib/rails-settings/base.rb +13 -7
- data/lib/rails-settings/request_cache.rb +30 -0
- data/lib/rails-settings/version.rb +1 -1
- metadata +16 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11dbf6161605ff3aed324a9ac29600d47918cd5fad1b8a5abfadaeddd9636c57
|
4
|
+
data.tar.gz: 8056da8c1b7441dd6fc78117013b06c0d67b53bfb06497d1e2343b1417503b95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61579ae6068bdd643c4741ac07855c0802220f11be06709910efae5f653dc69db7d7b039cb265bc4229a5121aef90ee9de92d6da700aea87cec33bfb9c4800f3
|
7
|
+
data.tar.gz: 2fbd8d62d15ac420abc0ac96ca2aea53eb6d78c9072a1f19fa942f86fd89a8253e829ec827251909dc2cd8f2082e65e890737ba7588d4cf960ebad85fdc76269
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Rails Settings Cached
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
that uses simple ActiveRecord like methods for manipulation. Keep track of any global
|
6
|
-
|
7
|
-
of object. Strings, numbers, arrays, or any object.
|
3
|
+
The best solution for store global settings in Rails applications.
|
4
|
+
|
5
|
+
This gem will 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 app.
|
6
|
+
|
7
|
+
You can store any kind of object. Strings, numbers, arrays, booleans, or any object.
|
8
8
|
|
9
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)
|
10
10
|
|
@@ -53,7 +53,7 @@ class Setting < RailsSettings::Base
|
|
53
53
|
end
|
54
54
|
```
|
55
55
|
|
56
|
-
You must use `field` method to statement the setting keys, otherwise you can't use it.
|
56
|
+
You must use the `field` method to statement the setting keys, otherwise you can't use it.
|
57
57
|
|
58
58
|
Now just put that migration in the database with:
|
59
59
|
|
@@ -126,7 +126,6 @@ irb > Setting.notification_options
|
|
126
126
|
}
|
127
127
|
```
|
128
128
|
|
129
|
-
|
130
129
|
### Get defined fields
|
131
130
|
|
132
131
|
> version 2.3+
|
@@ -151,9 +150,11 @@ Setting.get_field("app_name")
|
|
151
150
|
=> { key: "app_name", type: :string, default: "Rails Settings", readonly: false }
|
152
151
|
```
|
153
152
|
|
154
|
-
##
|
153
|
+
## Use Setting in Rails initializing:
|
155
154
|
|
156
|
-
|
155
|
+
In `version 2.3+` you can use Setting before Rails is initialized.
|
156
|
+
|
157
|
+
For example `config/initializers/devise.rb`
|
157
158
|
|
158
159
|
```rb
|
159
160
|
Devise.setup do |config|
|
@@ -163,17 +164,53 @@ Devise.setup do |config|
|
|
163
164
|
end
|
164
165
|
```
|
165
166
|
|
166
|
-
|
167
|
+
```rb
|
168
|
+
class Setting < RailsSettings::Base
|
169
|
+
field :omniauth_google_client_id, default: ENV["OMNIAUTH_GOOGLE_CLIENT_ID"]
|
170
|
+
field :omniauth_google_client_secret, default: ENV["OMNIAUTH_GOOGLE_CLIENT_SECRET"]
|
171
|
+
end
|
172
|
+
```
|
173
|
+
|
174
|
+
## Readonly field
|
175
|
+
|
176
|
+
You may also want use Setting before Rails initialize:
|
177
|
+
|
178
|
+
```
|
179
|
+
config/environments/*.rb
|
180
|
+
```
|
181
|
+
|
182
|
+
If you want do that do that, the setting field must has `readonly: true`.
|
183
|
+
|
184
|
+
For example:
|
167
185
|
|
168
186
|
```rb
|
169
187
|
class Setting < RailsSettings::Base
|
170
|
-
|
171
|
-
field :
|
172
|
-
|
188
|
+
field :mailer_provider, default: (ENV["mailer_provider"] || "smtp"), readonly: true
|
189
|
+
field :mailer_options, type: :hash, readonly: true, default: {
|
190
|
+
address: ENV["mailer_options.address"],
|
191
|
+
port: ENV["mailer_options.port"],
|
192
|
+
domain: ENV["mailer_options.domain"],
|
193
|
+
user_name: ENV["mailer_options.user_name"],
|
194
|
+
password: ENV["mailer_options.password"],
|
195
|
+
authentication: ENV["mailer_options.authentication"] || "login",
|
196
|
+
enable_starttls_auto: ENV["mailer_options.enable_starttls_auto"]
|
197
|
+
}
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
config/environments/production.rb
|
202
|
+
|
203
|
+
```rb
|
204
|
+
# You must require_relative directly in Rails 6.1+ in config/environments/production.rb
|
205
|
+
require_relative "../../app/models/setting"
|
206
|
+
|
207
|
+
Rails.application.configure do
|
208
|
+
config.action_mailer.delivery_method = :smtp
|
209
|
+
config.action_mailer.smtp_settings = Setting.mailer_options.deep_symbolize_keys
|
173
210
|
end
|
174
211
|
```
|
175
212
|
|
176
|
-
|
213
|
+
## Caching flow:
|
177
214
|
|
178
215
|
```
|
179
216
|
Setting.host -> Check Cache -> Exist - Get value of key for cache -> Return
|
@@ -183,7 +220,7 @@ Setting.host -> Check Cache -> Exist - Get value of key for cache -> Return
|
|
183
220
|
Return default value or nil
|
184
221
|
```
|
185
222
|
|
186
|
-
In each Setting keys call, we will load the cache/db and save in [
|
223
|
+
In each Setting keys call, we will load the cache/db and save in [ActiveSupport::CurrentAttributes](https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html) to avoid hit cache/db.
|
187
224
|
|
188
225
|
Each key update will expire the cache, so do not add some frequent update key.
|
189
226
|
|
@@ -208,7 +245,7 @@ class ActiveSupport::TestCase
|
|
208
245
|
end
|
209
246
|
```
|
210
247
|
|
211
|
-
|
248
|
+
---
|
212
249
|
|
213
250
|
## How to manage Settings in the admin interface?
|
214
251
|
|
@@ -222,19 +259,16 @@ namespace :admin do
|
|
222
259
|
end
|
223
260
|
```
|
224
261
|
|
225
|
-
|
226
262
|
app/controllers/admin/settings_controller.rb
|
227
263
|
|
228
264
|
```rb
|
229
265
|
module Admin
|
230
266
|
class SettingsController < ApplicationController
|
231
|
-
before_action :get_setting, only: [:edit, :update]
|
232
|
-
|
233
267
|
def create
|
234
268
|
setting_params.keys.each do |key|
|
235
269
|
Setting.send("#{key}=", setting_params[key].strip) unless setting_params[key].nil?
|
236
270
|
end
|
237
|
-
redirect_to
|
271
|
+
redirect_to admin_settings_path, notice: "Setting was successfully updated."
|
238
272
|
end
|
239
273
|
|
240
274
|
private
|
@@ -315,9 +349,11 @@ end
|
|
315
349
|
- [forem/forem](https://github.com/forem/forem) - 2.x
|
316
350
|
- [siwapp/siwapp](https://github.com/siwapp/siwapp) - 2.x
|
317
351
|
- [aidewoode/black_candy](https://github.com/aidewoode/black_candy) - 2.x
|
318
|
-
- [
|
352
|
+
- [huacnlee/bluedoc](https://github.com/huacnlee/bluedoc) - 2.x
|
353
|
+
- [getzealot/zealot](https://github.com/getzealot/zealot) - 2.x
|
354
|
+
- [kaishuu0123/rebacklogs](https://github.com/kaishuu0123/rebacklogs) - 2.x
|
319
355
|
- [tootsuite/mastodon](https://github.com/tootsuite/mastodon) - 0.6.x
|
320
356
|
- [helpyio/helpy](https://github.com/helpyio/helpy) - 0.5.x
|
321
|
-
|
357
|
+
- [daqing/rabel](https://github.com/daqing/rabel) - 0.4.x
|
322
358
|
|
323
359
|
And more than [1K repositories](https://github.com/huacnlee/rails-settings-cached/network/dependents) used.
|
data/lib/rails-settings/base.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "request_store"
|
4
|
-
|
5
3
|
module RailsSettings
|
6
4
|
class Base < ActiveRecord::Base
|
7
5
|
class SettingNotFound < RuntimeError; end
|
@@ -25,7 +23,7 @@ module RailsSettings
|
|
25
23
|
|
26
24
|
class << self
|
27
25
|
def clear_cache
|
28
|
-
|
26
|
+
RequestCache.reset
|
29
27
|
Rails.cache.delete(cache_key)
|
30
28
|
end
|
31
29
|
|
@@ -142,19 +140,27 @@ module RailsSettings
|
|
142
140
|
end
|
143
141
|
|
144
142
|
def _value_of(var_name)
|
145
|
-
|
143
|
+
unless _table_exists?
|
144
|
+
# Fallback to default value if table was not ready (before migrate)
|
145
|
+
puts "WARNING: table: \"#{table_name}\" does not exist or not database connection, `#{name}.#{var_name}` fallback to returns the default value."
|
146
|
+
return nil
|
147
|
+
end
|
146
148
|
|
147
149
|
_all_settings[var_name.to_s]
|
148
150
|
end
|
149
151
|
|
152
|
+
def _table_exists?
|
153
|
+
table_exists?
|
154
|
+
rescue => e
|
155
|
+
false
|
156
|
+
end
|
157
|
+
|
150
158
|
def rails_initialized?
|
151
159
|
Rails.application&.initialized?
|
152
160
|
end
|
153
161
|
|
154
162
|
def _all_settings
|
155
|
-
|
156
|
-
|
157
|
-
RequestStore.store[:rails_settings_all_settings] ||= begin
|
163
|
+
RequestCache.settings ||= begin
|
158
164
|
Rails.cache.fetch(cache_key, expires_in: 1.week) do
|
159
165
|
vars = unscoped.select("var, value")
|
160
166
|
result = {}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module RailsSettings
|
2
|
+
if defined? ActiveSupport::CurrentAttributes
|
3
|
+
# For storage all settings in Current, it will reset after per request completed.
|
4
|
+
# Base on ActiveSupport::CurrentAttributes
|
5
|
+
# https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
|
6
|
+
class RequestCache < ActiveSupport::CurrentAttributes
|
7
|
+
attribute :settings
|
8
|
+
end
|
9
|
+
else
|
10
|
+
# https://github.com/steveklabnik/request_store
|
11
|
+
# For Rails 5.0
|
12
|
+
require "request_store"
|
13
|
+
|
14
|
+
class RequestCache
|
15
|
+
class << self
|
16
|
+
def reset
|
17
|
+
self.settings = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def settings
|
21
|
+
RequestStore.store[:rails_settings_all_settings]
|
22
|
+
end
|
23
|
+
|
24
|
+
def settings=(val)
|
25
|
+
RequestStore.store[:rails_settings_all_settings]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
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.4.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: 2020-
|
11
|
+
date: 2020-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,22 +16,22 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 5.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 5.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: codecov
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
|
-
type: :
|
34
|
+
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
@@ -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: minitest
|
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: pg
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -108,11 +108,11 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
-
description: "\n
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
111
|
+
description: "\n The best solution for store global settings in Rails applications.\n\n
|
112
|
+
\ This gem will managing a table of а global key, value pairs easy. Think of it
|
113
|
+
like a \n global Hash stored in your database, that uses simple ActiveRecord like
|
114
|
+
methods for manipulation.\n\n Keep track of any global setting that you dont want
|
115
|
+
to hard code into your rails app.\n You can store any kind of object. Strings,
|
116
116
|
numbers, arrays, or any object.\n "
|
117
117
|
email: huacnlee@gmail.com
|
118
118
|
executables: []
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- lib/rails-settings-cached.rb
|
129
129
|
- lib/rails-settings/base.rb
|
130
130
|
- lib/rails-settings/railtie.rb
|
131
|
+
- lib/rails-settings/request_cache.rb
|
131
132
|
- lib/rails-settings/version.rb
|
132
133
|
homepage: https://github.com/huacnlee/rails-settings-cached
|
133
134
|
licenses:
|
@@ -148,8 +149,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
149
|
- !ruby/object:Gem::Version
|
149
150
|
version: '0'
|
150
151
|
requirements: []
|
151
|
-
rubygems_version: 3.
|
152
|
+
rubygems_version: 3.1.2
|
152
153
|
signing_key:
|
153
154
|
specification_version: 4
|
154
|
-
summary:
|
155
|
+
summary: The best solution for store global settings in Rails applications.
|
155
156
|
test_files: []
|