rails-settings-cached 2.0.3 → 2.2.1

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: f433f1f67e2ad2493d2ea5569115ee4af86f7d37a6e2abf033f3f780697fbc5f
4
- data.tar.gz: 76593d4f3d843df0dbae34aaf0fbcce0ac9b89423dffef310c9de73c23e4e744
3
+ metadata.gz: 85279c41b179aa65d668a48d4d18e73b5fe886be9cd8235dc2acdb54c3beb5da
4
+ data.tar.gz: 516991953aa7698cf86511512d863b7903d1ec5ad648a69c386501413a75ac86
5
5
  SHA512:
6
- metadata.gz: 34f9a5afcdfb90d4524f69b121accf15011a770296a585d720e85e97b3500d226bb674cf2b28da68d3c619b7c8cd1c93ffa398a0142a5e2937fa0e04fb67621a
7
- data.tar.gz: e21096b92f58e31bd8db19bb09694ee236188d2b57ec1a35dc74bf853d591eaab84c50915715d5d08639086010b72e496c013a2d1548c48b8904e947f274d753
6
+ metadata.gz: 742596dc7e47c3d31ea64ecf54d96cae5885938076c2704aebe5f3e94701627631d19a8480f24ef97dc1861a8e1a1cab33c6f3aefd56ff755f88ef47c1380401
7
+ data.tar.gz: aa7b9a3ba2443a7ac6cb12150c196247bfd543e086b4eea1199f83eca1035ddefc1bcb32ae5d01cf9c3448dcd2f5c651dd9a73c8dab22600d28f202ef7d1f4a6
@@ -1,5 +1,4 @@
1
- Copyright (c) 2006 Alex Wayne
2
- Some additional features added 2009 by Georg Ledermann
1
+ Copyright (c) 2019 Jason Lee
3
2
 
4
3
  Permission is hereby granted, free of charge, to any person obtaining
5
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,43 +1,28 @@
1
- ## Rails Settings Cached
1
+ # Rails Settings Cached
2
2
 
3
3
  This a plugin that makes managing a table of
4
- global key, value pairs easy. Think of it like a global Hash stored in your database,
4
+ а global key, value pairs easy. Think of it like a global Hash stored in your database,
5
5
  that uses simple ActiveRecord like methods for manipulation. Keep track of any global
6
- setting that you dont want to hard code into your rails app. You can store any kind
6
+ setting that you don't want to hard code into your rails app. You can store any kind
7
7
  of object. Strings, numbers, arrays, or any object.
8
8
 
