ledermann-rails-settings 1.2.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.travis.yml +14 -6
- data/Gemfile +1 -1
- data/MIT-LICENSE +6 -5
- data/README.md +171 -158
- data/Rakefile +4 -9
- data/ci/Gemfile-rails-4-2 +7 -0
- data/ci/Gemfile-rails-5-0 +6 -0
- data/ci/Gemfile-rails-5-1 +6 -0
- data/ci/Gemfile-rails-5-2 +6 -0
- data/ci/Gemfile-rails-6-0 +6 -0
- data/lib/generators/rails_settings/migration/migration_generator.rb +23 -0
- data/lib/generators/rails_settings/migration/templates/migration.rb +21 -0
- data/lib/ledermann-rails-settings.rb +1 -0
- data/lib/rails-settings.rb +21 -5
- data/lib/rails-settings/base.rb +48 -0
- data/lib/rails-settings/configuration.rb +39 -0
- data/lib/rails-settings/scopes.rb +34 -0
- data/lib/rails-settings/setting_object.rb +84 -0
- data/lib/rails-settings/version.rb +2 -2
- data/rails-settings.gemspec +22 -21
- data/spec/configuration_spec.rb +120 -0
- data/spec/database.yml +3 -0
- data/spec/queries_spec.rb +101 -0
- data/spec/scopes_spec.rb +31 -0
- data/spec/serialize_spec.rb +40 -0
- data/spec/setting_object_spec.rb +153 -0
- data/spec/settings_spec.rb +248 -0
- data/spec/spec_helper.rb +111 -0
- data/spec/support/matchers/perform_queries.rb +22 -0
- data/spec/support/query_counter.rb +17 -0
- metadata +130 -118
- data/Changelog.md +0 -17
- data/ci/Gemfile.rails-2.3.x +0 -5
- data/ci/Gemfile.rails-3.0.x +0 -5
- data/ci/Gemfile.rails-3.1.x +0 -5
- data/ci/Gemfile.rails-3.2.x +0 -5
- data/init.rb +0 -1
- data/lib/rails-settings/active_record.rb +0 -38
- data/lib/rails-settings/null_store.rb +0 -48
- data/lib/rails-settings/scoped_settings.rb +0 -14
- data/lib/rails-settings/settings.rb +0 -142
- data/test/settings_test.rb +0 -252
- data/test/test_helper.rb +0 -34
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d35191c6b15823ec6934c9b66a8cf4291aa552c79bfe1ac6e24b2298a3bf6942
|
4
|
+
data.tar.gz: 1dabb6089d5894acc9f16aa57637daded9e9751c590b924fe3a8d3e074e01ac8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2556adc7367efd4acd8a208d342d4f485ea356a79b1bac8971785bb2d3ab9ea3d31ec13b2008fe4e58171eb181090e7d7b18f3a242ee220fee9703777c7ca387
|
7
|
+
data.tar.gz: b391a6f0c86c8abab56b8b68b20161fbe4981c7d08acfa5766416d097d0c2ada7c77db29182b46b62853f0e822b77cc21ae300b3f42657c50cdebc7d83bdf4a8
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
-
|
4
|
-
-
|
3
|
+
- 2.4.6
|
4
|
+
- 2.5.5
|
5
|
+
- 2.6.3
|
5
6
|
gemfile:
|
6
|
-
- ci/Gemfile
|
7
|
-
|
8
|
-
- ci/Gemfile
|
9
|
-
- ci/Gemfile
|
7
|
+
- ci/Gemfile-rails-4-2
|
8
|
+
- ci/Gemfile-rails-5-0
|
9
|
+
- ci/Gemfile-rails-5-1
|
10
|
+
- ci/Gemfile-rails-5-2
|
11
|
+
- ci/Gemfile-rails-6-0
|
12
|
+
matrix:
|
13
|
+
exclude:
|
14
|
+
- rvm: 2.4.6
|
15
|
+
gemfile: ci/Gemfile-rails-6-0
|
16
|
+
before_install: gem update bundler
|
17
|
+
sudo: false
|
data/Gemfile
CHANGED
data/MIT-LICENSE
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
Copyright (c)
|
2
|
-
|
1
|
+
Copyright (c) 2012-2019 Georg Ledermann
|
2
|
+
|
3
|
+
MIT License
|
3
4
|
|
4
5
|
Permission is hereby granted, free of charge, to any person obtaining
|
5
6
|
a copy of this software and associated documentation files (the
|
@@ -14,8 +15,8 @@ included in all copies or substantial portions of the Software.
|
|
14
15
|
|
15
16
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
17
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
18
|
-
NONINFRINGEMENT. IN NO EVENT
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,158 +1,171 @@
|
|
1
|
-
# Settings
|
2
|
-
|
3
|
-
[![Build Status](https://
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
## Installation
|
16
|
-
|
17
|
-
Include the gem in your Gemfile
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
1
|
+
# Settings for Rails
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/ledermann/rails-settings.svg?branch=master)](https://travis-ci.org/ledermann/rails-settings)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/ledermann/rails-settings.svg)](https://codeclimate.com/github/ledermann/rails-settings)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/ledermann/rails-settings/badge.svg?branch=master)](https://coveralls.io/r/ledermann/rails-settings?branch=master)
|
6
|
+
|
7
|
+
Ruby gem to handle settings for ActiveRecord instances by storing them as serialized Hash in a separate database table. Namespaces and defaults included.
|
8
|
+
|
9
|
+
## Requirements
|
10
|
+
|
11
|
+
* Ruby 2.4 or newer
|
12
|
+
* Rails 4.2 or newer (including Rails 6)
|
13
|
+
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Include the gem in your Gemfile and run `bundle` to install it:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'ledermann-rails-settings'
|
21
|
+
```
|
22
|
+
|
23
|
+
Generate and run the migration:
|
24
|
+
|
25
|
+
```shell
|
26
|
+
rails g rails_settings:migration
|
27
|
+
rake db:migrate
|
28
|
+
```
|
29
|
+
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### Define settings
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class User < ActiveRecord::Base
|
37
|
+
has_settings do |s|
|
38
|
+
s.key :dashboard, :defaults => { :theme => 'blue', :view => 'monthly', :filter => false }
|
39
|
+
s.key :calendar, :defaults => { :scope => 'company'}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
If no defaults are needed, a simplified syntax can be used:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class User < ActiveRecord::Base
|
48
|
+
has_settings :dashboard, :calendar
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
Every setting is handled by the class `RailsSettings::SettingObject`. You can use your own class, e.g. for validations:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class Project < ActiveRecord::Base
|
56
|
+
has_settings :info, :class_name => 'ProjectSettingObject'
|
57
|
+
end
|
58
|
+
|
59
|
+
class ProjectSettingObject < RailsSettings::SettingObject
|
60
|
+
validate do
|
61
|
+
unless self.owner_name.present? && self.owner_name.is_a?(String)
|
62
|
+
errors.add(:base, "Owner name is missing")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
In case you need to define settings separatedly for the same models, you can use the persistent option
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
module UserDashboardConcern
|
72
|
+
extend ActiveSupport::Concern
|
73
|
+
|
74
|
+
included do
|
75
|
+
has_settings persistent: true do |s|
|
76
|
+
s.key :dashboard
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class User < ActiveRecord::Base
|
82
|
+
has_settings persistent: true do |s|
|
83
|
+
s.key :calendar
|
84
|
+
end
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
### Set settings
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
user = User.find(1)
|
92
|
+
user.settings(:dashboard).theme = 'black'
|
93
|
+
user.settings(:calendar).scope = 'all'
|
94
|
+
user.settings(:calendar).display = 'daily'
|
95
|
+
user.save! # saves new or changed settings, too
|
96
|
+
```
|
97
|
+
|
98
|
+
or
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
user = User.find(1)
|
102
|
+
user.settings(:dashboard).update! :theme => 'black'
|
103
|
+
user.settings(:calendar).update! :scope => 'all', :display => 'daily'
|
104
|
+
```
|
105
|
+
|
106
|
+
|
107
|
+
### Get settings
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
user = User.find(1)
|
111
|
+
user.settings(:dashboard).theme
|
112
|
+
# => 'black
|
113
|
+
|
114
|
+
user.settings(:dashboard).view
|
115
|
+
# => 'monthly' (it's the default)
|
116
|
+
|
117
|
+
user.settings(:calendar).scope
|
118
|
+
# => 'all'
|
119
|
+
```
|
120
|
+
|
121
|
+
### Delete settings
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
user = User.find(1)
|
125
|
+
user.settings(:dashboard).update! :theme => nil
|
126
|
+
|
127
|
+
user.settings(:dashboard).view = nil
|
128
|
+
user.settings(:dashboard).save!
|
129
|
+
```
|
130
|
+
|
131
|
+
### Using scopes
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
User.with_settings
|
135
|
+
# => all users having any setting
|
136
|
+
|
137
|
+
User.without_settings
|
138
|
+
# => all users without having any setting
|
139
|
+
|
140
|
+
User.with_settings_for(:calendar)
|
141
|
+
# => all users having a setting for 'calendar'
|
142
|
+
|
143
|
+
User.without_settings_for(:calendar)
|
144
|
+
# => all users without having settings for 'calendar'
|
145
|
+
```
|
146
|
+
|
147
|
+
### Eager Loading
|
148
|
+
```ruby
|
149
|
+
User.includes(:setting_objects)
|
150
|
+
# => Eager load setting_objects when querying many users
|
151
|
+
```
|
152
|
+
|
153
|
+
## Compatibility
|
154
|
+
|
155
|
+
Version 2 is a complete rewrite and has a new DSL, so it's **not** compatible with Version 1. In addition, Rails 2.3 is not supported anymore. But the database schema is unchanged, so you can continue to use the data created by 1.x, no conversion is needed.
|
156
|
+
|
157
|
+
If you don't want to upgrade, you find the old version in the [1.x](https://github.com/ledermann/rails-settings/commits/1.x) branch. But don't expect any updates there.
|
158
|
+
|
159
|
+
|
160
|
+
## Changelog
|
161
|
+
|
162
|
+
See https://github.com/ledermann/rails-settings/releases
|
163
|
+
|
164
|
+
|
165
|
+
## License
|
166
|
+
|
167
|
+
MIT License
|
168
|
+
|
169
|
+
Copyright (c) 2012-2019 [Georg Ledermann](http://www.georg-ledermann.de)
|
170
|
+
|
171
|
+
This gem is a complete rewrite of [rails-settings](https://github.com/Squeegy/rails-settings) by [Alex Wayne](https://github.com/Squeegy)
|
data/Rakefile
CHANGED
@@ -1,11 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
Rake::TestTask.new do |t|
|
4
|
-
t.libs << 'lib' << 'test'
|
5
|
-
t.test_files = Dir.glob("test/**/*_test.rb")
|
6
|
-
t.verbose = true
|
7
|
-
end
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
8
3
|
|
9
|
-
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
10
5
|
|
11
|
-
|
6
|
+
task :default => :spec
|