dm_preferences 0.5.6 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/.gitignore +2 -0
- data/.travis.yml +2 -2
- data/{CHANGELOG.rdoc → CHANGELOG.md} +26 -15
- data/LICENSE +2 -0
- data/{README.rdoc → README.md} +144 -96
- data/Rakefile +20 -27
- data/dm_preferences.gemspec +7 -5
- data/lib/generators/{USAGE → preferences/USAGE} +0 -0
- data/lib/generators/{preferences_generator.rb → preferences/preferences_generator.rb} +0 -0
- data/lib/generators/{templates → preferences/templates}/create_preferences.rb +5 -1
- data/lib/preferences.rb +33 -10
- data/lib/preferences/engine.rb +2 -1
- data/lib/preferences/preference_definition.rb +15 -4
- data/lib/preferences/version.rb +1 -1
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/{test/app_root → spec/dummy}/app/models/car.rb +0 -0
- data/{test/app_root → spec/dummy}/app/models/employee.rb +0 -0
- data/{test/app_root → spec/dummy}/app/models/manager.rb +0 -0
- data/{test/app_root → spec/dummy}/app/models/user.rb +0 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +32 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/{test/app_root → spec/dummy}/db/migrate/001_create_users.rb +0 -0
- data/{test/app_root → spec/dummy}/db/migrate/002_create_cars.rb +0 -0
- data/{test/app_root → spec/dummy}/db/migrate/003_create_employees.rb +0 -0
- data/spec/dummy/db/migrate/004_create_preferences.rb +16 -0
- data/spec/dummy/db/schema.rb +42 -0
- data/spec/functional/preferences_spec.rb +1427 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/assert_queries.rb +36 -0
- data/{test → spec/support}/factory.rb +1 -1
- data/spec/unit/preference_definition_spec.rb +249 -0
- data/{test/unit/preference_test.rb → spec/unit/preference_spec.rb} +114 -122
- metadata +127 -53
- data/test/app_root/db/migrate/004_migrate_preferences_to_version_1.rb +0 -13
- data/test/functional/preferences_test.rb +0 -1387
- data/test/test_helper.rb +0 -26
- data/test/unit/preference_definition_test.rb +0 -237
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NGU0NzUwOWQ4MGQ2ZGZmZWM4OWNmNjAwNmNmZjU1MmEyMmVkYzkxZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MzE1ZGI3ZDU1MWE4MDI5MmI5ZmY1YTFlM2I2MDkxZTQzYmVlNTdjOQ==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmFlY2JjODc2NDViNGVkMmY0MmNiNzQwMGRhNDkyZmY3NGFhZjllZjU5OTk1
|
10
|
+
YmRlNTMzZTQ5YzJkNTZiZDVkNzNkMWY5ZGJkZDY1ODBkNjM2MTdmZGNkNjcw
|
11
|
+
NTE1NjEzOWJiYjM5Yjk3MmM4ZmIzOTM0MjJiZWNhNGE0NGIyYzQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MmVlNjc1MTk5NDE0Mzg0MDZiZDJjYmRjMTM1ZjNmMjU5MTY0NzA2MzliMjc4
|
14
|
+
OWJkNTk1OTQ4Y2VkMzkzODc3YjY1MzM1ZTIxMzJiN2YxMjIwNWU1ZDRlYzBk
|
15
|
+
ZDc3YjZhYTBjMWJlN2NiNGQ4OWFiMjM4ODE2ZDY3NjdkYTA4ZmM=
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
rvm:
|
2
|
-
- 1.9.
|
3
|
-
- 1
|
2
|
+
- 1.9.3
|
3
|
+
- 2.1
|
@@ -1,19 +1,30 @@
|
|
1
|
-
|
1
|
+
## 1.0 / 2015-08-01
|
2
2
|
|
3
|
-
|
3
|
+
* Rails 4.2 support
|
4
|
+
* Uses rspec for testing - all tests now pass
|
5
|
+
* Removed deprecation: "sanitize_sql_hash_for_conditions is deprecated, and will be removed in Rails 5.0"
|
6
|
+
* Add ActiveRecord::Type support for Rails 4.2
|
7
|
+
* use :delete_all on stored_preferences
|
8
|
+
|
9
|
+
## 0.5.6 / 2015-07-31
|
10
|
+
|
11
|
+
* changed name of gem to _dm_preferences_ so as not to conflict with original
|
12
|
+
* works with Rails 4.1
|
13
|
+
|
14
|
+
## 0.5.0 / 2011-08-23
|
4
15
|
|
5
16
|
* Rails 3 support [Jack Dempsey]
|
6
17
|
|
7
|
-
|
18
|
+
## 0.4.2 / 2010-04-17
|
8
19
|
|
9
20
|
* Fix #preferences lookup not typecasting values
|
10
21
|
|
11
|
-
|
22
|
+
## 0.4.1 / 2010-03-07
|
12
23
|
|
13
24
|
* Add support for per-group default preferences
|
14
25
|
* Fix unsaved boolean preferences getting overridden by defaults if value is false
|
15
26
|
|
16
|
-
|
27
|
+
## 0.4.0 / 2010-03-07
|
17
28
|
|
18
29
|
* Add {preference}_changed?, {preference}_was, {preference}_changed, {preference}_will_change!, and reset_{preference}!
|
19
30
|
* Add #preferences_changed?, #preferences_changed, and #preference_changes
|
@@ -30,44 +41,44 @@
|
|
30
41
|
* Add a generator for db migration to make installation a bit easier [Tim Lowrimore]
|
31
42
|
* Add named scopes: #with_preferences and #without_preferences
|
32
43
|
|
33
|
-
|
44
|
+
## 0.3.1 / 2009-04-25
|
34
45
|
|
35
46
|
* Rename Preference#attribute to #name to avoid conflicts with reserved methods in ActiveRecord
|
36
47
|
|
37
|
-
|
48
|
+
## 0.3.0 / 2009-04-13
|
38
49
|
|
39
50
|
* Add dependency on Rails 2.3
|
40
51
|
* Remove dependency on plugins_plus
|
41
52
|
|
42
|
-
|
53
|
+
## 0.2.0 / 2008-12-14
|
43
54
|
|
44
55
|
* Remove the PluginAWeek namespace
|
45
56
|
|
46
|
-
|
57
|
+
## 0.1.5 / 2008-11-16
|
47
58
|
|
48
59
|
* Add all prefers/preferred accessors for preferences to be analogous to ActiveRecord column accessors
|
49
60
|
* Fix preferences defined in STI subclasses not working [Quinn Shanahan]
|
50
61
|
|
51
|
-
|
62
|
+
## 0.1.4 / 2008-10-26
|
52
63
|
|
53
64
|
* Change how the base module is included to prevent namespacing conflicts
|
54
65
|
|
55
|
-
|
66
|
+
## 0.1.3 / 2008-06-29
|
56
67
|
|
57
68
|
* Add +prefs+ as an alias for +preferences+
|
58
69
|
* Fix +preferences+ not properly selecting preferences when a group is specified
|
59
70
|
* Improve test coverage
|
60
71
|
|
61
|
-
|
72
|
+
## 0.1.2 / 2008-06-22
|
62
73
|
|
63
74
|
* Remove log files from gems
|
64
75
|
|
65
|
-
|
76
|
+
## 0.1.1 / 2008-06-20
|
66
77
|
|
67
78
|
* Rename preference_values hash to preferences
|
68
79
|
* Rename preferences association to stored_preferences
|
69
80
|
|
70
|
-
|
81
|
+
## 0.1.0 / 2008-06-19
|
71
82
|
|
72
83
|
* Avoid string evaluation for dynamic methods
|
73
84
|
* Return hashes for the preference_values, e.g.
|
@@ -81,6 +92,6 @@
|
|
81
92
|
|
82
93
|
* Remove support for an options hash when specifying :for associations for preference
|
83
94
|
|
84
|
-
|
95
|
+
## 0.0.1 / 2008-05-10
|
85
96
|
|
86
97
|
* Initial public release
|
data/LICENSE
CHANGED
data/{README.rdoc → README.md}
RENAMED
@@ -1,34 +1,13 @@
|
|
1
|
-
|
1
|
+
# Preferences
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/dm_preferences.svg)](http://badge.fury.io/rb/dm_preferences)
|
4
|
+
[![Build Status](https://travis-ci.org/digitalmoksha/preferences.svg)](https://travis-ci.org/digitalmoksha/preferences)
|
4
5
|
|
5
|
-
|
6
|
+
**Preferences** adds support for easily creating custom preferences for models.
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
* http://rdoc.info/github/pluginaweek/preferences/master/frames
|
10
|
-
|
11
|
-
Bugs
|
12
|
-
|
13
|
-
* http://github.com/pluginaweek/preferences/issues
|
14
|
-
|
15
|
-
Development
|
16
|
-
|
17
|
-
* http://github.com/pluginaweek/preferences
|
18
|
-
|
19
|
-
Testing
|
20
|
-
|
21
|
-
* http://travis-ci.org/pluginaweek/preferences
|
8
|
+
This gem is a fork of the original [preferences gem by Aaron Pfeifer](https://github.com/pluginaweek/preferences). It supports Rails 4.2, and all tests are working. The gem has been renamed to [dm_preferences](https://rubygems.org/gems/dm_preferences) so as not to conflict with the original.
|
22
9
|
|
23
|
-
|
24
|
-
|
25
|
-
* git://github.com/pluginaweek/preferences.git
|
26
|
-
|
27
|
-
Mailing List
|
28
|
-
|
29
|
-
* http://groups.google.com/group/pluginaweek-talk
|
30
|
-
|
31
|
-
== Description
|
10
|
+
## Description
|
32
11
|
|
33
12
|
Preferences for models within an application, such as for users, is a pretty
|
34
13
|
common idiom. Although the rule of thumb is to keep the number of preferences
|
@@ -37,41 +16,49 @@ disable things like e-mail notifications.
|
|
37
16
|
|
38
17
|
Generally, basic preferences can be accomplished through simple designs, such as
|
39
18
|
additional columns or a bit vector described and implemented by preference_fu[http://agilewebdevelopment.com/plugins/preferencefu].
|
19
|
+
|
40
20
|
However, as you find the need for non-binary preferences and the number of
|
41
21
|
preferences becomes unmanageable as individual columns in the database, the next
|
42
22
|
step is often to create a separate "preferences" table. This is where the
|
43
|
-
|
23
|
+
_preferences_ gem comes in.
|
44
24
|
|
45
|
-
|
25
|
+
_preferences_ encapsulates this design by exposing preferences using simple
|
46
26
|
attribute accessors on the model, hiding the fact that preferences are stored in
|
47
27
|
a separate table and making it dead-simple to define and manage preferences.
|
48
28
|
|
49
|
-
|
29
|
+
## Usage
|
50
30
|
|
51
|
-
|
31
|
+
### Installation
|
52
32
|
|
53
|
-
|
33
|
+
Add the following to your gem file:
|
34
|
+
|
35
|
+
gem 'dm_preferences', '~> 1.0'
|
36
|
+
|
37
|
+
_preferences_ requires an additional database table to work. You can generate
|
54
38
|
a migration for this table like so:
|
55
39
|
|
56
|
-
|
40
|
+
rails generate preferences
|
57
41
|
|
58
42
|
Then simply migrate your database:
|
59
43
|
|
60
|
-
|
44
|
+
rake db:migrate
|
61
45
|
|
62
46
|
=== Defining preferences
|
63
47
|
|
64
48
|
To define the preferences for a model, you can do so right within the model:
|
65
49
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
50
|
+
```ruby
|
51
|
+
class User < ActiveRecord::Base
|
52
|
+
preference :hot_salsa
|
53
|
+
preference :dark_chocolate, :default => true
|
54
|
+
preference :color, :string
|
55
|
+
preference :favorite_number
|
56
|
+
preference :language, :string, :default => 'English', :group_defaults => {:chat => 'Spanish'}
|
57
|
+
end
|
58
|
+
```
|
73
59
|
|
74
60
|
In the above model, 5 preferences have been defined:
|
61
|
+
|
75
62
|
* hot_salsa
|
76
63
|
* dark_chocolate
|
77
64
|
* color
|
@@ -82,59 +69,81 @@ For each preference, a data type and default value can be specified. If no
|
|
82
69
|
data type is given, it's assumed to be a boolean value. If no default value is
|
83
70
|
given, the default is assumed to be nil.
|
84
71
|
|
85
|
-
|
72
|
+
### Accessing preferences
|
86
73
|
|
87
74
|
Once preferences have been defined for a model, they can be accessed either
|
88
75
|
using the accessor methods that are generated for each preference or the generic
|
89
76
|
methods that are not specific to a particular preference.
|
90
77
|
|
91
|
-
|
78
|
+
#### Accessors
|
92
79
|
|
93
80
|
There are several shortcut methods that are generated for each preference
|
94
81
|
defined on a model. These reflect the same set of methods (attribute accessors)
|
95
82
|
that are generated for a model's columns. Examples of these are shown below:
|
96
83
|
|
97
84
|
Query methods:
|
85
|
+
|
86
|
+
```ruby
|
98
87
|
user.prefers_hot_salsa? # => false
|
99
88
|
user.preferred_language? # => true
|
89
|
+
```
|
100
90
|
|
101
91
|
Reader methods:
|
92
|
+
|
93
|
+
```ruby
|
102
94
|
user.prefers_hot_salsa # => false
|
103
95
|
user.preferred_language # => "English"
|
96
|
+
```
|
104
97
|
|
105
98
|
Writer methods:
|
106
|
-
user.prefers_hot_salsa = false # => false
|
107
|
-
user.preferred_language = 'English' # => "English"
|
108
99
|
|
109
|
-
|
100
|
+
```ruby
|
101
|
+
user.prefers_hot_salsa = false # => false
|
102
|
+
user.preferred_language = 'English' # => "English"
|
103
|
+
```
|
104
|
+
|
105
|
+
#### Generic methods
|
110
106
|
|
111
107
|
Each preference accessor is essentially a wrapper for the various generic methods
|
112
108
|
shown below:
|
113
109
|
|
114
110
|
Query method:
|
115
|
-
|
116
|
-
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
user.prefers?(:hot_salsa) # => false
|
114
|
+
user.preferred?(:language) # => true
|
115
|
+
```
|
117
116
|
|
118
117
|
Reader method:
|
119
|
-
|
120
|
-
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
user.prefers(:hot_salsa) # => false
|
121
|
+
user.preferred(:language) # => "English"
|
122
|
+
```
|
121
123
|
|
122
124
|
Write method:
|
123
|
-
user.write_preference(:hot_salsa, false) # => false
|
124
|
-
user.write_preference(:language, "English") # => "English"
|
125
125
|
|
126
|
-
|
126
|
+
```ruby
|
127
|
+
user.write_preference(:hot_salsa, false) # => false
|
128
|
+
user.write_preference(:language, "English") # => "English"
|
129
|
+
```
|
130
|
+
|
131
|
+
### Accessing all preferences
|
127
132
|
|
128
133
|
To get the collection of all custom, stored preferences for a particular record,
|
129
134
|
you can access the +stored_preferences+ has_many association which is automatically
|
130
135
|
generated:
|
131
136
|
|
132
|
-
|
137
|
+
```ruby
|
138
|
+
user.stored_preferences
|
139
|
+
```
|
133
140
|
|
134
141
|
In addition to this, you can get a hash of all stored preferences *and* default
|
135
142
|
preferences, by accessing the +preferences+ helper:
|
136
143
|
|
137
|
-
|
144
|
+
```ruby
|
145
|
+
user.preferences # => {"language"=>"English", "color"=>nil}
|
146
|
+
```
|
138
147
|
|
139
148
|
This hash will contain the value for every preference that has been defined for
|
140
149
|
the model, whether that's the default value or one that has been previously
|
@@ -142,19 +151,23 @@ stored.
|
|
142
151
|
|
143
152
|
A short-hand alternative for preferences is also available:
|
144
153
|
|
145
|
-
|
154
|
+
```ruby
|
155
|
+
user.prefs # => {"language"=>"English", "color"=>nil}
|
156
|
+
```
|
146
157
|
|
147
|
-
|
158
|
+
### Grouping preferences
|
148
159
|
|
149
160
|
In addition to defining generic preferences for the owning record, you can also
|
150
161
|
group preferences by ActiveRecord objects or arbitrary names. This is best shown
|
151
162
|
through an example:
|
152
163
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
164
|
+
```ruby
|
165
|
+
user = User.find(:first)
|
166
|
+
car = Car.find(:first)
|
167
|
+
|
168
|
+
user.preferred_color = 'red', car
|
169
|
+
# user.write_preference(:color, 'red', car) # The generic way
|
170
|
+
```
|
158
171
|
|
159
172
|
This will create a color preference of "red" for the given car. In this way,
|
160
173
|
you can have "color" preferences for different records.
|
@@ -162,63 +175,98 @@ you can have "color" preferences for different records.
|
|
162
175
|
To access the preference for a particular record, you can use the same accessor
|
163
176
|
methods as before:
|
164
177
|
|
165
|
-
|
166
|
-
|
178
|
+
```ruby
|
179
|
+
user.preferred_color(car)
|
180
|
+
# user.preferred(:color, car) # The generic way
|
181
|
+
```
|
167
182
|
|
168
183
|
In addition to grouping preferences for a particular record, you can also group
|
169
184
|
preferences by name. For example,
|
170
185
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
user.preferred_color(:automobiles) # => "red"
|
177
|
-
user.preferred_color(:clothing) # => "tan"
|
178
|
-
|
179
|
-
user.preferences(:automobiles) # => {"color"=>"red"}
|
186
|
+
```ruby
|
187
|
+
user = User.find(:first)
|
188
|
+
|
189
|
+
user.preferred_color = 'red', :automobiles
|
190
|
+
user.preferred_color = 'tan', :clothing
|
180
191
|
|
181
|
-
|
192
|
+
user.preferred_color(:automobiles) # => "red"
|
193
|
+
user.preferred_color(:clothing) # => "tan"
|
194
|
+
|
195
|
+
user.preferences(:automobiles) # => {"color"=>"red"}
|
196
|
+
```
|
197
|
+
|
198
|
+
### Saving preferences
|
182
199
|
|
183
200
|
Note that preferences are not saved until the owning record is saved.
|
184
201
|
Preferences are treated in a similar fashion to attributes. For example,
|
185
202
|
|
186
|
-
|
187
|
-
|
188
|
-
|
203
|
+
```ruby
|
204
|
+
user = user.find(:first)
|
205
|
+
user.attributes = {:prefers_hot_salsa => false, :preferred_color => 'red'}
|
206
|
+
user.save!
|
207
|
+
```
|
189
208
|
|
190
209
|
Preferences are stored in a separate table called "preferences".
|
191
210
|
|
192
|
-
|
211
|
+
### Tracking changes
|
193
212
|
|
194
213
|
Similar to ActiveRecord attributes, unsaved changes to preferences can be
|
195
214
|
tracked. For example,
|
196
215
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
216
|
+
```ruby
|
217
|
+
user.preferred_language # => "English"
|
218
|
+
user.preferred_language_changed? # => false
|
219
|
+
user.preferred_language = 'Spanish'
|
220
|
+
user.preferred_language_changed? # => true
|
221
|
+
user.preferred_language_was # => "English"
|
222
|
+
user.preferred_language_change # => ["English", "Spanish"]
|
223
|
+
user.reset_preferred_language!
|
224
|
+
user.preferred_language # => "English"
|
225
|
+
```
|
205
226
|
|
206
227
|
Assigning the same value leaves the preference unchanged:
|
207
228
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
229
|
+
```ruby
|
230
|
+
user.preferred_language # => "English"
|
231
|
+
user.preferred_language = 'English'
|
232
|
+
user.preferred_language_changed? # => false
|
233
|
+
user.preferred_language_change # => nil
|
234
|
+
```
|
235
|
+
|
236
|
+
## Testing
|
237
|
+
|
238
|
+
You can run the tests by doing
|
212
239
|
|
213
|
-
|
240
|
+
rake
|
214
241
|
|
215
|
-
|
216
|
-
|
242
|
+
## Original List of Resources
|
243
|
+
|
244
|
+
API
|
245
|
+
|
246
|
+
* http://rdoc.info/github/pluginaweek/preferences/master/frames
|
247
|
+
|
248
|
+
Bugs
|
249
|
+
|
250
|
+
* http://github.com/pluginaweek/preferences/issues
|
217
251
|
|
218
|
-
|
252
|
+
Development
|
253
|
+
|
254
|
+
* http://github.com/pluginaweek/preferences
|
255
|
+
|
256
|
+
Testing
|
257
|
+
|
258
|
+
* http://travis-ci.org/pluginaweek/preferences
|
259
|
+
|
260
|
+
Source
|
261
|
+
|
262
|
+
* git://github.com/pluginaweek/preferences.git
|
263
|
+
|
264
|
+
Mailing List
|
265
|
+
|
266
|
+
* http://groups.google.com/group/pluginaweek-talk
|
219
267
|
|
220
|
-
rake test RAILS_FRAMEWORK_ROOT=/path/to/rails
|
221
268
|
|
222
|
-
|
269
|
+
## Dependencies
|
223
270
|
|
271
|
+
* 1.0.x for Rails 4.2
|
224
272
|
* 0.5.6 for Rails 4.1
|