rails-settings-cached 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 659ee746f1a087bb3ee48bba9f72a3dfd6af920830c6b003fd3bd909dce0a3c9
4
- data.tar.gz: bb07a004ae0bbe413df58a0e2fd72fa7940cf5c55661c30214161745ebc5accc
3
+ metadata.gz: d45081eacc51de459cd0bc48469430474cd561386e924b40996bd5f66c1299e5
4
+ data.tar.gz: 485e2df525a6d3b66dfed0c0b5935aa8134b13ae0b0528d1fae536e5b19e4ae2
5
5
  SHA512:
6
- metadata.gz: 2cbd53148b2110e12e8280a34691e9c08fb1e0805d2cc3ce8a4d25bbb477339e0116d508bac3bfb988bd96ffd3c173838475b5c85bb4659644b275585870198f
7
- data.tar.gz: afe704c8920d76f70795ad55ac17a5ccbfa9807abe9903090fa797b1eb9a7ac4c5564b0a817ccca329b57704eebd3d688ad347c1f6afce58b4fbb58e20a029a1
6
+ metadata.gz: 6651da35fec5737f76192ec01f0980ea45a85c69efb768c22edbce29b490f29dfa2498798b5af6d45a126955a3fe72bf2fa1ae76bd249237b83f408f7bba72c4
7
+ data.tar.gz: b96670e501b9c5aff237b02fd2da2560b654dd2835322b2e64e28679e3edf646ee940f2f2ffe69bfa7d851732ac4844bb47b01e292f4ef4b5f04e587977f8c21
data/README.md CHANGED
@@ -30,26 +30,32 @@ You will get `app/models/setting.rb`
30
30
  ```rb
31
31
  class Setting < RailsSettings::Base
32
32
  # 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
33
 
42
- # Override array separator, default: /[\n,]/ split with \n or comma.
43
- field :tips, type: :array, separator: /[\n]+/
34
+ scope :application do
35
+ field :app_name, default: "Rails Settings", validates: { presence: true, length: { in: 2..20 } }
36
+ 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]
38
+ field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
39
+
40
+ # lambda default value
41
+ field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }, validates: { length: { maximum: 255 } }
42
+ # Override array separator, default: /[\n,]/ split with \n or comma.
43
+ field :tips, type: :array, separator: /[\n]+/
44
+ end
45
+
46
+ scope :limits do
47
+ field :user_limits, type: :integer, default: 20
48
+ field :exchange_rate, type: :float, default: 0.123
49
+ field :captcha_enable, type: :boolean, default: true, group: :limits
50
+ end
44
51
 
45
52
  field :notification_options, type: :hash, default: {
46
53
  send_all: true,
47
54
  logging: true,
48
55
  sender_email: "foo@bar.com"
49
- }
56
+ }, group: :advanced
50
57
 
51
- # lambda default value
52
- field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }, validates: { length: { maximum: 255 } }
58
+ field :readonly_item, type: :integer, default: 100, readonly: true
53
59
  end
54
60
  ```
55
61
 
@@ -145,9 +151,24 @@ Setting.readonly_keys
145
151
 
146
152
  # Get options of 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
+ ```
160
+
161
+ #### Get All defined fields
162
+
163
+ > version 2.7.0+
164
+
165
+ You can use `defined_fields` method to get all defined fields in Setting.
166
+
167
+ ```rb
168
+ # Get editable fields and group by scope
169
+ editable_fields = Setting.defined_fields
170
+ .select { |field| !field[:readonly] }
171
+ .group_by { |field| field[:scope] }
151
172
  ```
152
173
 
153
174
  ## Validations
@@ -34,8 +34,13 @@ module RailsSettings
34
34
  end
35
35
 
36
36
  def field(key, **opts)
37
- _define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly],
38
- separator: opts[:separator], validates: opts[:validates])
37
+ _define_field(key, **opts)
38
+ end
39
+
40
+ def scope(name)
41
+ @scope = name.to_sym
42
+ yield
43
+ @scope = nil
39
44
  end
40
45
 
41
46
  def get_field(key)
@@ -47,9 +52,9 @@ module RailsSettings
47
52
  end
48
53
 
49
54
  def cache_key
50
- scope = ["rails-settings-cached"]
51
- scope << @cache_prefix.call if @cache_prefix
52
- scope.join("/")
55
+ key_parts = ["rails-settings-cached"]
56
+ key_parts << @cache_prefix.call if @cache_prefix
57
+ key_parts.join("/")
53
58
  end
54
59
 
55
60
  def keys
@@ -64,19 +69,23 @@ module RailsSettings
64
69
  @defined_fields.select { |field| field[:readonly] }.map { |field| field[:key] }
65
70
  end
66
71
 
72
+ attr_reader :defined_fields
73
+
67
74
  private
68
75
 
69
- def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil)
76
+ def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil, **opts)
70
77
  key = key.to_s
71
78
 
72
79
  raise ProcetedKeyError.new(key) if PROTECTED_KEYS.include?(key)
73
80
 
74
81
  @defined_fields ||= []
75
82
  @defined_fields << {
83
+ scope: @scope,
76
84
  key: key,
77
85
  default: default,
78
86
  type: type || :string,
79
- readonly: readonly.nil? ? false : readonly
87
+ readonly: readonly.nil? ? false : readonly,
88
+ options: opts
80
89
  }
81
90
 
82
91
  if readonly
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.6.0"
6
+ "2.7.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.6.0
4
+ version: 2.7.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: 2021-06-21 00:00:00.000000000 Z
11
+ date: 2021-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails