enumerate_it 1.2.8 → 1.2.9
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 +4 -4
- data/.gitignore +3 -24
- data/{spec/spec.opts → .rspec} +0 -0
- data/.travis.yml +17 -0
- data/Appraisals +6 -0
- data/CHANGELOG.md +1 -0
- data/Gemfile.lock +33 -30
- data/{README.markdown → README.md} +96 -104
- data/Rakefile +1 -1
- data/enumerate_it.gemspec +22 -0
- data/gemfiles/activesupport_3_0.gemfile +8 -0
- data/gemfiles/activesupport_3_1.gemfile +8 -0
- data/gemfiles/activesupport_3_2.gemfile +8 -0
- data/gemfiles/activesupport_4_0.gemfile +8 -0
- data/gemfiles/activesupport_4_1.gemfile +8 -0
- data/gemfiles/activesupport_4_2.gemfile +8 -0
- data/lib/enumerate_it.rb +4 -266
- data/lib/enumerate_it/base.rb +3 -2
- data/lib/enumerate_it/version.rb +1 -1
- data/spec/enumerate_it/base_spec.rb +35 -5
- data/spec/enumerate_it_spec.rb +4 -1
- data/spec/spec_helper.rb +12 -12
- data/spec/support/test_classes.rb +1 -1
- metadata +26 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5eea658aa82d9e739fafd57e5ce7f36aba85ec4e
|
4
|
+
data.tar.gz: f82979bf4a625cea197e51e08e18218b9a513a41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fa1e1833a65794f76d022a9bc2a7398e193c08079c38abc8fb2ca5353ee036e511791d3a87557d5ff38cb4529e18fc71ab2ec5cc1c69ed3c6bf9fd1fc6696db
|
7
|
+
data.tar.gz: 98bf568bb99f96ad7e18a0488b532215f3ea19efc7493ce9be98c373282f4428911841212344e5cf7e80ab1809fb513c6ff120198ff348e7158fe84a5f8dbe1b
|
data/.gitignore
CHANGED
@@ -1,24 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
## TEXTMATE
|
5
|
-
*.tmproj
|
6
|
-
tmtags
|
7
|
-
|
8
|
-
## EMACS
|
9
|
-
*~
|
10
|
-
\#*
|
11
|
-
.\#*
|
12
|
-
|
13
|
-
## VIM
|
14
|
-
*.swp
|
15
|
-
tags
|
16
|
-
|
17
|
-
## PROJECT::GENERAL
|
18
|
-
coverage
|
19
|
-
rdoc
|
20
|
-
pkg
|
21
|
-
|
22
|
-
## PROJECT::SPECIFIC
|
23
|
-
enumerate_it-*.gem
|
24
|
-
enumerate_it.gemspec
|
1
|
+
/.bundle/
|
2
|
+
/pkg/
|
3
|
+
/gemfiles/*.lock
|
data/{spec/spec.opts → .rspec}
RENAMED
File without changes
|
data/.travis.yml
CHANGED
@@ -1,3 +1,20 @@
|
|
1
1
|
language: ruby
|
2
|
+
|
3
|
+
sudo: false
|
4
|
+
|
5
|
+
cache: bundler
|
6
|
+
|
2
7
|
rvm:
|
3
8
|
- 1.9.3
|
9
|
+
- 2.0.0
|
10
|
+
- 2.1.5
|
11
|
+
- 2.2.1
|
12
|
+
- 2.3.0
|
13
|
+
|
14
|
+
gemfile:
|
15
|
+
- gemfiles/activesupport_3_0.gemfile
|
16
|
+
- gemfiles/activesupport_3_1.gemfile
|
17
|
+
- gemfiles/activesupport_3_2.gemfile
|
18
|
+
- gemfiles/activesupport_4_0.gemfile
|
19
|
+
- gemfiles/activesupport_4_1.gemfile
|
20
|
+
- gemfiles/activesupport_4_2.gemfile
|
data/Appraisals
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Changes are maintained under [Releases](https://github.com/lucascaton/enumerate_it/releases).
|
data/Gemfile.lock
CHANGED
@@ -1,53 +1,56 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enumerate_it (1.2.
|
4
|
+
enumerate_it (1.2.9)
|
5
5
|
activesupport (>= 3.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
arel (2.0.9)
|
21
|
-
builder (2.1.2)
|
10
|
+
activesupport (4.2.6)
|
11
|
+
i18n (~> 0.7)
|
12
|
+
json (~> 1.7, >= 1.7.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
appraisal (2.1.0)
|
17
|
+
bundler
|
18
|
+
rake
|
19
|
+
thor (>= 0.14.0)
|
22
20
|
coderay (1.0.9)
|
23
|
-
diff-lcs (1.
|
24
|
-
i18n (0.
|
21
|
+
diff-lcs (1.2.5)
|
22
|
+
i18n (0.7.0)
|
23
|
+
json (1.8.3)
|
25
24
|
method_source (0.8.1)
|
25
|
+
minitest (5.8.4)
|
26
26
|
pry (0.9.12)
|
27
27
|
coderay (~> 1.0.5)
|
28
28
|
method_source (~> 0.8)
|
29
29
|
slop (~> 3.4)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
rspec-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
rspec-
|
39
|
-
diff-lcs (~> 1.1.2)
|
40
|
-
rspec-mocks (2.5.0)
|
30
|
+
rake (11.1.1)
|
31
|
+
rspec (2.14.1)
|
32
|
+
rspec-core (~> 2.14.0)
|
33
|
+
rspec-expectations (~> 2.14.0)
|
34
|
+
rspec-mocks (~> 2.14.0)
|
35
|
+
rspec-core (2.14.8)
|
36
|
+
rspec-expectations (2.14.5)
|
37
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
38
|
+
rspec-mocks (2.14.6)
|
41
39
|
slop (3.4.4)
|
42
|
-
|
40
|
+
thor (0.19.1)
|
41
|
+
thread_safe (0.3.5)
|
42
|
+
tzinfo (1.2.2)
|
43
|
+
thread_safe (~> 0.1)
|
43
44
|
|
44
45
|
PLATFORMS
|
45
46
|
ruby
|
46
47
|
|
47
48
|
DEPENDENCIES
|
48
|
-
|
49
|
+
appraisal
|
49
50
|
enumerate_it!
|
50
51
|
pry
|
51
|
-
pry-nav
|
52
52
|
rake
|
53
|
-
rspec (
|
53
|
+
rspec (~> 2.14.1)
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
1.11.2
|
@@ -1,9 +1,10 @@
|
|
1
1
|
# EnumerateIt - Ruby Enumerations
|
2
2
|
|
3
|
-
[](https://travis-ci.org/lucascaton/enumerate_it)
|
4
|
+
[](https://rubygems.org/gems/enumerate_it)
|
5
5
|
|
6
|
-
Author
|
6
|
+
* **Author:** Cássio Marques
|
7
|
+
* **Maintainer:** Lucas Caton
|
7
8
|
|
8
9
|
## Description
|
9
10
|
|
@@ -44,25 +45,23 @@ mean when we say that someone or something is '2'?
|
|
44
45
|
|
45
46
|
Enter EnumerateIt.
|
46
47
|
|
47
|
-
##
|
48
|
+
## Changelog
|
48
49
|
|
49
|
-
|
50
|
-
difference is that on 1.0.0 you need to `extend` the EnumerateIt module inside
|
51
|
-
classes that are going to have enumerated attributes, while in past versions
|
52
|
-
you would use `include`.
|
50
|
+
Changes are maintained under [Releases](https://github.com/lucascaton/enumerate_it/releases).
|
53
51
|
|
54
52
|
## Creating enumerations
|
55
53
|
|
56
|
-
Enumerations are created as models, but you can put
|
57
|
-
application. In Rails applications, you
|
54
|
+
Enumerations are created as models, but you can put them anywhere in your
|
55
|
+
application. In Rails applications, you should put them inside
|
56
|
+
`app/enumerations` folder.
|
58
57
|
|
59
|
-
```
|
58
|
+
```ruby
|
60
59
|
class RelationshipStatus < EnumerateIt::Base
|
61
60
|
associate_values(
|
62
|
-
:
|
63
|
-
:
|
64
|
-
:
|
65
|
-
:
|
61
|
+
single: [1, 'Single'],
|
62
|
+
married: [2, 'Married'],
|
63
|
+
widow: [3, 'Widow'],
|
64
|
+
divorced: [4, 'Divorced']
|
66
65
|
)
|
67
66
|
end
|
68
67
|
```
|
@@ -71,7 +70,7 @@ This will create some nice stuff:
|
|
71
70
|
|
72
71
|
* Each enumeration's value will turn into a constant:
|
73
72
|
|
74
|
-
```
|
73
|
+
```ruby
|
75
74
|
RelationshipStatus::SINGLE
|
76
75
|
#=> 1
|
77
76
|
|
@@ -81,7 +80,7 @@ This will create some nice stuff:
|
|
81
80
|
|
82
81
|
* You can retrieve a list with all the enumeration codes:
|
83
82
|
|
84
|
-
```
|
83
|
+
```ruby
|
85
84
|
RelationshipStatus.list
|
86
85
|
#=> [1, 2, 3, 4]
|
87
86
|
```
|
@@ -89,71 +88,70 @@ This will create some nice stuff:
|
|
89
88
|
* You can get an array of options, ready to use with the 'select',
|
90
89
|
'select_tag', etc family of Rails helpers.
|
91
90
|
|
92
|
-
```
|
91
|
+
```ruby
|
93
92
|
RelationshipStatus.to_a
|
94
93
|
#=> [["Divorced", 4], ["Married", 2], ["Single", 1], ["Widow", 3]]
|
95
94
|
```
|
96
95
|
|
97
96
|
* You can retrieve a list with values for a group of enumeration constants.
|
98
97
|
|
99
|
-
```
|
98
|
+
```ruby
|
100
99
|
RelationshipStatus.values_for %w(MARRIED SINGLE)
|
101
100
|
#=> [2, 1]
|
102
101
|
```
|
103
102
|
|
104
103
|
* You can retrieve the value for a specific enumeration constant:
|
105
104
|
|
106
|
-
```
|
105
|
+
```ruby
|
107
106
|
RelationshipStatus.value_for("MARRIED")
|
108
107
|
#=> 2
|
109
108
|
```
|
110
109
|
|
111
110
|
* You can retrieve the symbol used to declare a specific enumeration value:
|
112
111
|
|
113
|
-
```
|
112
|
+
```ruby
|
114
113
|
RelationshipStatus.key_for(RelationshipStatus::MARRIED)
|
115
114
|
#=> :married
|
116
115
|
```
|
117
116
|
|
118
117
|
* You can iterate over the list of the enumeration's values:
|
119
118
|
|
120
|
-
```
|
119
|
+
```ruby
|
121
120
|
RelationshipStatus.each_value { |value| ... }
|
122
121
|
```
|
123
122
|
|
124
123
|
* You can iterate over the list of the enumeration's translations:
|
125
124
|
|
126
|
-
```
|
125
|
+
```ruby
|
127
126
|
RelationshipStatus.each_translation { |translation| ... }
|
128
127
|
```
|
129
128
|
|
130
129
|
* You can also retrieve all the translations of the enumeration:
|
131
130
|
|
132
|
-
```
|
131
|
+
```ruby
|
133
132
|
RelationshipStatus.translations
|
134
133
|
```
|
135
134
|
|
136
135
|
* You can ask for the enumeration's length:
|
137
136
|
|
138
|
-
```
|
137
|
+
```ruby
|
139
138
|
RelationshipStatus.length
|
140
139
|
#=> 4
|
141
140
|
```
|
142
141
|
|
143
142
|
* You can manipulate the hash used to create the enumeration:
|
144
143
|
|
145
|
-
```
|
144
|
+
```ruby
|
146
145
|
RelationshipStatus.enumeration
|
147
146
|
#=> returns the exact hash used to define the enumeration
|
148
147
|
```
|
149
148
|
|
150
|
-
|
151
149
|
You can also create enumerations in the following ways:
|
152
150
|
|
153
151
|
* Passing an array of symbols, so that the respective value for each symbol
|
154
152
|
will be the stringified version of the symbol itself:
|
155
153
|
|
156
|
-
```
|
154
|
+
```ruby
|
157
155
|
class RelationshipStatus < EnumerateIt::Base
|
158
156
|
associate_values :married, :single
|
159
157
|
end
|
@@ -166,13 +164,12 @@ You can also create enumerations in the following ways:
|
|
166
164
|
translation. In this case, the I18n feature will be used (more on this
|
167
165
|
below):
|
168
166
|
|
169
|
-
```
|
167
|
+
```ruby
|
170
168
|
class RelationshipStatus < EnumerateIt::Base
|
171
|
-
associate_values :
|
169
|
+
associate_values married: 1, single: 2
|
172
170
|
end
|
173
171
|
```
|
174
172
|
|
175
|
-
|
176
173
|
### Defining a default sort mode
|
177
174
|
|
178
175
|
When calling methods like `to_a` and `to_json`, the returned values will be
|
@@ -180,9 +177,9 @@ sorted using the translation for each one of the enumeration values. If you
|
|
180
177
|
want to overwrite the default sort mode, you can use the `sort_by` class
|
181
178
|
method.
|
182
179
|
|
183
|
-
```
|
180
|
+
```ruby
|
184
181
|
class RelationshipStatus < EnumerateIt::Base
|
185
|
-
associate_values :
|
182
|
+
associate_values married: 1, single: 2
|
186
183
|
|
187
184
|
sort_by :value
|
188
185
|
end
|
@@ -198,29 +195,28 @@ The `sort_by` methods accept one of the following values:
|
|
198
195
|
* `:none`: Will return values in order that was passed to associate_values
|
199
196
|
call.
|
200
197
|
|
201
|
-
|
202
198
|
## Using enumerations
|
203
199
|
|
204
200
|
The cool part is that you can use these enumerations with any class, be it an
|
205
201
|
ActiveRecord instance or not.
|
206
202
|
|
207
|
-
```
|
203
|
+
```ruby
|
208
204
|
class Person
|
209
205
|
extend EnumerateIt
|
210
206
|
attr_accessor :relationship_status
|
211
207
|
|
212
|
-
has_enumeration_for :relationship_status, :
|
208
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus
|
213
209
|
end
|
214
210
|
```
|
215
211
|
|
216
|
-
The
|
212
|
+
The `:with` option is not required. If you ommit it, EnumerateIt will try to
|
217
213
|
load an enumeration class based on the camelized attribute name.
|
218
214
|
|
219
215
|
This will create:
|
220
216
|
|
221
217
|
* A humanized description for the values of the enumerated attribute:
|
222
218
|
|
223
|
-
```
|
219
|
+
```ruby
|
224
220
|
p = Person.new
|
225
221
|
p.relationship_status = RelationshipStatus::DIVORCED
|
226
222
|
p.relationship_status_humanize
|
@@ -231,11 +227,11 @@ This will create:
|
|
231
227
|
will use a 'humanized' version of the hash's key to humanize the
|
232
228
|
attribute's value:
|
233
229
|
|
234
|
-
```
|
230
|
+
```ruby
|
235
231
|
class RelationshipStatus < EnumerateIt::Base
|
236
232
|
associate_values(
|
237
|
-
:
|
238
|
-
:
|
233
|
+
married: 1,
|
234
|
+
single: 2
|
239
235
|
)
|
240
236
|
end
|
241
237
|
|
@@ -248,17 +244,17 @@ This will create:
|
|
248
244
|
* The associated enumerations can be retrieved with the 'enumerations' class
|
249
245
|
method.
|
250
246
|
|
251
|
-
```
|
247
|
+
```ruby
|
252
248
|
Person.enumerations[:relationship_status]
|
253
249
|
#=> RelationshipStatus
|
254
250
|
```
|
255
251
|
|
256
|
-
* If you pass the
|
252
|
+
* If you pass the `:create_helpers` option as `true`, it will create a helper
|
257
253
|
method for each enumeration option (this option defaults to false):
|
258
254
|
|
259
|
-
```
|
255
|
+
```ruby
|
260
256
|
class Person < ActiveRecord::Base
|
261
|
-
has_enumeration_for :relationship_status, :
|
257
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus, create_helpers: true
|
262
258
|
end
|
263
259
|
|
264
260
|
p = Person.new
|
@@ -272,12 +268,13 @@ This will create:
|
|
272
268
|
```
|
273
269
|
|
274
270
|
* It's also possible to "namespace" the created helper methods, passing a
|
275
|
-
hash to the
|
271
|
+
hash to the `:create_helpers` option. This can be useful when two or more of
|
276
272
|
the enumerations used share the same constants.
|
277
273
|
|
278
|
-
```
|
274
|
+
```ruby
|
279
275
|
class Person < ActiveRecord::Base
|
280
|
-
has_enumeration_for :relationship_status, :
|
276
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus,
|
277
|
+
create_helpers: { prefix: true }
|
281
278
|
end
|
282
279
|
|
283
280
|
p = Person.new
|
@@ -293,7 +290,7 @@ This will create:
|
|
293
290
|
* You can define polymorphic behavior for the enum values, so you can define
|
294
291
|
a class for each of them:
|
295
292
|
|
296
|
-
```
|
293
|
+
```ruby
|
297
294
|
class RelationshipStatus < EnumerateIt::Base
|
298
295
|
associate_values :married, :single
|
299
296
|
|
@@ -311,7 +308,8 @@ This will create:
|
|
311
308
|
end
|
312
309
|
|
313
310
|
class Person < ActiveRecord::Base
|
314
|
-
has_enumeration_for :relationship_status, :
|
311
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus,
|
312
|
+
create_helpers: { polymorphic: true }
|
315
313
|
end
|
316
314
|
|
317
315
|
p = Person.new
|
@@ -324,22 +322,23 @@ This will create:
|
|
324
322
|
#=> "Party Hard!"
|
325
323
|
```
|
326
324
|
|
327
|
-
You can also change the suffix '_object', using the
|
325
|
+
You can also change the suffix '_object', using the `:suffix` option:
|
328
326
|
|
329
|
-
```
|
327
|
+
```ruby
|
330
328
|
class Person < ActiveRecord::Base
|
331
|
-
has_enumeration_for :relationship_status, :
|
329
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus,
|
330
|
+
create_helpers: { polymorphic: { suffix: '_mode' } }
|
332
331
|
end
|
333
332
|
|
334
333
|
p.relationship_status_mode.saturday_night
|
335
334
|
```
|
336
335
|
|
337
|
-
* The
|
336
|
+
* The `:create_helpers` also creates some mutator helper methods, that can be
|
338
337
|
used to change the attribute's value.
|
339
338
|
|
340
|
-
```
|
339
|
+
```ruby
|
341
340
|
class Person < ActiveRecord::Base
|
342
|
-
has_enumeration_for :relationship_status, :
|
341
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus, create_helpers: true
|
343
342
|
end
|
344
343
|
|
345
344
|
p = Person.new
|
@@ -352,39 +351,38 @@ This will create:
|
|
352
351
|
#=> false
|
353
352
|
```
|
354
353
|
|
355
|
-
* If you pass the
|
354
|
+
* If you pass the `:create_scopes` option as `true`, it will create a scope
|
356
355
|
method for each enumeration option (this option defaults to false):
|
357
356
|
|
358
|
-
```
|
357
|
+
```ruby
|
359
358
|
class Person < ActiveRecord::Base
|
360
|
-
has_enumeration_for :relationship_status, :
|
359
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus, create_scopes: true
|
361
360
|
end
|
362
361
|
|
363
362
|
Person.married.to_sql
|
364
363
|
#=> SELECT "people".* FROM "people" WHERE "people"."relationship_status" = 1
|
365
364
|
```
|
366
365
|
|
367
|
-
The
|
366
|
+
The `:create_scopes` also accepts :prefix option.
|
368
367
|
|
369
|
-
```
|
368
|
+
```ruby
|
370
369
|
class Person < ActiveRecord::Base
|
371
|
-
has_enumeration_for :relationship_status, :
|
370
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus,
|
371
|
+
create_scopes: { prefix: true }
|
372
372
|
end
|
373
373
|
|
374
374
|
Person.relationship_status_married.to_sql
|
375
375
|
```
|
376
376
|
|
377
|
-
NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
|
378
|
-
|
379
377
|
* If your class can manage validations and responds to
|
380
378
|
:validates_inclusion_of, it will create this validation:
|
381
379
|
|
382
|
-
```
|
380
|
+
```ruby
|
383
381
|
class Person < ActiveRecord::Base
|
384
|
-
has_enumeration_for :relationship_status, :
|
382
|
+
has_enumeration_for :relationship_status, with: RelationshipStatus
|
385
383
|
end
|
386
384
|
|
387
|
-
p = Person.new(:
|
385
|
+
p = Person.new(relationship_status: 6) # there is no '6' value in the enumeration
|
388
386
|
p.valid?
|
389
387
|
#=> false
|
390
388
|
p.errors[:relationship_status]
|
@@ -392,45 +390,44 @@ NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
|
|
392
390
|
```
|
393
391
|
|
394
392
|
* If your class can manage validations and responds to
|
395
|
-
|
393
|
+
`:validates_presence_of`, you can pass the :required options as true and
|
396
394
|
this validation will be created for you (this option defaults to false):
|
397
395
|
|
398
|
-
```
|
396
|
+
```ruby
|
399
397
|
class Person < ActiveRecord::Base
|
400
|
-
has_enumeration_for :relationship_status, :
|
398
|
+
has_enumeration_for :relationship_status, required: true
|
401
399
|
end
|
402
400
|
|
403
|
-
p = Person.new :
|
401
|
+
p = Person.new relationship_status: nil
|
404
402
|
p.valid?
|
405
403
|
#=> false
|
406
404
|
p.errors[:relationship_status]
|
407
405
|
#=> "can't be blank"
|
408
406
|
```
|
409
407
|
|
410
|
-
|
411
|
-
|
412
|
-
only to those derived from ActiveRecord::Base.
|
408
|
+
Remember that you can add validations to any kind of class and not only to
|
409
|
+
those derived from ActiveRecord::Base.
|
413
410
|
|
414
411
|
## I18n
|
415
412
|
|
416
413
|
I18n lookup is provided on both `_humanized` and `Enumeration#to_a` methods,
|
417
414
|
given the hash key is a Symbol. The I18n strings are located on
|
418
|
-
enumerations
|
415
|
+
`enumerations.<enumeration_name>.<key>`:
|
419
416
|
|
420
|
-
```
|
421
|
-
#
|
417
|
+
```yaml
|
418
|
+
# Your locale file
|
422
419
|
pt:
|
423
420
|
enumerations:
|
424
421
|
relationship_status:
|
425
422
|
married: Casado
|
426
423
|
```
|
427
424
|
|
428
|
-
```
|
425
|
+
```ruby
|
429
426
|
class RelationshipStatus < EnumerateIt::Base
|
430
427
|
associate_values(
|
431
|
-
:
|
432
|
-
:
|
433
|
-
:
|
428
|
+
married: 1,
|
429
|
+
single: 2,
|
430
|
+
divorced: [3, "He's divorced"]
|
434
431
|
)
|
435
432
|
end
|
436
433
|
|
@@ -450,14 +447,14 @@ p.relationship_status_humanize # uses the provided string
|
|
450
447
|
|
451
448
|
You can also translate specific values:
|
452
449
|
|
453
|
-
```
|
450
|
+
```ruby
|
454
451
|
RelationshipStatus.t(1)
|
455
452
|
#=> 'Casado'
|
456
453
|
```
|
457
454
|
|
458
455
|
## Installation
|
459
456
|
|
460
|
-
```
|
457
|
+
```bash
|
461
458
|
gem install enumerate_it
|
462
459
|
```
|
463
460
|
|
@@ -465,36 +462,30 @@ gem install enumerate_it
|
|
465
462
|
|
466
463
|
* Add the gem to your Gemfile:
|
467
464
|
|
468
|
-
```
|
469
|
-
gem
|
465
|
+
```ruby
|
466
|
+
gem 'enumerate_it'
|
470
467
|
```
|
471
468
|
|
472
469
|
* Run the install generator:
|
473
470
|
|
474
|
-
```
|
471
|
+
```bash
|
475
472
|
rails generate enumerate_it:install
|
476
473
|
```
|
477
474
|
|
478
|
-
|
479
475
|
An interesting approach to use it in Rails apps is to create an
|
480
|
-
app/enumerations folder
|
481
|
-
config/application.rb:
|
482
|
-
|
483
|
-
``` ruby
|
484
|
-
module YourApp
|
485
|
-
class Application < Rails::Application
|
486
|
-
config.autoload_paths << "#{Rails.root}/app/enumerations"
|
487
|
-
end
|
488
|
-
end
|
489
|
-
```
|
476
|
+
`app/enumerations` folder.
|
490
477
|
|
491
478
|
There is also a Rails Generator that you can use to generate enumerations and
|
492
|
-
their locale files. Take a look at how to use it running
|
479
|
+
their locale files. Take a look at how to use it running:
|
493
480
|
|
494
|
-
```
|
481
|
+
```bash
|
495
482
|
rails generate enumerate_it:enum --help
|
496
483
|
```
|
497
484
|
|
485
|
+
## Supported versions
|
486
|
+
|
487
|
+
Check [travis config file](https://github.com/lucascaton/enumerate_it/blob/master/.travis.yml).
|
488
|
+
|
498
489
|
## Why did you reinvent the wheel?
|
499
490
|
|
500
491
|
There are other similar solutions to the problem out there, but I could not
|
@@ -503,9 +494,9 @@ codes. I had both situations in my legacy database.
|
|
503
494
|
|
504
495
|
## Why defining enumerations outside the class that use it?
|
505
496
|
|
506
|
-
*
|
507
|
-
*
|
508
|
-
*
|
497
|
+
* I think it's cleaner.
|
498
|
+
* You can add behaviour to the enumeration class.
|
499
|
+
* You can reuse the enumeration inside other classes.
|
509
500
|
|
510
501
|
## Note on Patches/Pull Requests
|
511
502
|
|
@@ -513,11 +504,12 @@ codes. I had both situations in my legacy database.
|
|
513
504
|
* Make your feature addition or bug fix.
|
514
505
|
* Add tests for it. This is important so I don't break it in a future
|
515
506
|
version unintentionally.
|
516
|
-
*
|
507
|
+
* Run the tests agaist all supported versions: `$ appraisal rake`.
|
508
|
+
* Commit, do not mess with Rakefile, version, or history. (if you want to
|
517
509
|
have your own version, that is fine but bump version in a commit by itself
|
518
510
|
I can ignore when I pull)
|
519
511
|
* Send me a pull request. Bonus points for topic branches.
|
520
512
|
|
521
513
|
## Copyright
|
522
514
|
|
523
|
-
Copyright (c) 2010 Cássio Marques. See LICENSE for details.
|
515
|
+
Copyright (c) 2010-2016 Cássio Marques and Lucas Caton. See LICENSE for details.
|