rails-settings-cached 2.3.1 → 2.4.0

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: 4a7bc508cf14fd62288de455806b11c39f5894fc0830ea89523e9a4f9ca3d09b
4
- data.tar.gz: 9d4affefb02823cf0a73344c7356f2e416d6cb6fb78dc0e25ad94a21cb4b1646
3
+ metadata.gz: 603b7101883a17d76bfb33682ce421b7259101bfc7d3b0fea450d02466adcb03
4
+ data.tar.gz: '088348b905fba8727569b052468ca194c74fcde740976e0d4cfe6b780063144f'
5
5
  SHA512:
6
- metadata.gz: c023a86676cd5f41c5479e8c671df1946076de4167f8fe40c437e69d69e27d1d46a5d5e5a97ad35f950f6b477a0c14ab6de0c4e4fbd3210df46d0c091ac6c312
7
- data.tar.gz: 16ed2df3231c5379847c25c97f3950a8692ce51a36d5b1ffc7e1973a11786de820f8264e628f3f7df5e6689018a91e3849a4749c5757eb7f1b076a5b041f538b
6
+ metadata.gz: eeaed942036ec6c70fbd0f8a687000032e87cbef17324eca6329c32e1f7b69b6f38a7d295ec00441ec1fd1601f53c8cbe162df33e0f5d4f518658f6cb3236835
7
+ data.tar.gz: 13f87d6cac9234fcf48b50b3e67723aac4977d4a9e8fb71528bf8d12fd29eb27372d9b5705a496f694550ad988dba613f9dc3bf069fa699a910ebd1f4c5b8506
data/README.md CHANGED
@@ -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+` we allows you to 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
+ }
173
198
  end
174
199
  ```
175
200
 
176
- ### Caching flow:
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
210
+ end
211
+ ```
212
+
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
@@ -319,5 +353,4 @@ end
319
353
  - [tootsuite/mastodon](https://github.com/tootsuite/mastodon) - 0.6.x
320
354
  - [helpyio/helpy](https://github.com/helpyio/helpy) - 0.5.x
321
355
 
322
-
323
356
  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_store"
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
+ RequestStore.reset
29
27
  Rails.cache.delete(cache_key)
30
28
  end
31
29
 
@@ -121,15 +119,15 @@ module RailsSettings
121
119
  when :hash
122
120
  value = begin
123
121
  begin
124
- YAML.load(value).to_h
122
+ YAML.load(value).to_h
125
123
  rescue StandardError
126
124
  eval(value).to_h
127
- end
125
+ end
128
126
  rescue StandardError
129
127
  {}
130
128
  end
131
129
  value.deep_stringify_keys!
132
- value
130
+ ActiveSupport::HashWithIndifferentAccess.new(value)
133
131
  when :integer
134
132
  value.to_i
135
133
  when :float
@@ -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
+ RequestStore.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,8 @@
1
+ module RailsSettings
2
+ # For storage all settings in Current, it will reset after per request completed.
3
+ # Base on ActiveSupport::CurrentAttributes
4
+ # https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
5
+ class RequestStore < ActiveSupport::CurrentAttributes
6
+ attribute :settings
7
+ end
8
+ end
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.3.1"
6
+ "2.4.0"
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.1
4
+ version: 2.4.0
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-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -25,13 +25,13 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.2.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
  - - ">="
@@ -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_store.rb
131
132
  - lib/rails-settings/version.rb
132
133
  homepage: https://github.com/huacnlee/rails-settings-cached
133
134
  licenses:
@@ -148,7 +149,7 @@ 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
155
  summary: Settings plugin for Rails that makes managing a table of global keys.