rails-settings-cached 2.7.0 → 2.8.2

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: d45081eacc51de459cd0bc48469430474cd561386e924b40996bd5f66c1299e5
4
- data.tar.gz: 485e2df525a6d3b66dfed0c0b5935aa8134b13ae0b0528d1fae536e5b19e4ae2
3
+ metadata.gz: 2cdcbd12a2a3eb29491036b8c865a18bd098174324981e85498217bc708d2770
4
+ data.tar.gz: 6abfb107f6453ad4d221c6accc4a5c04a78da731cdb39cef1a4fc72149d5e63a
5
5
  SHA512:
6
- metadata.gz: 6651da35fec5737f76192ec01f0980ea45a85c69efb768c22edbce29b490f29dfa2498798b5af6d45a126955a3fe72bf2fa1ae76bd249237b83f408f7bba72c4
7
- data.tar.gz: b96670e501b9c5aff237b02fd2da2560b654dd2835322b2e64e28679e3edf646ee940f2f2ffe69bfa7d851732ac4844bb47b01e292f4ef4b5f04e587977f8c21
6
+ metadata.gz: 4c7acbc5ff62818c2b526301748019926c33611db3ee8b77a7b6d4d14b582874483cd401eadffc9580083f8c4bcf363893851e4a1b6eeb90ddb0266b7b52f2ab
7
+ data.tar.gz: 5a31aed5bcda620d0540e8c0f90afbdfdfdb4f1504fd1677916f1437d94719fb856cab7159d6ccad20ec4dd871e6c7e21ec66c6de039035ace56492ab9443af3
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 rails app.
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
- You can store any kind of object. Strings, numbers, arrays, booleans, or any object.
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
 
@@ -34,7 +32,7 @@ class Setting < RailsSettings::Base
34
32
  scope :application do
35
33
  field :app_name, default: "Rails Settings", validates: { presence: true, length: { in: 2..20 } }
36
34
  field :host, default: "http://example.com", readonly: true
37
- field :default_locale, default: "zh-CN", validates: { presence: true, inclusion: { in: %w[zh-CN en jp] } }, option_values: %w[en zh-CN]
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 ..."
38
36
  field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
39
37
 
40
38
  # lambda default value
@@ -46,14 +44,14 @@ class Setting < RailsSettings::Base
46
44
  scope :limits do
47
45
  field :user_limits, type: :integer, default: 20
48
46
  field :exchange_rate, type: :float, default: 0.123
49
- field :captcha_enable, type: :boolean, default: true, group: :limits
47
+ field :captcha_enable, type: :boolean, default: true
50
48
  end
51
49
 
52
50
  field :notification_options, type: :hash, default: {
53
51
  send_all: true,
54
52
  logging: true,
55
53
  sender_email: "foo@bar.com"
56
- }, group: :advanced
54
+ }
57
55
 
58
56
  field :readonly_item, type: :integer, default: 100, readonly: true
59
57
  end
@@ -61,6 +59,8 @@ end
61
59
 
62
60
  You must use the `field` method to statement the setting keys, otherwise you can't use it.
63
61
 
62
+ The `scope` method allows you to group the keys for admin UI.
63
+
64
64
  Now just put that migration in the database with:
65
65
 
66
66
  ```bash
@@ -142,20 +142,23 @@ Setting.keys
142
142
  => ["app_name", "host", "default_locale", "readonly_item"]
143
143
 
144
144
  # Get editable keys
145
- Settng.editable_keys
145
+ Setting.editable_keys
146
146
  => ["app_name", "default_locale"]
147
147
 
148
148
  # Get readonly keys
149
149
  Setting.readonly_keys
150
150
  => ["host", "readonly_item"]
151
151
 
152
- # Get options of field
152
+ # Get field
153
153
  Setting.get_field("host")
154
154
  => { scope: :application, key: "host", type: :string, default: "http://example.com", readonly: true }
155
155
  Setting.get_field("app_name")
156
156
  => { scope: :application, key: "app_name", type: :string, default: "Rails Settings", readonly: false }
157
157
  Setting.get_field(:user_limits)
158
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 ..." }
159
162
  ```
160
163
 
161
164
  #### Get All defined fields
@@ -278,6 +281,10 @@ Rails.application.configure do
278
281
  end
279
282
  ```
280
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
+
281
288
  ## Caching flow:
282
289
 
283
290
  ```
@@ -15,7 +15,11 @@ module RailsSettings
15
15
  # get the value field, YAML decoded
16
16
  def value
17
17
  # rubocop:disable Security/YAMLLoad
18
- YAML.load(self[:value]) if self[:value].present?
18
+ payload = self[:value]
19
+
20
+ if payload.present?
21
+ YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(payload) : YAML.load(payload)
22
+ end
19
23
  end
20
24
 
21
25
  # set the value field, YAML encoded
@@ -37,9 +41,16 @@ module RailsSettings
37
41
  _define_field(key, **opts)
38
42
  end
39
43
 
40
- def scope(name)
44
+ alias_method :_rails_scope, :scope
45
+ def scope(*args, &block)
46
+ name = args.shift
47
+ body = args.shift
48
+ if body.respond_to?(:call)
49
+ return _rails_scope(name, body, &block)
50
+ end
51
+
41
52
  @scope = name.to_sym
42
- yield
53
+ yield block
43
54
  @scope = nil
44
55
  end
45
56
 
@@ -193,7 +204,7 @@ module RailsSettings
193
204
  end
194
205
 
195
206
  def _all_settings
196
- RequestCache.settings ||= Rails.cache.fetch(cache_key, expires_in: 1.week) do
207
+ RequestCache.all_settings ||= Rails.cache.fetch(cache_key, expires_in: 1.week) do
197
208
  vars = unscoped.select("var, value")
198
209
  result = {}
199
210
  vars.each { |record| result[record.var] = record.value }
@@ -0,0 +1,14 @@
1
+ module RailsSettings
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ RailsSettings::RequestCache.enable!
9
+ result = @app.call(env)
10
+ RailsSettings::RequestCache.disable!
11
+ result
12
+ end
13
+ end
14
+ end
@@ -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
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.7.0"
6
+ "2.8.2"
7
7
  end
8
8
  end
9
9
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "rails-settings/base"
4
4
  require_relative "rails-settings/request_cache"
5
+ require_relative "rails-settings/middleware"
5
6
  require_relative "rails-settings/railtie"
6
7
  require_relative "rails-settings/version"
7
8
 
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.7.0
4
+ version: 2.8.2
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: 2021-07-08 00:00:00.000000000 Z
11
+ date: 2022-01-11 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: codecov
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
@@ -134,7 +107,7 @@ homepage: https://github.com/huacnlee/rails-settings-cached
134
107
  licenses:
135
108
  - MIT
136
109
  metadata: {}
137
- post_install_message:
110
+ post_install_message:
138
111
  rdoc_options: []
139
112
  require_paths:
140
113
  - lib
@@ -149,8 +122,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
122
  - !ruby/object:Gem::Version
150
123
  version: '0'
151
124
  requirements: []
152
- rubygems_version: 3.2.3
153
- signing_key:
125
+ rubygems_version: 3.3.3
126
+ signing_key:
154
127
  specification_version: 4
155
128
  summary: The best solution for store global settings in Rails applications.
156
129
  test_files: []