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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be7b05e634deda4d1a7b720528431277c7dcd57cace57181a84a5b02115ed412
4
- data.tar.gz: b7dd8a8e4aec0dc80240bbcc4ecbb747896c7fd0495c0c7fc9820dd92191ec5c
3
+ metadata.gz: 11dbf6161605ff3aed324a9ac29600d47918cd5fad1b8a5abfadaeddd9636c57
4
+ data.tar.gz: 8056da8c1b7441dd6fc78117013b06c0d67b53bfb06497d1e2343b1417503b95
5
5
  SHA512:
6
- metadata.gz: 70b0c0010ee5839c6bc85e7376649bbdbdec38e492af0d57559a32df4ae658125a180f021802f0b14cee43dffa8e6daba71df4f262b06ae6f4e705dc0474d46c
7
- data.tar.gz: 277ba14e3774bd321dd038e315496294269d008d9e07e57e42dd9f8d398e185775754ef65e4ebe257178b9f74c826f660c017ced91936da7f25153d0c59790ad
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
- 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,
5
- that uses simple ActiveRecord like methods for manipulation. Keep track of any global
6
- setting that you don't want to hard code into your rails app. You can store any kind
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
- ## Readonly field
153
+ ## Use Setting in Rails initializing:
155
154
 
156
- Sometimes you may need to use Setting before Rails is initialized, for example `config/devise.rb`
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
- In this case, you must define the `readonly` field:
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
- # cache_prefix { "v1" }
171
- field :omniauth_google_client_id, default: ENV["OMNIAUTH_GOOGLE_CLIENT_ID"], readonly: true
172
- field :omniauth_google_client_secret, default: ENV["OMNIAUTH_GOOGLE_CLIENT_SECRET"], readonly: true
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
- ### Caching flow:
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 [RequestStore](https://github.com/steveklabnik/request_store) to avoid hit cache/db.
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 settings_path, notice: "Setting was successfully updated."
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
- - [thebluedoc/bluedoc](https://github.com/thebluedoc/bluedoc/blob/master/app/models/setting.rb) - 2.x
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.
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "rails-settings/base"
4
+ require_relative "rails-settings/request_cache"
4
5
  require_relative "rails-settings/railtie"
5
6
  require_relative "rails-settings/version"
6
7
 
@@ -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
- RequestStore.store[:rails_settings_all_settings] = nil
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
- raise "#{table_name} does not exist." unless table_exists?
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
- raise "You cannot use settings before Rails initialize." unless rails_initialized?
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
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.3.2"
6
+ "2.4.1"
7
7
  end
8
8
  end
9
9
  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.3.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-10-15 00:00:00.000000000 Z
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: 4.2.0
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: 4.2.0
26
+ version: 5.0.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: request_store
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: :runtime
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: codecov
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: minitest
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 This is improved from rails-settings, added caching.\n Settings
112
- plugin for Rails that makes managing a table of global key,\n value pairs easy.
113
- Think of it like a global Hash stored in you database,\n that uses simple ActiveRecord
114
- like methods for manipulation.\n\n Keep track of any global setting that you dont
115
- want to hard code into your rails app.\n You can store any kind of object. Strings,
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.0.3
152
+ rubygems_version: 3.1.2
152
153
  signing_key:
153
154
  specification_version: 4
154
- summary: Settings plugin for Rails that makes managing a table of global keys.
155
+ summary: The best solution for store global settings in Rails applications.
155
156
  test_files: []