ledermann-rails-settings 1.2.1 → 2.0.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 +7 -0
- data/.travis.yml +2 -3
- data/Changelog.md +12 -1
- data/Gemfile +1 -1
- data/MIT-LICENSE +6 -5
- data/README.md +133 -153
- data/Rakefile +4 -9
- data/ci/Gemfile.rails-3.1.x +4 -3
- data/ci/Gemfile.rails-3.2.x +4 -3
- data/lib/generators/rails_settings/migration/migration_generator.rb +23 -0
- data/lib/generators/rails_settings/migration/templates/migration.rb +15 -0
- data/lib/rails-settings.rb +14 -4
- data/lib/rails-settings/base.rb +36 -0
- data/lib/rails-settings/configuration.rb +32 -0
- data/lib/rails-settings/scopes.rb +33 -0
- data/lib/rails-settings/setting_object.rb +64 -0
- data/lib/rails-settings/version.rb +2 -2
- data/rails-settings.gemspec +18 -20
- data/spec/configuration_spec.rb +108 -0
- data/spec/queries_spec.rb +108 -0
- data/spec/scopes_spec.rb +31 -0
- data/spec/serialize_spec.rb +40 -0
- data/spec/setting_object_spec.rb +119 -0
- data/spec/settings_spec.rb +206 -0
- data/spec/spec_helper.rb +90 -0
- data/spec/support/matchers/perform_queries.rb +18 -0
- data/spec/support/query_counter.rb +17 -0
- metadata +59 -72
- data/ci/Gemfile.rails-2.3.x +0 -5
- data/ci/Gemfile.rails-3.0.x +0 -5
- data/init.rb +0 -1
- data/lib/rails-settings/active_record.rb +0 -38
- data/lib/rails-settings/scoped_settings.rb +0 -20
- data/lib/rails-settings/settings.rb +0 -121
- data/test/settings_test.rb +0 -258
- data/test/test_helper.rb +0 -30
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7aa1af77c8b2aba3819e77ba00790f313f143e69
|
4
|
+
data.tar.gz: c4d85b477105c7f86dae04d5fb5b9245506fd1a2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 883960f19b067da03bc8f54d3de59d82d8640eeeb44d31b5ada5949a552a43027f59bec7b3854e9f9bc341c10126deb3c1714c0a6b6d4a5433639a9a1f53ebdb
|
7
|
+
data.tar.gz: 9786e48162d45e3a07020d268dc8dd9cbab7a2d5dd2bcc290a5ac47121f27bd4d0601fe75054d009773a95bf644390546aeaaf8b8425f5735a7eb06c02cdc2cb
|
data/.travis.yml
CHANGED
data/Changelog.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
Version 2.0.0 (2013-03-07)
|
2
|
+
|
3
|
+
- Complete rewrite
|
4
|
+
- New DSL (see README.md)
|
5
|
+
- No global defaults anymore. Defaults are defined per ActiveRecord class
|
6
|
+
- No more storing defaults in the database
|
7
|
+
- Rails >= 3.1 needed (Rails 2.3 not supported anymore)
|
8
|
+
- Threadsafe
|
9
|
+
- Switched to RSpec for testing
|
10
|
+
|
11
|
+
|
1
12
|
Version 1.2.1 (2013-02-09)
|
2
13
|
|
3
14
|
- Quick and dirty fix for design flaw in target scope implementation (thanks to Yves-Eric Martin)
|
@@ -24,4 +35,4 @@ Version 1.0.0 (2011-11-05)
|
|
24
35
|
|
25
36
|
- Conversion from Plugin to Gem
|
26
37
|
- Destroying false values (thanks to @Pavling)
|
27
|
-
- Testing with Travis
|
38
|
+
- Testing with Travis
|
data/Gemfile
CHANGED
data/MIT-LICENSE
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
Copyright (c)
|
2
|
-
|
1
|
+
Copyright (c) 2013 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,153 +1,133 @@
|
|
1
|
-
# Settings
|
2
|
-
|
3
|
-
[](http://travis-ci.org/ledermann/rails-settings)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
User.find(123).settings.some_default
|
136
|
-
# => 'foo'
|
137
|
-
|
138
|
-
|
139
|
-
I you want to find users having or not having some settings, there are named scopes for this:
|
140
|
-
|
141
|
-
User.with_settings
|
142
|
-
# returns a scope of users having any setting
|
143
|
-
|
144
|
-
User.with_settings_for('color')
|
145
|
-
# returns a scope of users having a 'color' setting
|
146
|
-
|
147
|
-
User.without_settings
|
148
|
-
# returns a scope of users having no setting at all (means user.settings.all == {})
|
149
|
-
|
150
|
-
User.without_settings('color')
|
151
|
-
# returns a scope of users having no 'color' setting (means user.settings.color == nil)
|
152
|
-
|
153
|
-
That's all there is to it! Enjoy!
|
1
|
+
# Settings for Rails 3
|
2
|
+
|
3
|
+
[](http://travis-ci.org/ledermann/rails-settings)
|
4
|
+
[](https://codeclimate.com/github/ledermann/rails-settings)
|
5
|
+
|
6
|
+
Ruby gem to handle settings for ActiveRecord objects by storing them as serialized Hash in a separate database table. Optional: Defaults and Namespaces.
|
7
|
+
|
8
|
+
**BEWARE: This is version 2 which is a complete rewrite and NOT compatible with version 1.x**
|
9
|
+
|
10
|
+
|
11
|
+
## Example
|
12
|
+
|
13
|
+
### Defining settings
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class User < ActiveRecord::Base
|
17
|
+
has_settings do |s|
|
18
|
+
s.key :dashboard, :defaults => { :theme => 'blue', :view => 'monthly', :filter => false }
|
19
|
+
s.key :calendar, :defaults => { :scope => 'company'}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
If no defaults are needed, a simplified syntax can be used:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
has_settings :dashboard, :calendar
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
Every setting is handled by the class `RailsSettings::SettingObject`. You can use your own class, e.g. for validations:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class Project < ActiveRecord::Base
|
36
|
+
has_settings :info, :class_name => 'ProjectSettingObject'
|
37
|
+
end
|
38
|
+
|
39
|
+
class ProjectSettingObject < RailsSettings::SettingObject
|
40
|
+
validate do
|
41
|
+
unless self.owner_name.present? && self.owner_name.is_a?(String)
|
42
|
+
errors.add(:base, "Owner name is missing")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
### Set settings
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
user = User.find(1)
|
52
|
+
user.settings(:dashboard).theme = 'black'
|
53
|
+
user.settings(:calendar).scope = 'all'
|
54
|
+
user.settings(:calendar).display = 'daily'
|
55
|
+
user.save! # saves new or changed settings, too
|
56
|
+
```
|
57
|
+
|
58
|
+
or
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
user = User.find(1)
|
62
|
+
user.settings(:dashboard).update_attributes! :theme => 'black'
|
63
|
+
user.settings(:calendar).update_attributes! :scope => 'all', :display => 'dialy'
|
64
|
+
```
|
65
|
+
|
66
|
+
|
67
|
+
### Get settings
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
user = User.find(1)
|
71
|
+
user.settings(:dashboard).theme
|
72
|
+
# => 'black
|
73
|
+
|
74
|
+
user.settings(:dashboard).view
|
75
|
+
# => 'monthly' (it's the default)
|
76
|
+
|
77
|
+
user.settings(:calendar).scope
|
78
|
+
# => 'all'
|
79
|
+
```
|
80
|
+
|
81
|
+
|
82
|
+
### Using scopes
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
User.with_settings
|
86
|
+
# => all users having any setting
|
87
|
+
|
88
|
+
User.without_settings
|
89
|
+
# => all users without having any setting
|
90
|
+
|
91
|
+
User.with_settings_for(:calendar)
|
92
|
+
# => all users having a setting for 'calender'
|
93
|
+
|
94
|
+
User.without_settings_for(:calendar)
|
95
|
+
# => all users without having settings for 'calendar'
|
96
|
+
```
|
97
|
+
|
98
|
+
|
99
|
+
## Requirements
|
100
|
+
|
101
|
+
Rails 3.1.x or 3.2.x
|
102
|
+
Ruby 1.8.7, 1.9.3 or 2.0.0
|
103
|
+
|
104
|
+
|
105
|
+
## Installation
|
106
|
+
|
107
|
+
Include the gem in your Gemfile:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
gem 'ledermann-rails-settings', :require => 'rails-settings'
|
111
|
+
```
|
112
|
+
|
113
|
+
Generate and run the migration:
|
114
|
+
|
115
|
+
```shell
|
116
|
+
rails g rails_settings:migration
|
117
|
+
rake db:migrate
|
118
|
+
```
|
119
|
+
|
120
|
+
## Compatibility
|
121
|
+
|
122
|
+
Version 2 is a complete rewrite and has a new DSL, so it's **not** compatible with Version 1. But the database schema is unchanged, so you can use the same data after upgrading.
|
123
|
+
In addition, Rails 2.3 is not supported anymore.
|
124
|
+
|
125
|
+
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.
|
126
|
+
|
127
|
+
|
128
|
+
## License
|
129
|
+
|
130
|
+
MIT License
|
131
|
+
Copyright (c) 2013 Georg Ledermann
|
132
|
+
|
133
|
+
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
|
data/ci/Gemfile.rails-3.1.x
CHANGED
data/ci/Gemfile.rails-3.2.x
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module RailsSettings
|
5
|
+
class MigrationGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
desc "Generates migration for rails-settings"
|
9
|
+
source_root File.expand_path('../templates', __FILE__)
|
10
|
+
|
11
|
+
def create_migration_file
|
12
|
+
migration_template 'migration.rb', 'db/migrate/rails_settings_migration'
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.next_migration_number(dirname)
|
16
|
+
if ActiveRecord::Base.timestamped_migrations
|
17
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
18
|
+
else
|
19
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class RailsSettingsMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :settings do |t|
|
4
|
+
t.string :var, :null => false
|
5
|
+
t.text :value, :null => false
|
6
|
+
t.references :target, :null => false, :polymorphic => true
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
add_index :settings, [ :target_type, :target_id, :var ], :unique => true
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :settings
|
14
|
+
end
|
15
|
+
end
|
data/lib/rails-settings.rb
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
-
require 'rails-settings/
|
2
|
-
require 'rails-settings/
|
3
|
-
require 'rails-settings/
|
4
|
-
require 'rails-settings/
|
1
|
+
require 'rails-settings/setting_object'
|
2
|
+
require 'rails-settings/configuration'
|
3
|
+
require 'rails-settings/base'
|
4
|
+
require 'rails-settings/scopes'
|
5
|
+
|
6
|
+
ActiveRecord::Base.class_eval do
|
7
|
+
def self.has_settings(*args, &block)
|
8
|
+
RailsSettings::Configuration.new(*args.unshift(self), &block)
|
9
|
+
|
10
|
+
include RailsSettings::Base unless self.include?(RailsSettings::Base)
|
11
|
+
include RailsSettings::Scopes unless self.include?(RailsSettings::Scopes)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|