rails-settings-cached 2.1.1 → 2.2.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: f952983987531a930ac0f603d37aed4938648f4e858297182b4039987fc6c975
4
- data.tar.gz: ecbfbf363e6c67519ced21e25966ebb0339bc7dae2e3171d95ab2d04675b5b7e
3
+ metadata.gz: 765057576ddcdb19d9f394f1844950f03e8c70944abd9b104e70824f60f7e0e5
4
+ data.tar.gz: 0d2a2c4aa7560d96bee3a0c1a0d50f7cb89bffd5c650ae47f2bd6169c0d12b6e
5
5
  SHA512:
6
- metadata.gz: d78651e359e244a155e1e743dd391494f0792504b816dc8bd0d3cdbfd1d7103a16c020895f7b751007f5f5b3a04d4d94398e5a7df4cc09ad07ff1166b0d05ed2
7
- data.tar.gz: 2320d0950d5a9b9090f5b5df27f42df1b7a74553104e584728146ac5cd41cd567b58e450e9121dc764be4c534209d988b2b1b516b1e948913806f1b4c5850961
6
+ metadata.gz: 908f7ae3d8e01710f9df4cb32a77b2302129107bcd34254cb5237fc5f5b98f2e4007fe315751ab3fd7e2212e14a0db133f796f994b0bd6a1cee48768717a01a9
7
+ data.tar.gz: fe611e7af4ac5ca72a98040c63b0475833878ac6f11b82953fd80458bb6b96311fff7fc174f890171f466f724be809cd334bc184b0cb0c8c7b3ae94f5fbee28f
data/README.md CHANGED
@@ -50,6 +50,7 @@ class Setting < RailsSettings::Base
50
50
  field :host, default: "http://example.com"
51
51
  field :readonly_item, type: :integer, default: 100, readonly: true
52
52
  field :user_limits, type: :integer, default: 20
53
+ field :exchange_rate, type: :float, default: 0.123
53
54
  field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
54
55
  # Override array separator, default: /[\n,]/ split with \n or comma.
55
56
  field :tips, type: :array, separator: /[\n]+/
@@ -212,18 +213,13 @@ module Admin
212
213
  class SettingsController < ApplicationController
213
214
  before_action :get_setting, only: [:edit, :update]
214
215
 
215
- def show
216
- end
217
-
218
216
  def create
219
217
  setting_params.keys.each do |key|
220
- next if key.to_s == "site_logo"
221
218
  Setting.send("#{key}=", setting_params[key].strip) unless setting_params[key].nil?
222
219
  end
223
- redirect_to admin_settings_path(notice: "Setting was successfully updated.")
220
+ redirect_to settings_path, notice: "Setting was successfully updated."
224
221
  end
225
222
 
226
-
227
223
  private
228
224
  def setting_params
