rails_attr_enum 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/CHANGELOG.md +4 -0
- data/README.md +160 -100
- data/lib/rails_attr_enum/enum.rb +9 -2
- data/lib/rails_attr_enum/version.rb +1 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +18 -0
- data/test/dummy/log/test.log +7618 -0
- data/test/dummy/spec/label_value_pairs_spec.rb +40 -0
- data/test/dummy/spec/mapped_entries_methods_spec.rb +25 -0
- data/test/dummy/spec/models/user_enum_scope_spec.rb +16 -8
- metadata +12 -8
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
OTNhYjFjOGFiZjQ1NTg3N2QzNjE5YmFkZWNjNmVkM2EwZWVjZDk2OA==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3bb8000f3dc7b7ea0f1cf0c782cb0e7b1494070d
|
4
|
+
data.tar.gz: 840eb22350179ec480a12db0964c937067963632
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MGI4YTJhMzU2MDQ5MTBhYWVmMTM4N2UzZmM0NDk4NjUzM2M3MGUxYTFmM2Ey
|
11
|
-
ZWFhN2Y1YjFlN2EwNjQ2YTNlZTM4ZGQzY2Q1OTM3ZGM3YjcyODE=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MDk4YzYxMzBkZDZmZjQ2ZTU1MGM3NGE0ZjA4YmNkNzNjMjM0ZTcwOGI2Nzc1
|
14
|
-
Y2I3ODhmZDQxMjczOGMwYmM4OWE4ODVjMjgxNDIyNTAxMDgyZGM3NGIyZjA0
|
15
|
-
Y2M4YjNjNTNlYTdkYWE1MDUzYTY5NjlmYWQ3Y2U5ZjFjMWMyOTc=
|
6
|
+
metadata.gz: 260ffdeda4eb00489b5701444c3a21e88b1fe962f69b5cf0d3c220797ec2c6f5221835c1cc1cf6625119addd3c98ccd0ffab82a53d52d9f779886b2dba35f743
|
7
|
+
data.tar.gz: 6b19aabcad242e31ed1960e140d34b16d6d312c4ac64628abb7b3df7d8e59d59b6a3d5354e69b14f2ba193246784734e084045428265219733c7f6fc4e7b6be7
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
### v0.1.1
|
2
|
+
* Added support for filtering results from `Enum.label_value_pairs` by
|
3
|
+
passinging in enum value keys as symbol arguments.
|
4
|
+
|
1
5
|
### v0.1.0
|
2
6
|
* Added Rails scope helpers for searching on enum values. For a given
|
3
7
|
model `User` with an enum attribute of `role` with keys `admin`,
|
data/README.md
CHANGED
@@ -1,46 +1,63 @@
|
|
1
1
|
# RailsAttrEnum
|
2
|
-
## Enums for Rails models
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
ensure only the values are accepted.
|
3
|
+
Enums for Rails attributes. Specify enumeration identifiers for int attributes
|
4
|
+
on your Rails models. Includes constants for your identifiers, validation,
|
5
|
+
model class scopes, and instance helper methods.
|
8
6
|
|
9
|
-
|
7
|
+
## Build Status
|
8
|
+
[![Build Status](https://travis-ci.org/jfairbank/rails_attr_enum.png?branch=master)](https://travis-ci.org/jfairbank/rails_attr_enum)
|
10
9
|
|
11
|
-
|
10
|
+
## Installation
|
11
|
+
Add to your Gemfile:
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
```ruby
|
14
|
+
gem 'rails_attr_enum'
|
15
|
+
```
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
And then run bundler:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Here's an example given a class `User` with an attribute `role`:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# Example model User for a blog app
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
extend RailsAttrEnum
|
29
|
+
attr_enum :role, :admin, :editor, :author, :user
|
30
|
+
end
|
19
31
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
32
|
+
# Creates module `User::Role` with constants for each possible value
|
33
|
+
User::Role::ADMIN == 0
|
34
|
+
User::Role::EDITOR == 1
|
35
|
+
User::Role::AUTHOR == 2
|
36
|
+
User::Role::USER == 3
|
37
|
+
```
|
25
38
|
|
26
39
|
[View other ways to define and customize enums](https://github.com/jfairbank/rails_attr_enum/wiki/Adding-an-Enum-to-a-Model)
|
27
40
|
|
28
|
-
|
41
|
+
## Helpers for Model Instances
|
29
42
|
|
30
43
|
A couple helpers methods are added to the model and the enum attribute.
|
31
44
|
|
32
45
|
Get the "display" label for the current value with the `display_*` method:
|
33
46
|
|
34
|
-
|
35
|
-
|
47
|
+
```ruby
|
48
|
+
user = User.new(role: User::Role::ADMIN)
|
49
|
+
user.display_role == 'Admin'
|
50
|
+
```
|
36
51
|
|
37
52
|
You can check for a specific value with a query `*?` method:
|
38
53
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
54
|
+
```ruby
|
55
|
+
user = User.new(role: User::Role::AUTHOR)
|
56
|
+
user.role.admin? # false
|
57
|
+
user.role.editor? # false
|
58
|
+
user.role.author? # true
|
59
|
+
user.role.user? # false
|
60
|
+
```
|
44
61
|
|
45
62
|
The query method works via a forwarding class, so the normal `role` and `role=`
|
46
63
|
methods should work as expected.
|
@@ -48,95 +65,136 @@ methods should work as expected.
|
|
48
65
|
**NOTE**: one caveat to this is if you try to use
|
49
66
|
a hash map of the enum values to some other value. See below:
|
50
67
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
68
|
+
```ruby
|
69
|
+
alt_label_map = {
|
70
|
+
User::Role::ADMIN => 'The admin user',
|
71
|
+
User::Role::EDITOR => 'An editor',
|
72
|
+
User::Role::AUTHOR => 'An author',
|
73
|
+
User::Role::USER => 'A user'
|
74
|
+
}
|
57
75
|
|
58
|
-
|
59
|
-
|
60
|
-
|
76
|
+
user = User.new(role: User::Role::EDITOR)
|
77
|
+
alt_label = alt_label_map[user.role]
|
78
|
+
alt_label == nil # not 'An editor'
|
79
|
+
```
|
61
80
|
|
62
|
-
If you want the hash to work as expected
|
81
|
+
If you want the hash to work as expected then call the `.value` method on the
|
63
82
|
attribute:
|
64
83
|
|
65
|
-
|
66
|
-
|
84
|
+
```ruby
|
85
|
+
alt_label = alt_label_map[user.role.value]
|
86
|
+
alt_label == 'An editor'
|
87
|
+
```
|
67
88
|
|
68
89
|
Thus, the `.value` method on the attribute gives the actual `Fixnum` value.
|
69
90
|
There is also a `.key` method which gives the symbol key:
|
70
91
|
|
71
|
-
|
72
|
-
|
92
|
+
```ruby
|
93
|
+
user = User.new(role: User::Role::ADMIN)
|
94
|
+
user.role.key == :admin
|
95
|
+
```
|
73
96
|
|
74
97
|
The attribute value can also be set with a bang `*!` method
|
75
98
|
|
76
|
-
|
77
|
-
|
78
|
-
|
99
|
+
```ruby
|
100
|
+
user = User.new
|
101
|
+
user.role.user!
|
102
|
+
user.display_role == 'User'
|
79
103
|
|
80
|
-
|
81
|
-
|
104
|
+
user.role.author!
|
105
|
+
user.display_role == 'Author'
|
106
|
+
```
|
82
107
|
|
83
|
-
|
108
|
+
## Scopes for Models
|
84
109
|
|
85
110
|
Convenient scopes are created for each possible enum value on the model class:
|
86
111
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
112
|
+
```ruby
|
113
|
+
User.scope_admin == User.where(role: User::Role::ADMIN)
|
114
|
+
User.scope_editor == User.where(role: User::Role::EDITOR)
|
115
|
+
User.scope_author == User.where(role: User::Role::AUTHOR)
|
116
|
+
User.scope_user == User.where(role: User::Role::USER)
|
117
|
+
```
|
91
118
|
|
92
|
-
|
119
|
+
## Enum Helper Methods
|
93
120
|
|
94
121
|
Helper methods are added to the actual `Enum` module as well.
|
95
122
|
|
96
|
-
|
123
|
+
### get_label and get_key
|
124
|
+
Get the (surprise!) label and key for a given enum
|
97
125
|
value:
|
98
126
|
|
99
|
-
|
100
|
-
|
127
|
+
```ruby
|
128
|
+
User::Role.get_label(User::Role::ADMIN) == 'Admin'
|
129
|
+
User::Role.get_key(User::Role::USER) == :user
|
130
|
+
```
|
101
131
|
|
102
|
-
|
132
|
+
### attr_name
|
133
|
+
Return the attribute name as a symbol
|
103
134
|
|
104
|
-
|
135
|
+
```ruby
|
136
|
+
User::Role.attr_name == :role
|
137
|
+
```
|
105
138
|
|
106
|
-
|
139
|
+
### keys
|
140
|
+
Return all the enum keys
|
107
141
|
|
108
|
-
|
142
|
+
```ruby
|
143
|
+
User::Role.keys == [:admin, :editor, :author, :user]
|
144
|
+
```
|
109
145
|
|
110
|
-
|
146
|
+
### values
|
147
|
+
Return all the enum values
|
111
148
|
|
112
|
-
|
149
|
+
```ruby
|
150
|
+
User::Role.values == [0, 1, 2, 3]
|
151
|
+
```
|
113
152
|
|
114
|
-
|
153
|
+
### labels
|
154
|
+
Return all the enum labels
|
115
155
|
|
116
|
-
|
156
|
+
```ruby
|
157
|
+
User::Role.labels == ['Admin', 'Editor', 'Author', 'User']
|
158
|
+
```
|
117
159
|
|
118
|
-
|
119
|
-
|
120
|
-
---
|
121
|
-
|
122
|
-
`labels` returns all the enum labels
|
123
|
-
|
124
|
-
User::Role.labels == ['Admin', 'Editor', 'Author', 'User']
|
125
|
-
|
126
|
-
---
|
127
|
-
|
128
|
-
`label_value_pairs` returns an array of pairs of the label and value for each
|
160
|
+
### label_value_pairs
|
161
|
+
Return an array of pairs of the label and value for each
|
129
162
|
enum value. This is mainly a convenience method for something like the
|
130
163
|
collection option for a select input in the
|
131
164
|
[Formtastic](https://github.com/justinfrench/formtastic) or
|
132
|
-
[ActiveAdmin
|
133
|
-
gems:
|
165
|
+
[ActiveAdmin](https://github.com/gregbell/active_admin) (which uses Formtastic)
|
166
|
+
gems, so you can easily generate select options:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
User::Role.label_value_pairs == [
|
170
|
+
['Admin', 0], ['Editor', 1], ['Author', 2], ['User', 3]
|
171
|
+
]
|
172
|
+
|
173
|
+
# In an ActiveAdmin form
|
174
|
+
ActiveAdmin.register User do
|
175
|
+
form do |f|
|
176
|
+
f.inputs 'User Details' do
|
177
|
+
f.input :first_name
|
178
|
+
f.input :last_name
|
179
|
+
f.input :email
|
180
|
+
|
181
|
+
# Example usage of `label_value_pairs`
|
182
|
+
f.input :role, as: :select, collection: User::Role.label_value_pairs
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
```
|
134
187
|
|
135
|
-
|
136
|
-
|
188
|
+
You can also filter the results by passing in enum value keys as symbol
|
189
|
+
arguments:
|
137
190
|
|
138
|
-
|
191
|
+
```ruby
|
192
|
+
User::Role.label_value_pairs(:admin, :author) == [
|
193
|
+
['Admin', 0], ['Author', 2]
|
194
|
+
]
|
195
|
+
```
|
139
196
|
|
197
|
+
### to_h and to_json
|
140
198
|
`to_h` and `to_json` return a hash and a json string representation of the enum,
|
141
199
|
respectively. They both offer an `only` and an `except` option to specify if
|
142
200
|
you only want the value or maybe only the label and key or if you want
|
@@ -144,27 +202,29 @@ everything but key. **NOTE**: passing only key to `only` or excluding all but
|
|
144
202
|
one key via `except` will give that single value (whether it's value, key, or
|
145
203
|
label) instead of a hash. See below to understand:
|
146
204
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
205
|
+
```ruby
|
206
|
+
# Default call with no options
|
207
|
+
User::Role.to_h == {
|
208
|
+
'ADMIN' => { key: :admin, label: 'Admin', value: 0 },
|
209
|
+
'EDITOR' => { key: :editor, label: 'Editor', value: 1 },
|
210
|
+
'AUTHOR' => { key: :author, label: 'Author', value: 2 },
|
211
|
+
'USER' => { key: :user, label: 'User', value: 3 }
|
212
|
+
}
|
213
|
+
|
214
|
+
# Call with a single symbol (would also work with `only: [:value]`)
|
215
|
+
# Notice the mapped values are not hashes like above because we only
|
216
|
+
# specified that we wanted the value
|
217
|
+
User::Role.to_h(only: :value) == {
|
218
|
+
'ADMIN' => 0,
|
219
|
+
'EDITOR' => 1,
|
220
|
+
'AUTHOR' => 2,
|
221
|
+
'USER' => 3
|
222
|
+
}
|
223
|
+
|
224
|
+
# Would also work with `except: [:value]`
|
225
|
+
User::Role.to_json(except: :value) ==
|
226
|
+
"{\"ADMIN\":{\"key\":\"admin\",\"label\":\"Admin\"},\"EDITOR\":{\"key\":\"editor\",\"label\":\"Editor\"},\"AUTHOR\":{\"key\":\"author\",\"label\":\"Author\"},\"USER\":{\"key\":\"user\",\"label\":\"User\"}}"
|
227
|
+
```
|
228
|
+
|
229
|
+
## Feedback and Pull Requests Welcome
|
170
230
|
This is my first real Rails gem, so I welcome all feedback and ideas. I hope this gem is as helpful to you as it has been to me in my own projects.
|
data/lib/rails_attr_enum/enum.rb
CHANGED
@@ -23,8 +23,15 @@ module RailsAttrEnum
|
|
23
23
|
@entries << entry
|
24
24
|
end
|
25
25
|
|
26
|
-
def label_value_pairs
|
27
|
-
|
26
|
+
def label_value_pairs(*keys)
|
27
|
+
if keys.empty?
|
28
|
+
labels.zip(values)
|
29
|
+
else
|
30
|
+
@entries.reduce([]) do |arr, entry|
|
31
|
+
arr << [entry.label, entry.value] if keys.include?(entry.key)
|
32
|
+
arr
|
33
|
+
end
|
34
|
+
end
|
28
35
|
end
|
29
36
|
|
30
37
|
def get_label(value)
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -112,3 +112,21 @@ DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead. (
|
|
112
112
|
[1m[35m (0.1ms)[0m rollback transaction
|
113
113
|
[1m[36m (0.3ms)[0m [1mbegin transaction[0m
|
114
114
|
[1m[35m (0.1ms)[0m rollback transaction
|
115
|
+
[1m[36m (1.7ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) [0m
|
116
|
+
[1m[35m (1.5ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
117
|
+
[1m[36m (1.7ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
118
|
+
[1m[35m (0.2ms)[0m SELECT version FROM "schema_migrations"
|
119
|
+
[1m[36m (1.4ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131023212242')[0m
|
120
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.3ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
121
|
+
[1m[36m (1.3ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) [0m
|
122
|
+
[1m[35m (1.1ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
123
|
+
[1m[36m (1.1ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
124
|
+
[1m[35m (0.1ms)[0m SELECT version FROM "schema_migrations"
|
125
|
+
[1m[36m (0.9ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131023212242')[0m
|
126
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.3ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
127
|
+
[1m[36m (18.1ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) [0m
|
128
|
+
[1m[35m (1.2ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
129
|
+
[1m[36m (1.1ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
130
|
+
[1m[35m (0.2ms)[0m SELECT version FROM "schema_migrations"
|
131
|
+
[1m[36m (1.3ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131023212242')[0m
|
132
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.3ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|