rails-settings-cached 2.4.1 → 2.5.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: 11dbf6161605ff3aed324a9ac29600d47918cd5fad1b8a5abfadaeddd9636c57
4
- data.tar.gz: 8056da8c1b7441dd6fc78117013b06c0d67b53bfb06497d1e2343b1417503b95
3
+ metadata.gz: f37f8d9bad4b40dd60dbf30806e1e530e1d292448ba803db06bdcca5f562c62e
4
+ data.tar.gz: 2900b13077db46b60b2c22cfa3a21b7af5704b86d874d485672508c8ef866475
5
5
  SHA512:
6
- metadata.gz: 61579ae6068bdd643c4741ac07855c0802220f11be06709910efae5f653dc69db7d7b039cb265bc4229a5121aef90ee9de92d6da700aea87cec33bfb9c4800f3
7
- data.tar.gz: 2fbd8d62d15ac420abc0ac96ca2aea53eb6d78c9072a1f19fa942f86fd89a8253e829ec827251909dc2cd8f2082e65e890737ba7588d4cf960ebad85fdc76269
6
+ metadata.gz: c696568044b97ce9a9112de42682a7e484e13a8220e0d7b1b52d5977f56358f82109e4bb1dd8b227f89089d060a49454b2bd1c86ac4cc7fbfc2c275bf0a48c55
7
+ data.tar.gz: c2eb40491e08ce8eefe6d96e6d987fd9a02efbad708e9a1ae0442d98b82e626300ee90eda5664be385aa81178c4be4a19b190d623a2ff8cdb0d8e0f8b53d0649
data/README.md CHANGED
@@ -30,9 +30,9 @@ 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"
33
+ field :app_name, default: "Rails Settings", validates: { presence: true, length: { in: 2..20 } }
34
34
  field :host, default: "http://example.com", readonly: true
35
- field :default_locale, default: "zh-CN"
35
+ field :default_locale, default: "zh-CN", validates: { presence: true, inclusion: { in: %w[zh-CN en jp] } }
36
36
  field :readonly_item, type: :integer, default: 100, readonly: true
37
37
  field :user_limits, type: :integer, default: 20
38
38
  field :exchange_rate, type: :float, default: 0.123
@@ -49,7 +49,7 @@ class Setting < RailsSettings::Base
49
49
  }
50
50
 
51
51
  # lambda default value
52
- field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }
52
+ field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }, validates: { length: { maximum: 255 } }
53
53
  end
54
54
  ```
55
55
 
@@ -150,6 +150,39 @@ Setting.get_field("app_name")
150
150
  => { key: "app_name", type: :string, default: "Rails Settings", readonly: false }
151
151
  ```
152
152
 
153
+ ## Validations
154
+
155
+ You can use `validates` options to special the [Rails Validation](https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) for fields.
156
+
157
+ ```rb
158
+ class Setting < RailsSettings::Base
159
+ # cache_prefix { "v1" }
160
+ field :app_name, default: "Rails Settings", validates: { presence: true, length: { in: 2..20 } }
161
+ field :default_locale, default: "zh-CN", validates: { presence: true, inclusion: { in: %w[zh-CN en jp], message: "is not included in [zh-CN, en, jp]" } }
162
+ end
163
+ ```
164
+
165
+ Now validate will work on record save.
166
+
167
+ ```rb
168
+ setting = Setting.find_or_initialize_by(var: :app_name)
169
+ setting.value = ""
170
+ setting.valid?
171
+ # => false
172
+ setting.errors.full_messages
173
+ # => ["App name can't be blank", "App name too short (minimum is 2 characters)"]
174
+
175
+ setting = Setting.find_or_initialize_by(var: :default_locale)
176
+ setting.value = "zh-TW"
177
+ setting.save
178
+ # => false
179
+ setting.errors.full_messages
180
+ # => ["Default locale is not included in [zh-CN, en, jp]"]
181
+ setting.value = "en"
182
+ setting.valid?
183
+ # => true
184
+ ```
185
+
153
186
  ## Use Setting in Rails initializing:
154
187
 
155
188
  In `version 2.3+` you can use Setting before Rails is initialized.
@@ -265,9 +298,25 @@ app/controllers/admin/settings_controller.rb
265
298
  module Admin
266
299
  class SettingsController < ApplicationController
267
300
  def create
301
+ @errors = ActiveModel::Errors.new
302
+ setting_params.keys.each do |key|
303
+ next if setting_params[key].nil?
304
+
305
+ setting = Setting.new(var: key)
306
+ setting.value = setting_params[key].strip
307
+ unless setting.valid?
308
+ @errors.merge!(setting.errors)
309
+ end
310
+ end
311
+
312
+ if @errors.any?
313
+ render :new
314
+ end
315
+
268
316
  setting_params.keys.each do |key|
269
317
  Setting.send("#{key}=", setting_params[key].strip) unless setting_params[key].nil?
270
318
  end
319
+
271
320
  redirect_to admin_settings_path, notice: "Setting was successfully updated."
272
321
  end
273
322
 
@@ -284,6 +333,16 @@ app/views/admin/settings/show.html.erb
284
333
 
285
334
  ```erb
286
335
  <%= form_for(Setting.new, url: admin_settings_path) do |f| %>
336
+ <% if @errors.any? %>
337
+ <div class="alert alert-block alert-danger">
338
+ <ul>
339
+ <% @errors.full_messages.each do |msg| %>
340
+ <li><%= msg %></li>
341
+ <% end %>
342
+ </ul>
343
+ </div>
344
+ <% end %>
345
+
287
346
  <div class="form-group">
288
347
  <label class="control-label">Host</label>
289
348
  <%= f.text_field :host, value: Setting.host, class: "form-control", placeholder: "http://localhost" %>
@@ -28,7 +28,7 @@ module RailsSettings
28
28
  end
29
29
 
30
30
  def field(key, **opts)
31
- _define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator])
31
+ _define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator], validates: opts[:validates])
32
32
  end
33
33
 
34
34
  def get_field(key)
@@ -59,7 +59,7 @@ module RailsSettings
59
59
 
60
60
  private
61
61
 
62
- def _define_field(key, default: nil, type: :string, readonly: false, separator: nil)
62
+ def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil)
63
63
  @defined_fields ||= []
64
64
  @defined_fields << {
65
65
  key: key.to_s,
@@ -95,10 +95,19 @@ module RailsSettings
95
95
  value = send(:_convert_string_to_typeof_value, type, value, separator: separator)
96
96
 
97
97
  record.value = value
98
- record.save!
98
+ record.save(validate: false)
99
99
 
100
100
  value
101
101
  end
102
+
103
+ if validates
104
+ validates[:if] = Proc.new { |item| item.var.to_s == key.to_s }
105
+ send(:validates, key, **validates)
106
+
107
+ define_method(:read_attribute_for_validation) do |_key|
108
+ self.value
109
+ end
110
+ end
102
111
  end
103
112
 
104
113
  if type == :boolean
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.4.1"
6
+ "2.5.0"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-settings-cached
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  - !ruby/object:Gem::Version
150
150
  version: '0'
151
151
  requirements: []
152
- rubygems_version: 3.1.2
152
+ rubygems_version: 3.0.8
153
153
  signing_key:
154
154
  specification_version: 4
155
155
  summary: The best solution for store global settings in Rails applications.