229
225
  params.require(:setting).permit(:host, :user_limits, :admin_emails,
@@ -259,7 +255,8 @@ app/views/admin/settings/show.html.erb
259
255
  <%= f.text_area :notification_options, value: YAML.dump(Setting.notification_options), class: "form-control", style: "height: 180px;" %>
260
256
  <div class="form-text">
261
257
  Use YAML format to config the SMTP_html
262
- </details>
258
+ </div>
259
+ </div>
263
260
  <% end %>
264
261
  ```
265
262
 
@@ -331,8 +328,7 @@ Same values will fetch from the `settings` table.
331
328
 
332
329
  ## Use cases:
333
330
 
334
- - [ruby-china/ruby-china](https://github.com/ruby-china/ruby-china)
331
+ - [ruby-china/homeland](https://github.com/ruby-china/homeland)
335
332
  - [thebluedoc/bluedoc](https://github.com/thebluedoc/bluedoc/blob/master/app/models/setting.rb)
336
333
  - [tootsuite/mastodon](https://github.com/tootsuite/mastodon)
337
334
  - [helpyio/helpy](https://github.com/helpyio/helpy)
338
-
@@ -6,7 +6,7 @@ module RailsSettings
6
6
  class Base < ActiveRecord::Base
7
7
  class SettingNotFound < RuntimeError; end
8
8
 
9
- SEPARATOR_REGEXP = /[\n,]+/
9
+ SEPARATOR_REGEXP = /[\n,;]+/
10
10
  self.table_name = table_name_prefix + "settings"
11
11
 
12
12
  # get the value field, YAML decoded
@@ -30,6 +30,8 @@ module RailsSettings
30
30
  end
31
31
 
32
32
  def field(key, **opts)
33
+ @keys ||= []
34
+ @keys << key.to_s
33
35
  _define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator])
34
36
  end
35
37
 
@@ -43,11 +45,16 @@ module RailsSettings
43
45
  scope.join("/")
44
46
  end
45
47
 
48
+ def keys
49
+ @keys
50
+ end
51
+
46
52
  private
53
+
47
54
  def _define_field(key, default: nil, type: :string, readonly: false, separator: nil)
48
55
  if readonly
49
56
  define_singleton_method(key) do
50
- self.send(:_covert_string_to_typeof_value, type, default, separator: separator)
57
+ self.send(:_convert_string_to_typeof_value, type, default, separator: separator)
51
58
  end
52
59
  else
53
60
  define_singleton_method(key) do
@@ -60,7 +67,7 @@ module RailsSettings
60
67
  result = default.call if default.is_a?(Proc)
61
68
  end
62
69
 
63
- result = self.send(:_covert_string_to_typeof_value, type, result, separator: separator)
70
+ result = self.send(:_convert_string_to_typeof_value, type, result, separator: separator)
64
71
 
65
72
  result
66
73
  end
@@ -69,7 +76,7 @@ module RailsSettings
69
76
  var_name = key.to_s
70
77
 
71
78
  record = find_by(var: var_name) || new(var: var_name)
72
- value = self.send(:_covert_string_to_typeof_value, type, value, separator: separator)
79
+ value = self.send(:_convert_string_to_typeof_value, type, value, separator: separator)
73
80
 
74
81
  record.value = value
75
82
  record.save!
@@ -85,20 +92,24 @@ module RailsSettings
85
92
  end
86
93
  end
87
94
 
88
- def _covert_string_to_typeof_value(type, value, separator: nil)
89
- return value unless value.is_a?(String) || value.is_a?(Integer)
95
+ def _convert_string_to_typeof_value(type, value, separator: nil)
96
+ return value unless [String, Integer, Float, BigDecimal].include?(value.class)
90
97
 
91
98
  case type
92
99
  when :boolean
93
- return value == "true" || value == "1" || value == 1 || value == true
100
+ value == "true" || value == "1" || value == 1 || value == true
94
101
  when :array
95
- return value.split(separator || SEPARATOR_REGEXP).reject { |str| str.empty? }
102
+ value.split(separator || SEPARATOR_REGEXP).reject { |str| str.empty? }.map(&:strip)
96
103
  when :hash
97
- value = YAML.load(value).to_hash rescue {}
104
+ value = YAML.load(value).to_h rescue eval(value).to_h rescue {}
98
105
  value.deep_stringify_keys!
99
- return value
106
+ value
100
107
  when :integer
101
- return value.to_i
108
+ value.to_i
109
+ when :float
110
+ value.to_f
111
+ when :big_decimal
112
+ value.to_d
102
113
  else
103
114
  value
104
115
  end
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.1.1"
6
+ "2.2.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.1.1
4
+ version: 2.2.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: 2019-10-07 00:00:00.000000000 Z
11
+ date: 2020-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: sqlite3
42
+ name: codecov
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: rubocop
56
+ name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: simplecov
70
+ name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: minitest
84
+ name: simplecov
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: codecov
98
+ name: sqlite3
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="