enumerize 0.8.0 → 2.6.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 +5 -5
- data/.github/workflows/ruby.yml +73 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +210 -0
- data/Gemfile +4 -21
- data/Gemfile.global +12 -0
- data/Gemfile.mongo_mapper +6 -0
- data/Gemfile.rails60 +6 -0
- data/Gemfile.rails61 +6 -0
- data/Gemfile.rails70 +9 -0
- data/Gemfile.railsmaster +5 -0
- data/README.md +366 -73
- data/Rakefile +4 -4
- data/enumerize.gemspec +2 -1
- data/lib/enumerize/activemodel.rb +47 -0
- data/lib/enumerize/activerecord.rb +102 -27
- data/lib/enumerize/attribute.rb +59 -15
- data/lib/enumerize/attribute_map.rb +2 -0
- data/lib/enumerize/base.rb +32 -17
- data/lib/enumerize/hooks/formtastic.rb +7 -9
- data/lib/enumerize/hooks/sequel_dataset.rb +17 -0
- data/lib/enumerize/hooks/simple_form.rb +9 -12
- data/lib/enumerize/hooks/uniqueness.rb +7 -8
- data/lib/enumerize/integrations/rails_admin.rb +3 -1
- data/lib/enumerize/integrations/rspec/matcher.rb +112 -26
- data/lib/enumerize/integrations/rspec.rb +3 -0
- data/lib/enumerize/module.rb +2 -0
- data/lib/enumerize/module_attributes.rb +2 -0
- data/lib/enumerize/mongoid.rb +36 -0
- data/lib/enumerize/predicatable.rb +10 -17
- data/lib/enumerize/predicates.rb +5 -1
- data/lib/enumerize/scope/activerecord.rb +53 -0
- data/lib/enumerize/scope/mongoid.rb +50 -0
- data/lib/enumerize/scope/sequel.rb +56 -0
- data/lib/enumerize/sequel.rb +62 -0
- data/lib/enumerize/set.rb +20 -8
- data/lib/enumerize/utils.rb +12 -0
- data/lib/enumerize/value.rb +20 -20
- data/lib/enumerize/version.rb +3 -1
- data/lib/enumerize.rb +33 -2
- data/lib/sequel/plugins/enumerize.rb +18 -0
- data/spec/enumerize/integrations/rspec/matcher_spec.rb +261 -0
- data/spec/spec_helper.rb +30 -0
- data/test/activemodel_test.rb +114 -0
- data/test/activerecord_test.rb +434 -58
- data/test/attribute_map_test.rb +9 -7
- data/test/attribute_test.rb +52 -23
- data/test/base_test.rb +118 -66
- data/test/formtastic_test.rb +28 -12
- data/test/module_attributes_test.rb +10 -8
- data/test/mongo_mapper_test.rb +26 -11
- data/test/mongoid_test.rb +100 -15
- data/test/multiple_test.rb +41 -12
- data/test/predicates_test.rb +34 -26
- data/test/rails_admin_test.rb +8 -6
- data/test/sequel_test.rb +342 -0
- data/test/set_test.rb +42 -26
- data/test/simple_form_test.rb +25 -1
- data/test/support/mock_controller.rb +6 -0
- data/test/support/shared_enums.rb +43 -0
- data/test/support/view_test_helper.rb +18 -1
- data/test/test_helper.rb +33 -2
- data/test/value_test.rb +79 -28
- metadata +51 -12
- data/.travis.yml +0 -19
- data/Gemfile.rails4 +0 -23
- data/lib/enumerize/form_helper.rb +0 -23
- data/test/rspec_matcher_test.rb +0 -76
- data/test/rspec_spec.rb +0 -13
data/README.md
CHANGED
@@ -1,6 +1,34 @@
|
|
1
|
-
# Enumerize [](https://github.com/brainspec/enumerize/actions/workflows/ruby.yml)
|
2
|
+
|
3
|
+
Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper/Sequel support
|
4
|
+
|
5
|
+
## Table of Contents
|
6
|
+
|
7
|
+
- [Installation](#installation)
|
8
|
+
- [Supported Versions](#supported-versions)
|
9
|
+
- [Usage](#usage)
|
10
|
+
- [Database support](#database-support)
|
11
|
+
- [ActiveRecord](#activerecord)
|
12
|
+
- [Mongoid](#mongoid)
|
13
|
+
- [MongoMapper](#mongomapper)
|
14
|
+
- [I18n Support](#i18n-support)
|
15
|
+
- [I18n Helper Methods](#i18n-helper-methods)
|
16
|
+
- [Boolean Helper Methods](#boolean-helper-methods)
|
17
|
+
- [Basic](#basic)
|
18
|
+
- [Predicate Methods](#predicate-methods)
|
19
|
+
- [Optimzations and Tips](#optimzations-and-tips)
|
20
|
+
- [Extendable Module](#extendable-module)
|
21
|
+
- [Customizing Enumerize Value](#customizing-enumerize-value)
|
22
|
+
- [ActiveRecord scopes](#activerecord-scopes)
|
23
|
+
- [Array-like Attributes](#array-like-attributes)
|
24
|
+
- [Forms](#forms)
|
25
|
+
- [SimpleForm](#simpleform)
|
26
|
+
- [Formtastic](#formtastic)
|
27
|
+
- [Testing](#testing)
|
28
|
+
- [RSpec](#rspec)
|
29
|
+
- [Minitest with Shoulda](#minitest-with-shoulda)
|
30
|
+
- [Other Integrations](#other-integrations)
|
31
|
+
- [Contributing](#contributing)
|
4
32
|
|
5
33
|
## Installation
|
6
34
|
|
@@ -16,6 +44,11 @@ Or install it yourself as:
|
|
16
44
|
|
17
45
|
$ gem install enumerize
|
18
46
|
|
47
|
+
## Supported Versions
|
48
|
+
|
49
|
+
- Ruby 2.7+
|
50
|
+
- Rails 5.2+
|
51
|
+
|
19
52
|
## Usage
|
20
53
|
|
21
54
|
Basic:
|
@@ -24,26 +57,52 @@ Basic:
|
|
24
57
|
class User
|
25
58
|
extend Enumerize
|
26
59
|
|
27
|
-
enumerize :
|
60
|
+
enumerize :role, in: [:user, :admin]
|
28
61
|
end
|
29
62
|
```
|
30
63
|
|
31
|
-
Note that enumerized values are just
|
64
|
+
Note that enumerized values are just identifiers so if you want to use multi-word, etc. values then you should use `I18n` feature.
|
32
65
|
|
66
|
+
---
|
33
67
|
|
34
|
-
|
68
|
+
## Database support
|
69
|
+
|
70
|
+
### ActiveRecord
|
35
71
|
|
36
72
|
```ruby
|
73
|
+
class CreateUsers < ActiveRecord::Migration
|
74
|
+
def change
|
75
|
+
create_table :users do |t|
|
76
|
+
t.string :status
|
77
|
+
t.string :role
|
78
|
+
|
79
|
+
t.timestamps
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
37
84
|
class User < ActiveRecord::Base
|
38
85
|
extend Enumerize
|
39
86
|
|
40
|
-
enumerize :
|
87
|
+
enumerize :status, in: [:student, :employed, :retired], default: lambda { |user| StatusIdentifier.status_for_age(user.age).to_sym }
|
41
88
|
|
42
89
|
enumerize :role, in: [:user, :admin], default: :user
|
43
90
|
end
|
44
91
|
```
|
45
92
|
|
46
|
-
|
93
|
+
:warning: By default, `enumerize` adds `inclusion` validation to the model. You can skip validations by passing `skip_validations` option. :warning:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class User < ActiveRecord::Base
|
97
|
+
extend Enumerize
|
98
|
+
|
99
|
+
enumerize :status, in: [:student, :employed, :retired], skip_validations: lambda { |user| user.new_record? }
|
100
|
+
|
101
|
+
enumerize :role, in: [:user, :admin], skip_validations: true
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
### Mongoid
|
47
106
|
|
48
107
|
```ruby
|
49
108
|
class User
|
@@ -55,7 +114,7 @@ class User
|
|
55
114
|
end
|
56
115
|
```
|
57
116
|
|
58
|
-
MongoMapper
|
117
|
+
### MongoMapper
|
59
118
|
|
60
119
|
```ruby
|
61
120
|
class User
|
@@ -67,49 +126,54 @@ class User
|
|
67
126
|
end
|
68
127
|
```
|
69
128
|
|
70
|
-
|
129
|
+
---
|
130
|
+
|
131
|
+
## I18n Support
|
71
132
|
|
72
133
|
```ruby
|
73
134
|
en:
|
74
135
|
enumerize:
|
75
136
|
user:
|
76
|
-
|
77
|
-
|
78
|
-
|
137
|
+
status:
|
138
|
+
student: "Student"
|
139
|
+
employed: "Employed"
|
140
|
+
retired: "Retiree"
|
79
141
|
```
|
80
142
|
|
81
|
-
or if you use `
|
143
|
+
or if you use `status` attribute across several models you can use `defaults` scope:
|
82
144
|
|
83
145
|
```ruby
|
84
146
|
en:
|
85
147
|
enumerize:
|
86
|
-
|
87
|
-
|
88
|
-
|
148
|
+
defaults:
|
149
|
+
status:
|
150
|
+
student: "Student"
|
151
|
+
employed: "Employed"
|
152
|
+
retired: "Retiree"
|
89
153
|
```
|
90
154
|
|
91
|
-
You can also pass `i18n_scope` option to specify scope (or array of scopes)
|
92
|
-
|
155
|
+
You can also pass `i18n_scope` option to specify scope (or array of scopes) storing the translations.
|
93
156
|
|
94
157
|
```ruby
|
95
158
|
class Person
|
96
159
|
extend Enumerize
|
97
160
|
extend ActiveModel::Naming
|
98
161
|
|
99
|
-
enumerize :
|
100
|
-
enumerize :
|
162
|
+
enumerize :status, in: %w[student employed retired], i18n_scope: "status"
|
163
|
+
enumerize :roles, in: %w[user admin], i18n_scope: ["user.roles", "roles"]
|
101
164
|
end
|
102
165
|
|
103
166
|
# localization file
|
104
167
|
en:
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
168
|
+
status:
|
169
|
+
student: "Student"
|
170
|
+
employed: "Employed"
|
171
|
+
retired: "Retiree"
|
172
|
+
user:
|
173
|
+
roles:
|
174
|
+
user: "User"
|
175
|
+
roles:
|
176
|
+
admin: "Admin"
|
113
177
|
```
|
114
178
|
|
115
179
|
Note that if you want to use I18n feature with plain Ruby object don't forget to extend it with `ActiveModel::Naming`:
|
@@ -121,87 +185,120 @@ class User
|
|
121
185
|
end
|
122
186
|
```
|
123
187
|
|
124
|
-
|
188
|
+
### I18n Helper Methods
|
189
|
+
|
190
|
+
#### \*\_text / .text
|
191
|
+
|
192
|
+
Attribute's I18n text value:
|
125
193
|
|
126
194
|
```ruby
|
127
|
-
@user.
|
195
|
+
@user.status_text # or @user.status.text
|
128
196
|
```
|
129
197
|
|
130
|
-
|
198
|
+
#### values
|
199
|
+
|
200
|
+
List of possible values for an enumerized attribute:
|
131
201
|
|
132
202
|
```ruby
|
133
|
-
User.
|
203
|
+
User.status.values # or User.enumerized_attributes[:status].values
|
204
|
+
# => ['student', 'employed', 'retired']
|
134
205
|
```
|
135
206
|
|
136
|
-
|
207
|
+
#### I18n text values
|
208
|
+
|
209
|
+
List of possible I18n text values for an enumerized attribute:
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
User.status.values.collect(&:text)
|
213
|
+
# => ['Student', 'Employed', 'Retiree']
|
214
|
+
```
|
215
|
+
|
216
|
+
#### Form example
|
217
|
+
|
218
|
+
Use it with forms (it supports `:only` and `:except` options):
|
137
219
|
|
138
220
|
```erb
|
139
221
|
<%= form_for @user do |f| %>
|
140
|
-
<%= f.select :
|
222
|
+
<%= f.select :status, User.status.options %>
|
141
223
|
<% end %>
|
142
224
|
```
|
143
225
|
|
144
|
-
|
226
|
+
---
|
227
|
+
|
228
|
+
## Boolean Helper Methods
|
229
|
+
|
230
|
+
### Basic
|
145
231
|
|
146
232
|
```ruby
|
147
|
-
user.
|
148
|
-
user.
|
149
|
-
user.
|
233
|
+
user.status = :student
|
234
|
+
user.status.student? #=> true
|
235
|
+
user.status.retired? #=> false
|
150
236
|
```
|
151
237
|
|
152
|
-
Predicate
|
238
|
+
### Predicate Methods
|
153
239
|
|
154
240
|
```ruby
|
155
241
|
class User
|
156
242
|
extend Enumerize
|
157
243
|
|
158
|
-
enumerize :
|
244
|
+
enumerize :status, in: %w(student employed retired), predicates: true
|
159
245
|
end
|
160
246
|
|
161
247
|
user = User.new
|
162
248
|
|
163
|
-
user.
|
164
|
-
user.
|
249
|
+
user.student? # => false
|
250
|
+
user.employed? # => false
|
165
251
|
|
166
|
-
user.
|
252
|
+
user.status = :student
|
167
253
|
|
168
|
-
user.
|
169
|
-
user.
|
254
|
+
user.student? # => true
|
255
|
+
user.employed? # => false
|
170
256
|
```
|
171
257
|
|
172
|
-
|
258
|
+
:warning: If `enumerize` is used with Mongoid, it's not recommended to use `"writer"` as a field value since `writer?` is defined by Mongoid. [See more](https://github.com/brainspec/enumerize/issues/235). :warning:
|
259
|
+
|
260
|
+
#### Predicate Prefixes
|
173
261
|
|
174
262
|
```ruby
|
175
263
|
class User
|
176
264
|
extend Enumerize
|
177
265
|
|
178
|
-
enumerize :
|
266
|
+
enumerize :status, in: %w(student employed retired), predicates: { prefix: true }
|
179
267
|
end
|
180
268
|
|
181
269
|
user = User.new
|
182
|
-
user.
|
183
|
-
user.
|
270
|
+
user.status = 'student'
|
271
|
+
user.status_student? # => true
|
184
272
|
```
|
273
|
+
|
185
274
|
Use `:only` and `:except` options to specify what values create predicate methods for.
|
186
275
|
|
276
|
+
---
|
277
|
+
|
278
|
+
## Optimzations and Tips
|
279
|
+
|
280
|
+
### Extendable Module
|
281
|
+
|
187
282
|
To make some attributes shared across different classes it's possible to define them in a separate module and then include it into classes:
|
188
283
|
|
189
284
|
```ruby
|
190
|
-
module
|
285
|
+
module RoleEnumerations
|
191
286
|
extend Enumerize
|
192
287
|
|
193
|
-
enumerize :
|
288
|
+
enumerize :roles, in: %w[user admin]
|
194
289
|
end
|
195
290
|
|
196
|
-
class
|
197
|
-
include
|
291
|
+
class Buyer
|
292
|
+
include RoleEnumerations
|
198
293
|
end
|
199
294
|
|
200
|
-
class
|
201
|
-
include
|
295
|
+
class Seller
|
296
|
+
include RoleEnumerations
|
202
297
|
end
|
203
298
|
```
|
204
299
|
|
300
|
+
### Customizing Enumerize Value
|
301
|
+
|
205
302
|
It's also possible to store enumerized attribute value using custom values (e.g. integers). You can pass a hash as `:in` option to achieve this:
|
206
303
|
|
207
304
|
```ruby
|
@@ -220,25 +317,49 @@ User.role.find_value(:user).value #=> 1
|
|
220
317
|
User.role.find_value(:admin).value #=> 2
|
221
318
|
```
|
222
319
|
|
223
|
-
ActiveRecord scopes:
|
320
|
+
### ActiveRecord scopes:
|
321
|
+
|
322
|
+
#### Basic
|
224
323
|
|
225
324
|
```ruby
|
226
325
|
class User < ActiveRecord::Base
|
227
326
|
extend Enumerize
|
228
|
-
enumerize :
|
229
|
-
enumerize :status, :in => {
|
327
|
+
enumerize :role, :in => [:user, :admin], scope: true
|
328
|
+
enumerize :status, :in => { student: 1, employed: 2, retired: 3 }, scope: :having_status
|
230
329
|
end
|
231
330
|
|
232
|
-
User.
|
233
|
-
# SELECT "users".* FROM "users" WHERE "users"."
|
331
|
+
User.with_role(:admin)
|
332
|
+
# SELECT "users".* FROM "users" WHERE "users"."role" IN ('admin')
|
333
|
+
|
334
|
+
User.without_role(:admin)
|
335
|
+
# SELECT "users".* FROM "users" WHERE "users"."role" NOT IN ('admin')
|
336
|
+
|
337
|
+
User.having_status(:employed).with_role(:user, :admin)
|
338
|
+
# SELECT "users".* FROM "users" WHERE "users"."status" IN (2) AND "users"."role" IN ('user', 'admin')
|
339
|
+
```
|
340
|
+
|
341
|
+
#### Shallow Scopes
|
342
|
+
|
343
|
+
Adds named scopes to the class directly.
|
344
|
+
|
345
|
+
```ruby
|
346
|
+
class User < ActiveRecord::Base
|
347
|
+
extend Enumerize
|
348
|
+
enumerize :status, :in => [:student, :employed, :retired], scope: :shallow
|
349
|
+
enumerize :role, :in => { user: 1, admin: 2 }, scope: :shallow
|
350
|
+
end
|
234
351
|
|
235
|
-
User.
|
236
|
-
# SELECT "users".* FROM "users" WHERE "users"."
|
352
|
+
User.student
|
353
|
+
# SELECT "users".* FROM "users" WHERE "users"."status" = 'student'
|
237
354
|
|
238
|
-
User.
|
239
|
-
# SELECT "users".* FROM "users" WHERE "users"."
|
355
|
+
User.admin
|
356
|
+
# SELECT "users".* FROM "users" WHERE "users"."role" = 2
|
240
357
|
```
|
241
358
|
|
359
|
+
:warning: It is not possible to define a scope when using the `:multiple` option. :warning:
|
360
|
+
|
361
|
+
### Array-like Attributes
|
362
|
+
|
242
363
|
Array-like attributes with plain ruby objects:
|
243
364
|
|
244
365
|
```ruby
|
@@ -264,13 +385,35 @@ class User < ActiveRecord::Base
|
|
264
385
|
end
|
265
386
|
```
|
266
387
|
|
388
|
+
get an array of all text values:
|
389
|
+
|
390
|
+
```ruby
|
391
|
+
@user.interests.texts # shortcut for @user.interests.map(&:text)
|
392
|
+
```
|
393
|
+
|
394
|
+
Also, the reader method can be overridden, referencing the enumerized attribute value using `super`:
|
395
|
+
|
396
|
+
```ruby
|
397
|
+
def status
|
398
|
+
if current_user.admin?
|
399
|
+
"Super #{super}"
|
400
|
+
else
|
401
|
+
super
|
402
|
+
end
|
403
|
+
end
|
404
|
+
```
|
405
|
+
|
406
|
+
---
|
407
|
+
|
408
|
+
## Forms
|
409
|
+
|
267
410
|
### SimpleForm
|
268
411
|
|
269
412
|
If you are using SimpleForm gem you don't need to specify input type (`:select` by default) and collection:
|
270
413
|
|
271
414
|
```erb
|
272
415
|
<%= simple_form_for @user do |f| %>
|
273
|
-
<%= f.input :
|
416
|
+
<%= f.input :status %>
|
274
417
|
<% end %>
|
275
418
|
```
|
276
419
|
|
@@ -278,17 +421,19 @@ and if you want it as radio buttons:
|
|
278
421
|
|
279
422
|
```erb
|
280
423
|
<%= simple_form_for @user do |f| %>
|
281
|
-
<%= f.input :
|
424
|
+
<%= f.input :status, :as => :radio_buttons %>
|
282
425
|
<% end %>
|
283
426
|
```
|
284
427
|
|
428
|
+
Please note that Enumerize overwrites the I18n keys of SimpleForm collections. The enumerized keys are used instead of the SimpleForm ones for inputs concerning enumerized attributes. If you don't want this just pass `:collection` option to the `input` call.
|
429
|
+
|
285
430
|
### Formtastic
|
286
431
|
|
287
432
|
If you are using Formtastic gem you also don't need to specify input type (`:select` by default) and collection:
|
288
433
|
|
289
434
|
```erb
|
290
435
|
<%= semantic_form_for @user do |f| %>
|
291
|
-
<%= f.input :
|
436
|
+
<%= f.input :status %>
|
292
437
|
<% end %>
|
293
438
|
```
|
294
439
|
|
@@ -296,10 +441,14 @@ and if you want it as radio buttons:
|
|
296
441
|
|
297
442
|
```erb
|
298
443
|
<%= semantic_form_for @user do |f| %>
|
299
|
-
<%= f.input :
|
444
|
+
<%= f.input :status, :as => :radio %>
|
300
445
|
<% end %>
|
301
446
|
```
|
302
447
|
|
448
|
+
---
|
449
|
+
|
450
|
+
## Testing
|
451
|
+
|
303
452
|
### RSpec
|
304
453
|
|
305
454
|
Also you can use builtin RSpec matcher:
|
@@ -308,12 +457,155 @@ Also you can use builtin RSpec matcher:
|
|
308
457
|
class User
|
309
458
|
extend Enumerize
|
310
459
|
|
311
|
-
enumerize :
|
460
|
+
enumerize :status, in: [:student, :employed, :retired]
|
461
|
+
end
|
462
|
+
|
463
|
+
describe User do
|
464
|
+
it { should enumerize(:status) }
|
465
|
+
|
466
|
+
# or with RSpec 3 expect syntax
|
467
|
+
it { is_expected.to enumerize(:status) }
|
468
|
+
end
|
469
|
+
```
|
470
|
+
|
471
|
+
#### Qualifiers
|
472
|
+
|
473
|
+
##### in
|
474
|
+
|
475
|
+
Use `in` to test usage of the `:in` option.
|
476
|
+
|
477
|
+
```ruby
|
478
|
+
class User
|
479
|
+
extend Enumerize
|
480
|
+
|
481
|
+
enumerize :status, in: [:student, :employed, :retired]
|
482
|
+
end
|
483
|
+
|
484
|
+
describe User do
|
485
|
+
it { should enumerize(:status).in(:student, :employed, :retired) }
|
486
|
+
end
|
487
|
+
```
|
488
|
+
|
489
|
+
You can test enumerized attribute value using custom values with the `in`
|
490
|
+
qualifier.
|
491
|
+
|
492
|
+
```ruby
|
493
|
+
class User
|
494
|
+
extend Enumerize
|
495
|
+
|
496
|
+
enumerize :role, in: { user: 0, admin: 1 }
|
497
|
+
end
|
498
|
+
|
499
|
+
describe User do
|
500
|
+
it { should enumerize(:role).in(user: 0, admin: 1) }
|
501
|
+
end
|
502
|
+
```
|
503
|
+
|
504
|
+
##### with_default
|
505
|
+
|
506
|
+
Use `with_default` to test usage of the `:default` option.
|
507
|
+
|
508
|
+
```ruby
|
509
|
+
class User
|
510
|
+
extend Enumerize
|
511
|
+
|
512
|
+
enumerize :role, in: [:user, :admin], default: :user
|
513
|
+
end
|
514
|
+
|
515
|
+
describe User do
|
516
|
+
it { should enumerize(:user).in(:user, :admin).with_default(:user) }
|
517
|
+
end
|
518
|
+
```
|
519
|
+
|
520
|
+
##### with_i18n_scope
|
521
|
+
|
522
|
+
Use `with_i18n_scope` to test usage of the `:i18n_scope` option.
|
523
|
+
|
524
|
+
```ruby
|
525
|
+
class User
|
526
|
+
extend Enumerize
|
527
|
+
|
528
|
+
enumerize :status, in: [:student, :employed, :retired], i18n_scope: 'status'
|
529
|
+
end
|
530
|
+
|
531
|
+
describe User do
|
532
|
+
it { should enumerize(:status).in(:student, :employed, :retired).with_i18n_scope('status') }
|
533
|
+
end
|
534
|
+
```
|
535
|
+
|
536
|
+
##### with_predicates
|
537
|
+
|
538
|
+
Use `with_predicates` to test usage of the `:predicates` option.
|
539
|
+
|
540
|
+
```ruby
|
541
|
+
class User
|
542
|
+
extend Enumerize
|
543
|
+
|
544
|
+
enumerize :status, in: [:student, :employed, :retired], predicates: true
|
545
|
+
end
|
546
|
+
|
547
|
+
describe User do
|
548
|
+
it { should enumerize(:status).in(:student, :employed, :retired).with_predicates(true) }
|
549
|
+
end
|
550
|
+
```
|
551
|
+
|
552
|
+
You can text prefixed predicates with the `with_predicates` qualifiers.
|
553
|
+
|
554
|
+
```ruby
|
555
|
+
class User
|
556
|
+
extend Enumerize
|
557
|
+
|
558
|
+
enumerize :status, in: [:student, :employed, :retired], predicates: { prefix: true }
|
559
|
+
end
|
560
|
+
|
561
|
+
describe User do
|
562
|
+
it { should enumerize(:status).in(:student, :employed, :retired).with_predicates(prefix: true) }
|
563
|
+
end
|
564
|
+
```
|
565
|
+
|
566
|
+
##### with_scope
|
567
|
+
|
568
|
+
Use `with_scope` to test usage of the `:scope` option.
|
569
|
+
|
570
|
+
```ruby
|
571
|
+
class User
|
572
|
+
extend Enumerize
|
573
|
+
|
574
|
+
enumerize :status, in: [:student, :employed, :retired], scope: true
|
575
|
+
end
|
576
|
+
|
577
|
+
describe User do
|
578
|
+
it { should enumerize(:status).in(:student, :employed, :retired).with_scope(true) }
|
579
|
+
end
|
580
|
+
```
|
581
|
+
|
582
|
+
You can test a custom scope with the `with_scope` qualifiers.
|
583
|
+
|
584
|
+
```ruby
|
585
|
+
class User
|
586
|
+
extend Enumerize
|
587
|
+
|
588
|
+
enumerize :status, in: [:student, :employed], scope: :employable
|
589
|
+
end
|
590
|
+
|
591
|
+
describe User do
|
592
|
+
it { should enumerize(:status).in(:student, :employed, :retired).with_scope(scope: :employable) }
|
593
|
+
end
|
594
|
+
```
|
595
|
+
|
596
|
+
##### with_multiple
|
597
|
+
|
598
|
+
Use `with_multiple` to test usage of the `:multiple` option.
|
599
|
+
|
600
|
+
```ruby
|
601
|
+
class User
|
602
|
+
extend Enumerize
|
603
|
+
|
604
|
+
enumerize :status, in: [:student, :employed, :retired], multiple: true
|
312
605
|
end
|
313
606
|
|
314
607
|
describe User do
|
315
|
-
it { should enumerize(:
|
316
|
-
it { should enumerize(:sex).in(:male, :female).with_default(:male) }
|
608
|
+
it { should enumerize(:status).in(:student, :employed, :retired).with_multiple(true) }
|
317
609
|
end
|
318
610
|
```
|
319
611
|
|
@@ -336,8 +628,9 @@ end
|
|
336
628
|
|
337
629
|
Enumerize integrates with the following automatically:
|
338
630
|
|
339
|
-
|
631
|
+
- [RailsAdmin](https://github.com/sferik/rails_admin/)
|
340
632
|
|
633
|
+
---
|
341
634
|
|
342
635
|
## Contributing
|
343
636
|
|
data/Rakefile
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
require "bundler/gem_tasks"
|
3
5
|
|
4
6
|
require 'rake/testtask'
|
@@ -10,8 +12,6 @@ Rake::TestTask.new do |t|
|
|
10
12
|
t.verbose = true
|
11
13
|
end
|
12
14
|
|
13
|
-
RSpec::Core::RakeTask.new
|
14
|
-
t.pattern = FileList['test/rspec_spec.rb']
|
15
|
-
end
|
15
|
+
RSpec::Core::RakeTask.new
|
16
16
|
|
17
|
-
task :default => :test
|
17
|
+
task :default => [:test, :spec]
|
data/enumerize.gemspec
CHANGED
@@ -4,6 +4,7 @@ require File.expand_path('../lib/enumerize/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Sergey Nartimov"]
|
6
6
|
gem.email = "team@brainspec.com"
|
7
|
+
gem.licenses = ['MIT']
|
7
8
|
gem.description = %q{Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support}
|
8
9
|
gem.summary = %q{Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support}
|
9
10
|
gem.homepage = "https://github.com/brainspec/enumerize"
|
@@ -15,7 +16,7 @@ Gem::Specification.new do |gem|
|
|
15
16
|
gem.require_paths = ["lib"]
|
16
17
|
gem.version = Enumerize::VERSION
|
17
18
|
|
18
|
-
gem.required_ruby_version = '>=
|
19
|
+
gem.required_ruby_version = '>= 2.7'
|
19
20
|
|
20
21
|
gem.add_dependency('activesupport', '>= 3.2')
|
21
22
|
end
|