9
- > 🚨 BREAK CHANGES WARNING:
10
- > rails-settings-cached 2.x has redesign the API, the new version will compatible with the stored setting values by older version.
11
- > When you wants to upgrade 2.x, you must read the README again, and follow guides to change your Setting model.
12
-
13
- ## Status
14
-
15
- [![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![CI Status](https://travis-ci.org/huacnlee/rails-settings-cached.svg)](http://travis-ci.org/huacnlee/rails-settings-cached) [![Code Climate](https://codeclimate.com/github/huacnlee/rails-settings-cached/badges/gpa.svg)](https://codeclimate.com/github/huacnlee/rails-settings-cached) [![codecov.io](https://codecov.io/github/huacnlee/rails-settings-cached/coverage.svg?branch=master)](https://codecov.io/github/huacnlee/rails-settings-cached?branch=master)
9
+ [![Gem Version](https://badge.fury.io/rb/rails-settings-cached.svg)](https://rubygems.org/gems/rails-settings-cached) [![CI Status](https://travis-ci.org/huacnlee/rails-settings-cached.svg)](http://travis-ci.org/huacnlee/rails-settings-cached) [![codecov.io](https://codecov.io/github/huacnlee/rails-settings-cached/coverage.svg?branch=master)](https://codecov.io/github/huacnlee/rails-settings-cached?branch=master)
16
10
 
17
- ## Setup
11
+ ## Installation
18
12
 
19
13
  Edit your Gemfile:
20
14
 
21
- ```ruby
22
- gem "rails-settings-cached"
15
+ ```bash
16
+ $ bundle add rails-settings-cached
23
17
  ```
24
18
 
25
19
  Generate your settings:
26
20
 
27
21
  ```bash
28
22
  $ rails g settings:install
29
- ```
30
-
31
- If you want custom model name:
32
23
 
33
- ```bash
34
- $ rails g settings:install
35
- ```
36
-
37
- Or use a custom name:
38
-
39
- ```bash
40
- $ rails g settings:install SiteConfig
24
+ # Or use a custom name:
25
+ $ rails g settings:install AppConfig
41
26
  ```
42
27
 
43
28
  You will get `app/models/setting.rb`
@@ -45,31 +30,40 @@ You will get `app/models/setting.rb`
45
30
  ```rb
46
31
  class Setting < RailsSettings::Base
47
32
  # cache_prefix { "v1" }
48
-
33
+ field :app_name, default: "Rails Settings Cache Demo"
49
34
  field :host, default: "http://example.com"
35
+ field :default_locale, default: "zh-CN"
50
36
  field :readonly_item, type: :integer, default: 100, readonly: true
51
37
  field :user_limits, type: :integer, default: 20
38
+ field :exchange_rate, type: :float, default: 0.123
52
39
  field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
53
- field :captcha_enable, type: :boolean, default: 1
40
+ field :captcha_enable, type: :boolean, default: true
41
+
42
+ # Override array separator, default: /[\n,]/ split with \n or comma.
43
+ field :tips, type: :array, separator: /[\n]+/
44
+
54
45
  field :notification_options, type: :hash, default: {
55
46
  send_all: true,
56
47
  logging: true,
57
48
  sender_email: "foo@bar.com"
58
49
  }
50
+
51
+ # lambda default value
52
+ field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }
59
53
  end
60
54
  ```
61
55
 
62
- You must use `field` method to statement the setting keys, otherwice you can't use it.
56
+ You must use `field` method to statement the setting keys, otherwise you can't use it.
63
57
 
64
58
  Now just put that migration in the database with:
65
59
 
66
60
  ```bash
67
- rake db:migrate
61
+ $ rails db:migrate
68
62
  ```
69
63
 
70
64
  ## Usage
71
65
 
72
- The syntax is easy. First, lets create some settings to keep track of:
66
+ The syntax is easy. First, let's create some settings to keep track of:
73
67
 
74
68
  ```ruby
75
69
  irb > Setting.host
@@ -132,7 +126,7 @@ irb > Setting.notification_options
132
126
 
133
127
  ## Readonly field
134
128
 
135
- Some times you may need use Setting before Rails initialize, for example `config/devise.rb`
129
+ Sometimes you may need to use Setting before Rails is initialized, for example `config/devise.rb`
136
130
 
137
131
  ```rb
138
132
  Devise.setup do |config|
@@ -162,9 +156,9 @@ Setting.host -> Check Cache -> Exist - Get value of key for cache -> Return
162
156
  Return default value or nil
163
157
  ```
164
158
 
165
- In each Setting keys call, we will load the cache/db and save in Thread.current for avoid hit cache/db.
159
+ 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.
166
160
 
167
- Each key update will expires the cache, so do not add some frequent update key.
161
+ Each key update will expire the cache, so do not add some frequent update key.
168
162
 
169
163
  ## Change cache key
170
164
 
@@ -189,15 +183,15 @@ end
189
183
 
190
184
  -----
191
185
 
192
- ## How to manage Settings in admin interface?
186
+ ## How to manage Settings in the admin interface?
193
187
 
194
- If you want create an admin interface to editing the Settings, you can try methods in follow:
188
+ If you want to create an admin interface to editing the Settings, you can try methods in following:
195
189
 
196
190
  config/routes.rb
197
191
 
198
192
  ```rb
199
193
  namespace :admin do
200
- resources :settings
194
+ resource :settings
201
195
  end
202
196
  ```
203
197
 
@@ -209,18 +203,13 @@ module Admin
209
203
  class SettingsController < ApplicationController
210
204
  before_action :get_setting, only: [:edit, :update]
211
205
 
212
- def show
213
- end
214
-
215
206
  def create
216
207
  setting_params.keys.each do |key|
217
- next if key.to_s == "site_logo"
218
208
  Setting.send("#{key}=", setting_params[key].strip) unless setting_params[key].nil?
219
209
  end
220
- redirect_to admin_settings_path(notice: "Setting was successfully updated.")
210
+ redirect_to settings_path, notice: "Setting was successfully updated."
221
211
  end
222
212
 
223
-
224
213
  private
225
214
  def setting_params
226
215
  params.require(:setting).permit(:host, :user_limits, :admin_emails,
@@ -256,10 +245,52 @@ app/views/admin/settings/show.html.erb
256
245
  <%= f.text_area :notification_options, value: YAML.dump(Setting.notification_options), class: "form-control", style: "height: 180px;" %>
257
246
  <div class="form-text">
258
247
  Use YAML format to config the SMTP_html
259
- </details>
248
+ </div>
249
+ </div>
250
+
251
+ <div>
252
+ <%= f.submit 'Update Settings' %>
253
+ </div>
260
254
  <% end %>
261
255
  ```
262
256
 
263
- ## Use case:
257
+ ## Scoped Settings
258
+
259
+ > 🚨 BREAK CHANGES WARNING:
260
+ > rails-settings-cached 2.x has redesigned the API, the new version will compatible with the stored setting values by an older version.
261
+ > When you want to upgrade 2.x, you must read the README again, and follow guides to change your Setting model.
262
+ > 0.x stable branch: https://github.com/huacnlee/rails-settings-cached/tree/0.x
263
+
264
+ - [Backward compatible to support 0.x scoped settings](docs/backward-compatible-to-scoped-settings.md)
265
+
266
+ For new project / new user of rails-settings-cached. The [ActiveRecord::AttributeMethods::Serialization](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize) is best choice.
267
+
268
+ > This is reason of why rails-settings-cached 2.x removed **Scoped Settings** feature.
269
+
270
+ For example:
271
+
272
+ We wants a preferences setting for user.
273
+
274
+ ```rb
275
+ class User < ActiveRecord::Base
276
+ serialize :preferences
277
+ end
278
+
279
+ @user = User.new
280
+ @user.preferences[:receive_emails] = true
281
+ @user.preferences[:public_email] = true
282
+ @user.save
283
+ ```
284
+
285
+ ## Use cases:
286
+
287
+ - [ruby-china/homeland](https://github.com/ruby-china/homeland) - master
288
+ - [forem/forem](https://github.com/forem/forem) - 2.x
289
+ - [siwapp/siwapp](https://github.com/siwapp/siwapp) - 2.x
290
+ - [aidewoode/black_candy](https://github.com/aidewoode/black_candy) - 2.x
291
+ - [thebluedoc/bluedoc](https://github.com/thebluedoc/bluedoc/blob/master/app/models/setting.rb) - 2.x
292
+ - [tootsuite/mastodon](https://github.com/tootsuite/mastodon) - 0.6.x
293
+ - [helpyio/helpy](https://github.com/helpyio/helpy) - 0.5.x
294
+
264
295
 
265
- - [ruby-china/ruby-china](https://github.com/ruby-china/ruby-china)
296
+ And more than [1K repositories](https://github.com/huacnlee/rails-settings-cached/network/dependents) used.
@@ -3,14 +3,15 @@
3
3
  require "rails/generators"
4
4
  require "rails/generators/migration"
5
5
 
6
- module Settings
6
+ module RailsSettings
7
7
  class InstallGenerator < Rails::Generators::NamedBase
8
+ namespace "settings:install"
8
9
  desc "Generate RailsSettings files."
9
10
  include Rails::Generators::Migration
10
11
 
11
12
  argument :name, type: :string, default: "setting"
12
13
 
13
- source_root File.expand_path("../templates", __FILE__)
14
+ source_root File.expand_path("templates", __dir__)
14
15
 
15
16
  @@migrations = false
16
17
 
@@ -32,12 +33,16 @@ module Settings
32
33
  migration_template "migration.rb", "db/migrate/create_settings.rb", migration_version: migration_version
33
34
  end
34
35
 
35
- def rails5?
36
- Rails.version.start_with? "5"
36
+ def rails_version_major
37
+ Rails::VERSION::MAJOR
38
+ end
39
+
40
+ def rails_version_minor
41
+ Rails::VERSION::MINOR
37
42
  end
38
43
 
39
44
  def migration_version
40
- "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if rails5?
45
+ "[#{rails_version_major}.#{rails_version_minor}]" if rails_version_major >= 5
41
46
  end
42
47
  end
43
48
  end
@@ -6,7 +6,7 @@ module RailsSettings
6
6
  class Base < ActiveRecord::Base
7
7
  class SettingNotFound < RuntimeError; end
8
8
 
9
- SEPARATOR_REGEXP = /[\s,]/
9
+ SEPARATOR_REGEXP = /[\n,;]+/
10
10
  self.table_name = table_name_prefix + "settings"
11
11
 
12
12
  # get the value field, YAML decoded
@@ -30,7 +30,9 @@ module RailsSettings
30
30
  end
31
31
 
32
32
  def field(key, **opts)
33
- _define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly])
33
+ @keys ||= []
34
+ @keys << key.to_s
35
+ _define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator])
34
36
  end
35
37
 
36
38
  def cache_prefix(&block)
@@ -43,14 +45,19 @@ module RailsSettings
43
45
  scope.join("/")
44
46
  end
45
47
 
48
+ def keys
49
+ @keys
50
+ end
51
+
46
52
  private
47
- def _define_field(key, default: nil, type: :string, readonly: false)
53
+
54
+ def _define_field(key, default: nil, type: :string, readonly: false, separator: nil)
48
55
  if readonly
49
- self.class.define_method(key) do
50
- self.send(:_covert_string_to_typeof_value, type, default)
56
+ define_singleton_method(key) do
57
+ self.send(:_convert_string_to_typeof_value, type, default, separator: separator)
51
58
  end
52
59
  else
53
- self.class.define_method(key) do
60
+ define_singleton_method(key) do
54
61
  val = self.send(:_value_of, key)
55
62
  result = nil
56
63
  if !val.nil?
@@ -60,16 +67,16 @@ 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)
70
+ result = self.send(:_convert_string_to_typeof_value, type, result, separator: separator)
64
71
 
65
72
  result
66
73
  end
67
74
 
68
- self.class.define_method("#{key}=") do |value|
75
+ define_singleton_method("#{key}=") do |value|
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)
79
+ value = self.send(:_convert_string_to_typeof_value, type, value, separator: separator)
73
80
 
74
81
  record.value = value
75
82
  record.save!
@@ -79,26 +86,30 @@ module RailsSettings
79
86
  end
80
87
 
81
88
  if type == :boolean
82
- self.class.define_method("#{key}?") do
89
+ define_singleton_method("#{key}?") do
83
90
  self.send(key)
84
91
  end
85
92
  end
86
93
  end
87
94
 
88
- def _covert_string_to_typeof_value(type, value)
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_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
@@ -115,7 +126,7 @@ module RailsSettings
115
126
  end
116
127
 
117
128
  def _all_settings
118
- raise "You can use settings before Rails initialize." unless rails_initialized?
129
+ raise "You cannot use settings before Rails initialize." unless rails_initialized?
119
130
  RequestStore.store[:rails_settings_all_settings] ||= begin
120
131
  Rails.cache.fetch(self.cache_key, expires_in: 1.week) do
121
132
  vars = unscoped.select("var, value")
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.0.3"
6
+ "2.2.1"
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.0.3
4
+ version: 2.2.1
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: 2019-04-26 00:00:00.000000000 Z
11
+ date: 2020-10-14 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
  - - ">="
@@ -130,9 +130,10 @@ files:
130
130
  - lib/rails-settings/railtie.rb
131
131
  - lib/rails-settings/version.rb
132
132
  homepage: https://github.com/huacnlee/rails-settings-cached
133
- licenses: []
133
+ licenses:
134
+ - MIT
134
135
  metadata: {}
135
- post_install_message:
136
+ post_install_message:
136
137
  rdoc_options: []
137
138
  require_paths:
138
139
  - lib
@@ -147,8 +148,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
148
  - !ruby/object:Gem::Version
148
149
  version: '0'
149
150
  requirements: []
150
- rubygems_version: 3.0.1
151
- signing_key:
151
+ rubygems_version: 3.0.3
152
+ signing_key:
152
153
  specification_version: 4
153
154
  summary: Settings plugin for Rails that makes managing a table of global keys.
154
155
  test_files: []