rails_attr_enum 0.0.6 → 0.1.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 +8 -8
- data/CHANGELOG.md +12 -0
- data/README.md +119 -117
- data/lib/rails_attr_enum/entry.rb +0 -1
- data/lib/rails_attr_enum/enum.rb +2 -0
- data/lib/rails_attr_enum/version.rb +1 -1
- data/lib/rails_attr_enum.rb +2 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +15 -0
- data/test/dummy/log/test.log +5004 -0
- data/test/dummy/rails +2 -0
- data/test/dummy/rake +2 -0
- data/test/dummy/rspec +2 -0
- data/test/dummy/spec/factories/users.rb +9 -0
- data/test/dummy/spec/models/user_enum_scope_spec.rb +25 -0
- data/test/dummy/spec/models/{user_spec.rb → user_enum_spec.rb} +20 -25
- data/test/dummy/spec/spec_helper.rb +17 -0
- metadata +13 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjQyNmMyOWJkNGFjZmViNjBlZTg5OGEzYTAyZjc2N2MwOTUyYjk5Ng==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OTNhYjFjOGFiZjQ1NTg3N2QzNjE5YmFkZWNjNmVkM2EwZWVjZDk2OA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZmNmNDYxMjFlY2VkNWQ3MjZmNTQwNTdkYzEwYTgyNDM0Yjg2MmU2ZmRjODgy
|
10
|
+
MGI4YTJhMzU2MDQ5MTBhYWVmMTM4N2UzZmM0NDk4NjUzM2M3MGUxYTFmM2Ey
|
11
|
+
ZWFhN2Y1YjFlN2EwNjQ2YTNlZTM4ZGQzY2Q1OTM3ZGM3YjcyODE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MDk4YzYxMzBkZDZmZjQ2ZTU1MGM3NGE0ZjA4YmNkNzNjMjM0ZTcwOGI2Nzc1
|
14
|
+
Y2I3ODhmZDQxMjczOGMwYmM4OWE4ODVjMjgxNDIyNTAxMDgyZGM3NGIyZjA0
|
15
|
+
Y2M4YjNjNTNlYTdkYWE1MDUzYTY5NjlmYWQ3Y2U5ZjFjMWMyOTc=
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
### v0.1.0
|
2
|
+
* Added Rails scope helpers for searching on enum values. For a given
|
3
|
+
model `User` with an enum attribute of `role` with keys `admin`,
|
4
|
+
`editor`, `author`, and `user`, the following scopes will be added:
|
5
|
+
* `User.role_admin`
|
6
|
+
* `User.role_editor`
|
7
|
+
* `User.role_author`
|
8
|
+
* `User.role_user`
|
9
|
+
* Added `to_h` and `to_json` methods to an `Enum` like `User::Role` to
|
10
|
+
give the hash and json string representations of the enum,
|
11
|
+
respectively.
|
12
|
+
|
1
13
|
### v0.0.6
|
2
14
|
* Added example usage of extending `RailsAttrEnum` and calling
|
3
15
|
`attr_enum` in `User` model in test Rails app.
|
data/README.md
CHANGED
@@ -14,155 +14,157 @@ Here's an example given a class `User` with an attribute `role`:
|
|
14
14
|
class User < ActiveRecord::Base
|
15
15
|
extend RailsAttrEnum
|
16
16
|
|
17
|
-
attr_enum :role, :admin, :
|
17
|
+
attr_enum :role, :admin, :editor, :author, :user
|
18
18
|
end
|
19
19
|
|
20
20
|
# Creates module `User::Role` with constants for each possible value
|
21
21
|
User::Role::ADMIN == 0
|
22
|
-
User::Role::
|
23
|
-
User::Role::
|
22
|
+
User::Role::EDITOR == 1
|
23
|
+
User::Role::AUTHOR == 2
|
24
24
|
User::Role::USER == 3
|
25
25
|
|
26
|
-
|
27
|
-
`EDITOR`, and `USER` with the respective values of `0`, `1`, `2`, and `3`.
|
26
|
+
[View other ways to define and customize enums](https://github.com/jfairbank/rails_attr_enum/wiki/Adding-an-Enum-to-a-Model)
|
28
27
|
|
29
|
-
|
30
|
-
you don't specify will automatically be filled with the first available integer
|
31
|
-
value.
|
28
|
+
### Helpers for Model Instances
|
32
29
|
|
33
|
-
|
34
|
-
class User < ActiveRecord::Base
|
35
|
-
extend RailsAttrEnum
|
30
|
+
A couple helpers methods are added to the model and the enum attribute.
|
36
31
|
|
37
|
-
|
38
|
-
end
|
32
|
+
Get the "display" label for the current value with the `display_*` method:
|
39
33
|
|
40
|
-
User::Role::ADMIN
|
41
|
-
|
42
|
-
User::Role::EDITOR == 1 # Notice this still defaulted to 1
|
43
|
-
User::Role::USER == 42
|
34
|
+
user = User.new(User::Role::ADMIN)
|
35
|
+
user.display_role == 'Admin'
|
44
36
|
|
45
|
-
|
46
|
-
class User < ActiveRecord::Base
|
47
|
-
extend RailsAttrEnum
|
37
|
+
You can check for a specific value with a query `*?` method:
|
48
38
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
}
|
55
|
-
end
|
39
|
+
user = User.new(User::Role::AUTHOR)
|
40
|
+
user.role.admin? # false
|
41
|
+
user.role.editor? # false
|
42
|
+
user.role.author? # true
|
43
|
+
user.role.user? # false
|
56
44
|
|
57
|
-
|
58
|
-
|
59
|
-
User::Role::EDITOR == 4
|
60
|
-
User::Role::USER == 8
|
45
|
+
The query method works via a forwarding class, so the normal `role` and `role=`
|
46
|
+
methods should work as expected.
|
61
47
|
|
62
|
-
|
63
|
-
|
64
|
-
extend RailsAttrEnum
|
48
|
+
**NOTE**: one caveat to this is if you try to use
|
49
|
+
a hash map of the enum values to some other value. See below:
|
65
50
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
end
|
51
|
+
alt_label_map = {
|
52
|
+
User::Role::ADMIN => 'The admin user',
|
53
|
+
User::Role::EDITOR => 'An editor',
|
54
|
+
User::Role::AUTHOR => 'An author',
|
55
|
+
User::Role::USER => 'A user'
|
56
|
+
}
|
73
57
|
|
74
|
-
User::Role::
|
75
|
-
|
76
|
-
|
77
|
-
User::Role::USER == 7
|
58
|
+
user = User.new(User::Role::EDITOR)
|
59
|
+
alt_label = alt_label_map[user.role]
|
60
|
+
alt_label == nil # not 'An editor'
|
78
61
|
|
79
|
-
|
80
|
-
|
81
|
-
app to display something meaningful for a value. Appropriate label constants are
|
82
|
-
added to the module enum as well as a helper `display_*` method on instances of
|
83
|
-
your model.
|
62
|
+
If you want the hash to work as expected than call the `.value` method on the
|
63
|
+
attribute:
|
84
64
|
|
85
|
-
|
86
|
-
|
65
|
+
alt_label = alt_label_map[user.role.value]
|
66
|
+
alt_label == 'An editor'
|
87
67
|
|
88
|
-
|
89
|
-
|
68
|
+
Thus, the `.value` method on the attribute gives the actual `Fixnum` value.
|
69
|
+
There is also a `.key` method which gives the symbol key:
|
90
70
|
|
91
|
-
User::Role::
|
92
|
-
|
93
|
-
User::Role::EDITOR_LABEL == 'Editor'
|
94
|
-
User::Role::USER_LABEL == 'User'
|
71
|
+
user = User.new(User::Role::ADMIN)
|
72
|
+
user.role.key == :admin
|
95
73
|
|
96
|
-
|
97
|
-
user.display_role == 'Admin' # Helper method added by RailsAttrEnum
|
74
|
+
The attribute value can also be set with a bang `*!` method
|
98
75
|
|
99
|
-
|
100
|
-
|
76
|
+
user = User.new
|
77
|
+
user.role.user!
|
78
|
+
user.display_role == 'User'
|
101
79
|
|
102
|
-
|
103
|
-
|
80
|
+
user.role.author!
|
81
|
+
user.display_role == 'Author'
|
104
82
|
|
105
|
-
|
106
|
-
{ admin: 'Admin Role' }, :author, { editor: 'Editor Role' }, :user
|
107
|
-
end
|
83
|
+
### Scopes for Models
|
108
84
|
|
109
|
-
|
110
|
-
User::Role::AUTHOR_LABEL == 'Author'
|
111
|
-
User::Role::EDITOR_LABEL == 'Editor Role'
|
112
|
-
User::Role::USER_LABEL == 'User'
|
85
|
+
Convenient scopes are created for each possible enum value on the model class:
|
113
86
|
|
114
|
-
|
115
|
-
|
116
|
-
|
87
|
+
User.scope_admin == User.where(role: User::Role::ADMIN)
|
88
|
+
User.scope_editor == User.where(role: User::Role::EDITOR)
|
89
|
+
User.scope_author == User.where(role: User::Role::AUTHOR)
|
90
|
+
User.scope_user == User.where(role: User::Role::USER)
|
117
91
|
|
118
|
-
|
119
|
-
admin: 'Admin Role',
|
120
|
-
author: 'Author Role',
|
121
|
-
editor: 'Editor Role',
|
122
|
-
user: 'User Role'
|
123
|
-
}
|
124
|
-
end
|
92
|
+
### Enum Helper Methods
|
125
93
|
|
126
|
-
|
127
|
-
User::Role::AUTHOR_LABEL == 'Author Role'
|
128
|
-
User::Role::EDITOR_LABEL == 'Editor Role'
|
129
|
-
User::Role::USER_LABEL == 'User Role'
|
94
|
+
Helper methods are added to the actual `Enum` module as well.
|
130
95
|
|
131
|
-
|
132
|
-
|
133
|
-
extend RailsAttrEnum
|
96
|
+
`get_label` and `get_key` get the (surprise!) label and key for a given enum
|
97
|
+
value:
|
134
98
|
|
135
|
-
|
136
|
-
|
137
|
-
add author: 'Author Role'
|
138
|
-
add editor: 'Editor Role'
|
139
|
-
add :user
|
140
|
-
end
|
141
|
-
end
|
99
|
+
User::Role.get_label(User::Role::ADMIN) == 'Admin'
|
100
|
+
User::Role.get_key(User::Role::USER) == :user
|
142
101
|
|
143
|
-
|
144
|
-
User::Role::AUTHOR_LABEL == 'Author Role'
|
145
|
-
User::Role::EDITOR_LABEL == 'Editor Role'
|
146
|
-
User::Role::USER_LABEL == 'User'
|
102
|
+
---
|
147
103
|
|
148
|
-
|
149
|
-
If you need to be very specific about values and labels, then you can specify
|
150
|
-
both at the same time too.
|
104
|
+
`attr_name` returns the attribute symbol
|
151
105
|
|
152
|
-
|
153
|
-
extend RailsAttrEnum
|
106
|
+
User::Role.attr_name == :role
|
154
107
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
108
|
+
---
|
109
|
+
|
110
|
+
`keys` returns all the enum keys
|
111
|
+
|
112
|
+
User::Role.keys == [:admin, :editor, :author, :user]
|
113
|
+
|
114
|
+
---
|
115
|
+
|
116
|
+
`values` returns all the enum values
|
117
|
+
|
118
|
+
User::Role.values == [0, 1, 2, 3]
|
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
|
129
|
+
enum value. This is mainly a convenience method for something like the
|
130
|
+
collection option for a select input in the
|
131
|
+
[Formtastic](https://github.com/justinfrench/formtastic) or
|
132
|
+
[ActiveAdmin (which uses Formtastic)](https://github.com/gregbell/active_admin)
|
133
|
+
gems:
|
134
|
+
|
135
|
+
User::Role.label_value_pairs ==
|
136
|
+
[['Admin', 0], ['Editor', 1], ['Author', 2], ['User', 3]]
|
137
|
+
|
138
|
+
---
|
139
|
+
|
140
|
+
`to_h` and `to_json` return a hash and a json string representation of the enum,
|
141
|
+
respectively. They both offer an `only` and an `except` option to specify if
|
142
|
+
you only want the value or maybe only the label and key or if you want
|
143
|
+
everything but key. **NOTE**: passing only key to `only` or excluding all but
|
144
|
+
one key via `except` will give that single value (whether it's value, key, or
|
145
|
+
label) instead of a hash. See below to understand:
|
146
|
+
|
147
|
+
# Default call with no options
|
148
|
+
User::Role.to_h == {
|
149
|
+
'ADMIN' => { key: :admin, label: 'Admin', value: 0 },
|
150
|
+
'EDITOR' => { key: :editor, label: 'Editor', value: 1 },
|
151
|
+
'AUTHOR' => { key: :author, label: 'Author', value: 2 },
|
152
|
+
'USER' => { key: :user, label: 'User', value: 3 }
|
153
|
+
}
|
154
|
+
|
155
|
+
# Call with a single symbol (would also work with `only: [:value]`)
|
156
|
+
# Notice the mapped values are not hashes like above because we only
|
157
|
+
# specified that we wanted the value
|
158
|
+
User::Role.to_h(only: :value) == {
|
159
|
+
'ADMIN' => 0,
|
160
|
+
'EDITOR' => 1,
|
161
|
+
'AUTHOR' => 2,
|
162
|
+
'USER' => 3
|
163
|
+
}
|
164
|
+
|
165
|
+
# Would also work with `except: [:value]`
|
166
|
+
User::Role.to_json(except: :value) ==
|
167
|
+
"{\"ADMIN\":{\"key\":\"admin\",\"label\":\"Admin\"},\"EDITOR\":{\"key\":\"editor\",\"label\":\"Editor\"},\"AUTHOR\":{\"key\":\"author\",\"label\":\"Author\"},\"USER\":{\"key\":\"user\",\"label\":\"User\"}}"
|
160
168
|
|
161
|
-
|
162
|
-
|
163
|
-
User::Role::AUTHOR == 0
|
164
|
-
User::Role::AUTHOR_LABEL == 'Author Role'
|
165
|
-
User::Role::EDITOR == 42
|
166
|
-
User::Role::EDITOR_LABEL == 'Editor'
|
167
|
-
User::Role::USER == 2
|
168
|
-
User::Role::USER_LABEL == 'User'
|
169
|
+
### Feedback and Pull Requests Welcome
|
170
|
+
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
data/lib/rails_attr_enum.rb
CHANGED
@@ -43,7 +43,6 @@ module RailsAttrEnum
|
|
43
43
|
@_attr_enums[attr.name.to_sym] = Module.new { include Enum }.tap do |mod|
|
44
44
|
mod.init_enum(attr.name)
|
45
45
|
const_set(attr.enum_name, mod)
|
46
|
-
validates(attr.name.to_sym, validation_rules.merge(inclusion: { in: mod.values }))
|
47
46
|
|
48
47
|
entries.each do |entry|
|
49
48
|
mod.add(entry)
|
@@ -51,6 +50,8 @@ module RailsAttrEnum
|
|
51
50
|
scope :#{attr.name}_#{entry.key}, -> { where(:#{attr.name} => #{entry.value}) }
|
52
51
|
EOS
|
53
52
|
end
|
53
|
+
|
54
|
+
validates(attr.name.to_sym, validation_rules.merge(inclusion: { in: mod.values }))
|
54
55
|
end
|
55
56
|
|
56
57
|
add_methods(attr)
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -97,3 +97,18 @@ DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead. (
|
|
97
97
|
[1m[35m (0.1ms)[0m SELECT version FROM "schema_migrations"
|
98
98
|
[1m[36m (1.3ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131023212242')[0m
|
99
99
|
[1m[35mActiveRecord::SchemaMigration Load (0.2ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
100
|
+
[1m[36m (3.5ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) [0m
|
101
|
+
[1m[35m (1.6ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
102
|
+
[1m[36m (1.6ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
103
|
+
[1m[35m (0.2ms)[0m SELECT version FROM "schema_migrations"
|
104
|
+
[1m[36m (1.4ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131023212242')[0m
|
105
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
106
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
107
|
+
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."role" = 0[0m
|
108
|
+
[1m[35mUser Load (0.4ms)[0m SELECT "users".* FROM "users" WHERE "users"."role" = 1
|
109
|
+
[1m[36m (0.3ms)[0m [1mSELECT COUNT(*) FROM "users" WHERE "users"."role" = 1[0m
|
110
|
+
[1m[35m (0.4ms)[0m SELECT COUNT(*) FROM "users" WHERE "users"."role" = 0
|
111
|
+
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
112
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
113
|
+
[1m[36m (0.3ms)[0m [1mbegin transaction[0m
|
114
|
+
[1m[35m (0.1ms)[0m rollback transaction
|