rails_attr_enum 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +168 -0
- data/lib/rails_attr_enum.rb +2 -2
- data/lib/rails_attr_enum/version.rb +1 -1
- data/test/dummy/app/models/user.rb +0 -23
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +268 -0
- data/test/dummy/spec/models/user_spec.rb +112 -0
- data/test/dummy/spec/spec_helper.rb +42 -0
- metadata +14 -13
- data/README.rdoc +0 -3
- data/test/dummy/test/fixtures/users.yml +0 -9
- data/test/dummy/test/models/user_test.rb +0 -7
- data/test/rails_attr_enum_test.rb +0 -7
- data/test/test_helper.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9801e3908003e982b471d4af5ddbd4e589eb128
|
4
|
+
data.tar.gz: e0ad1758a752118f79a55e87a6d2b8a44f03cc8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de99edcb330ea895b1b5fa548d0291cb4b71ef742a6aa84066fb02f150b3a9286c8eb8ce8cde65e53cd2a66a6e55944ea44b2d5524c0c31211ec220fed70592b
|
7
|
+
data.tar.gz: 8ddb3ae5b3cbf6bf8cc0561b405aff12000c035bd24f713ae4cf13b0fba5f5db2498d498994ea085d747e87a24828c6ef9be06e14993953858b2e7abee029aaf
|
data/README.md
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
# RailsAttrEnum
|
2
|
+
## Enums for Rails models
|
3
|
+
|
4
|
+
I created RailsAttrEnum as a way to create an enum-like structure similar to
|
5
|
+
enums in C languages. You can specify the accepted identifiers for the possible
|
6
|
+
integer values for the model's attribute as well have built-in validation to
|
7
|
+
ensure only the values are accepted.
|
8
|
+
|
9
|
+
### Usage
|
10
|
+
|
11
|
+
Here's an example given a class `User` with an attribute `role`:
|
12
|
+
|
13
|
+
# Example model User for a blog app
|
14
|
+
class User < ActiveRecord::Base
|
15
|
+
extend RailsAttrEnum
|
16
|
+
|
17
|
+
attr_enum :role, :admin, :author, :editor, :user
|
18
|
+
end
|
19
|
+
|
20
|
+
# Creates module `User::Role` with constants for each possible value
|
21
|
+
User::Role::ADMIN == 0
|
22
|
+
User::Role::AUTHOR == 1
|
23
|
+
User::Role::EDITOR == 2
|
24
|
+
User::Role::USER == 3
|
25
|
+
|
26
|
+
As you can see, this would give a module `User::Role` containing constants `ADMIN`, `AUTHOR`,
|
27
|
+
`EDITOR`, and `USER` with the respective values of `0`, `1`, `2`, and `3`.
|
28
|
+
|
29
|
+
You can also specify the integer values for each identifier or only some. Those
|
30
|
+
you don't specify will automatically be filled with the first available integer
|
31
|
+
value.
|
32
|
+
|
33
|
+
# Target specific identifiers
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
extend RailsAttrEnum
|
36
|
+
|
37
|
+
attr_enum :role, :admin, { author: 12 }, :editor, { user: 42 }
|
38
|
+
end
|
39
|
+
|
40
|
+
User::Role::ADMIN == 0
|
41
|
+
User::Role::AUTHOR == 12
|
42
|
+
User::Role::EDITOR == 1 # Notice this still defaulted to 1
|
43
|
+
User::Role::USER == 42
|
44
|
+
|
45
|
+
# Use a hash to specify all values
|
46
|
+
class User < ActiveRecord::Base
|
47
|
+
extend RailsAttrEnum
|
48
|
+
|
49
|
+
attr_enum :role, {
|
50
|
+
admin: 1,
|
51
|
+
author: 2,
|
52
|
+
editor: 4,
|
53
|
+
user: 8
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
User::Role::ADMIN == 1
|
58
|
+
User::Role::AUTHOR == 2
|
59
|
+
User::Role::EDITOR == 4
|
60
|
+
User::Role::USER == 8
|
61
|
+
|
62
|
+
# Use a block to specify some (or all)
|
63
|
+
class User < ActiveRecord::Base
|
64
|
+
extend RailsAttrEnum
|
65
|
+
|
66
|
+
attr_enum :role do
|
67
|
+
add admin: 42
|
68
|
+
add :author
|
69
|
+
add :editor
|
70
|
+
add user: 7
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
User::Role::ADMIN == 42
|
75
|
+
User::Role::AUTHOR == 0 # Again notice how `AUTHOR` and `EDITOR` defaulted
|
76
|
+
User::Role::EDITOR == 1
|
77
|
+
User::Role::USER == 7
|
78
|
+
|
79
|
+
### Labels
|
80
|
+
RailsAttrEnum also creates a label for each identifier that you can use in your
|
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.
|
84
|
+
|
85
|
+
class User < ActiveRecord::Base
|
86
|
+
extend RailsAttrEnum
|
87
|
+
|
88
|
+
attr_enum :role, :admin, :author, :editor, :user
|
89
|
+
end
|
90
|
+
|
91
|
+
User::Role::ADMIN_LABEL == 'Admin'
|
92
|
+
User::Role::AUTHOR_LABEL == 'Author'
|
93
|
+
User::Role::EDITOR_LABEL == 'Editor'
|
94
|
+
User::Role::USER_LABEL == 'User'
|
95
|
+
|
96
|
+
user = User.new(role: User::Role::ADMIN)
|
97
|
+
user.display_role == 'Admin' # Helper method added by RailsAttrEnum
|
98
|
+
|
99
|
+
You can specify your own labels if you like. By default, RailAttrEnum calls
|
100
|
+
`.to_s.titleize` on the symbol identifier.
|
101
|
+
|
102
|
+
class User < ActiveRecord::Base
|
103
|
+
extend RailsAttrEnum
|
104
|
+
|
105
|
+
attr_enum :role,
|
106
|
+
{ admin: 'Admin Role' }, :author, { editor: 'Editor Role' }, :user
|
107
|
+
end
|
108
|
+
|
109
|
+
User::Role::ADMIN_LABEL == 'Admin Role'
|
110
|
+
User::Role::AUTHOR_LABEL == 'Author'
|
111
|
+
User::Role::EDITOR_LABEL == 'Editor Role'
|
112
|
+
User::Role::USER_LABEL == 'User'
|
113
|
+
|
114
|
+
# With a hash
|
115
|
+
class User < ActiveRecord::Base
|
116
|
+
extend RailsAttrEnum
|
117
|
+
|
118
|
+
attr_enum :role, {
|
119
|
+
admin: 'Admin Role',
|
120
|
+
author: 'Author Role',
|
121
|
+
editor: 'Editor Role',
|
122
|
+
user: 'User Role'
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
User::Role::ADMIN_LABEL == 'Admin Role'
|
127
|
+
User::Role::AUTHOR_LABEL == 'Author Role'
|
128
|
+
User::Role::EDITOR_LABEL == 'Editor Role'
|
129
|
+
User::Role::USER_LABEL == 'User Role'
|
130
|
+
|
131
|
+
# With a block
|
132
|
+
class User < ActiveRecord::Base
|
133
|
+
extend RailsAttrEnum
|
134
|
+
|
135
|
+
attr_enum :role do
|
136
|
+
add :admin
|
137
|
+
add author: 'Author Role'
|
138
|
+
add editor: 'Editor Role'
|
139
|
+
add :user
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
User::Role::ADMIN_LABEL == 'Admin'
|
144
|
+
User::Role::AUTHOR_LABEL == 'Author Role'
|
145
|
+
User::Role::EDITOR_LABEL == 'Editor Role'
|
146
|
+
User::Role::USER_LABEL == 'User'
|
147
|
+
|
148
|
+
### Mix-and-match
|
149
|
+
If you need to be very specific about values and labels, then you can specify
|
150
|
+
both at the same time too.
|
151
|
+
|
152
|
+
class User < ActiveRecord::Base
|
153
|
+
extend RailsAttrEnum
|
154
|
+
|
155
|
+
attr_enum :role, { admin: { label: 'Admin Role', value: 1 } },
|
156
|
+
{ author: 'Author Role' },
|
157
|
+
{ editor: 42 },
|
158
|
+
:user
|
159
|
+
end
|
160
|
+
|
161
|
+
User::Role::ADMIN == 1
|
162
|
+
User::Role::ADMIN_LABEL == 'Admin Role'
|
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'
|
data/lib/rails_attr_enum.rb
CHANGED
@@ -33,11 +33,11 @@ module RailsAttrEnum
|
|
33
33
|
@_attr_enums ||= {}
|
34
34
|
|
35
35
|
unless column_names.include?(attr.name)
|
36
|
-
raise "Invalid attribute name #{
|
36
|
+
raise "Invalid attribute name #{attr.name}"
|
37
37
|
end
|
38
38
|
|
39
39
|
if @_attr_enums.any? { |(_, enm)| enm.attr_name == attr.name }
|
40
|
-
raise "Already defined enum for '#{
|
40
|
+
raise "Already defined enum for '#{attr.name}'"
|
41
41
|
end
|
42
42
|
|
43
43
|
@_attr_enums[attr.name.to_sym] = Module.new { include Enum }.tap do |mod|
|
@@ -1,25 +1,2 @@
|
|
1
1
|
class User < ActiveRecord::Base
|
2
|
-
extend RailsAttrEnum
|
3
|
-
|
4
|
-
# attr_enum :role, :admin, :author, :editor, :user
|
5
|
-
# attr_enum :role, :admin, { author: 2 }, :editor, { user: 10 }
|
6
|
-
|
7
|
-
attr_enum :role, { admin: { value: 10, label: 'ADMIN' } },
|
8
|
-
{ author: 'Author Role' },
|
9
|
-
:editor,
|
10
|
-
{ user: { label: 'ID10T', value: 45 } }
|
11
|
-
|
12
|
-
# attr_enum :role, {
|
13
|
-
# admin: 'ADMIN',
|
14
|
-
# author: 'AUTHOR',
|
15
|
-
# editor: 'EDITOR',
|
16
|
-
# user: 'ID10T'
|
17
|
-
# }
|
18
|
-
|
19
|
-
# attr_enum :role do
|
20
|
-
# add admin: 1
|
21
|
-
# add :author
|
22
|
-
# add editor: 42
|
23
|
-
# add user: 7
|
24
|
-
# end
|
25
2
|
end
|
Binary file
|
@@ -0,0 +1,268 @@
|
|
1
|
+
[1m[36m (2.0ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
2
|
+
[1m[35m (1.4ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
3
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
4
|
+
Migrating to CreateUsers (20131023212242)
|
5
|
+
[1m[35m (0.1ms)[0m begin transaction
|
6
|
+
[1m[36m (0.4ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) [0m
|
7
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20131023212242"]]
|
8
|
+
[1m[36m (1.0ms)[0m [1mcommit transaction[0m
|
9
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.3ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
10
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
11
|
+
[1m[35m (0.1ms)[0m begin transaction
|
12
|
+
[1m[36m (0.2ms)[0m [1mrollback transaction[0m
|
13
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
14
|
+
[1m[35m (0.2ms)[0m begin transaction
|
15
|
+
[1m[36m (0.2ms)[0m [1mrollback transaction[0m
|
16
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
17
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
18
|
+
[1m[35m (0.1ms)[0m begin transaction
|
19
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
20
|
+
[1m[35m (0.1ms)[0m begin transaction
|
21
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
22
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.2ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
23
|
+
[1m[35m (0.1ms)[0m begin transaction
|
24
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
25
|
+
[1m[35m (0.1ms)[0m begin transaction
|
26
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
27
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
28
|
+
[1m[35m (0.1ms)[0m begin transaction
|
29
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
30
|
+
[1m[35m (0.1ms)[0m begin transaction
|
31
|
+
[1m[36m (0.2ms)[0m [1mrollback transaction[0m
|
32
|
+
[1m[35m (0.2ms)[0m begin transaction
|
33
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
34
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
35
|
+
[1m[35m (0.1ms)[0m begin transaction
|
36
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
37
|
+
[1m[35m (0.1ms)[0m begin transaction
|
38
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
39
|
+
[1m[35m (0.1ms)[0m begin transaction
|
40
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
41
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
42
|
+
[1m[35m (0.1ms)[0m begin transaction
|
43
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
44
|
+
[1m[35m (0.1ms)[0m begin transaction
|
45
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
46
|
+
[1m[35m (0.1ms)[0m begin transaction
|
47
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
48
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
49
|
+
[1m[35m (0.1ms)[0m begin transaction
|
50
|
+
[1m[36m (0.0ms)[0m [1mrollback transaction[0m
|
51
|
+
[1m[35m (0.1ms)[0m begin transaction
|
52
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
53
|
+
[1m[35m (0.1ms)[0m begin transaction
|
54
|
+
[1m[36m (0.0ms)[0m [1mrollback transaction[0m
|
55
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
56
|
+
[1m[35m (0.1ms)[0m begin transaction
|
57
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
58
|
+
[1m[35m (0.1ms)[0m begin transaction
|
59
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
60
|
+
[1m[35m (0.1ms)[0m begin transaction
|
61
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
62
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
63
|
+
[1m[35m (0.1ms)[0m begin transaction
|
64
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
65
|
+
[1m[35m (0.1ms)[0m begin transaction
|
66
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
67
|
+
[1m[35m (0.1ms)[0m begin transaction
|
68
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
69
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
70
|
+
[1m[35m (0.1ms)[0m begin transaction
|
71
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
72
|
+
[1m[35m (0.1ms)[0m begin transaction
|
73
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
74
|
+
[1m[35m (0.1ms)[0m begin transaction
|
75
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
76
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
77
|
+
[1m[35m (0.1ms)[0m begin transaction
|
78
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
79
|
+
[1m[35m (0.1ms)[0m begin transaction
|
80
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
81
|
+
[1m[35m (0.1ms)[0m begin transaction
|
82
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
83
|
+
[1m[35m (0.1ms)[0m begin transaction
|
84
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
85
|
+
[1m[35m (0.1ms)[0m begin transaction
|
86
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
87
|
+
[1m[35m (0.1ms)[0m begin transaction
|
88
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
89
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
90
|
+
[1m[35m (0.1ms)[0m begin transaction
|
91
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
92
|
+
[1m[35m (0.1ms)[0m begin transaction
|
93
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
94
|
+
[1m[35m (0.1ms)[0m begin transaction
|
95
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
96
|
+
[1m[35m (0.1ms)[0m begin transaction
|
97
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
98
|
+
[1m[35m (0.1ms)[0m begin transaction
|
99
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
100
|
+
[1m[35m (0.1ms)[0m begin transaction
|
101
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
102
|
+
[1m[35m (0.0ms)[0m begin transaction
|
103
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
104
|
+
[1m[35m (0.1ms)[0m begin transaction
|
105
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
106
|
+
[1m[35m (0.1ms)[0m begin transaction
|
107
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
108
|
+
[1m[35m (0.1ms)[0m begin transaction
|
109
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
110
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
111
|
+
[1m[35m (0.1ms)[0m begin transaction
|
112
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
113
|
+
[1m[35m (0.1ms)[0m begin transaction
|
114
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
115
|
+
[1m[35m (0.1ms)[0m begin transaction
|
116
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
117
|
+
[1m[35m (0.1ms)[0m begin transaction
|
118
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
119
|
+
[1m[35m (0.1ms)[0m begin transaction
|
120
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
121
|
+
[1m[35m (0.1ms)[0m begin transaction
|
122
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
123
|
+
[1m[35m (0.1ms)[0m begin transaction
|
124
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
125
|
+
[1m[35m (0.1ms)[0m begin transaction
|
126
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
127
|
+
[1m[35m (0.1ms)[0m begin transaction
|
128
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
129
|
+
[1m[35m (0.1ms)[0m begin transaction
|
130
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
131
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
132
|
+
[1m[35m (0.1ms)[0m begin transaction
|
133
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
134
|
+
[1m[35m (0.1ms)[0m begin transaction
|
135
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
136
|
+
[1m[35m (0.1ms)[0m begin transaction
|
137
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
138
|
+
[1m[35m (0.1ms)[0m begin transaction
|
139
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
140
|
+
[1m[35m (0.1ms)[0m begin transaction
|
141
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
142
|
+
[1m[35m (0.1ms)[0m begin transaction
|
143
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
144
|
+
[1m[35m (0.1ms)[0m begin transaction
|
145
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
146
|
+
[1m[35m (0.1ms)[0m begin transaction
|
147
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
148
|
+
[1m[35m (0.1ms)[0m begin transaction
|
149
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
150
|
+
[1m[35m (0.1ms)[0m begin transaction
|
151
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
152
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
153
|
+
[1m[35m (0.1ms)[0m begin transaction
|
154
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
155
|
+
[1m[35m (0.1ms)[0m begin transaction
|
156
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
157
|
+
[1m[35m (0.1ms)[0m begin transaction
|
158
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
159
|
+
[1m[35m (0.1ms)[0m begin transaction
|
160
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
161
|
+
[1m[35m (0.1ms)[0m begin transaction
|
162
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
163
|
+
[1m[35m (0.1ms)[0m begin transaction
|
164
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
165
|
+
[1m[35m (0.1ms)[0m begin transaction
|
166
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
167
|
+
[1m[35m (0.1ms)[0m begin transaction
|
168
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
169
|
+
[1m[35m (0.1ms)[0m begin transaction
|
170
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
171
|
+
[1m[35m (0.1ms)[0m begin transaction
|
172
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
173
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
174
|
+
[1m[35m (0.1ms)[0m begin transaction
|
175
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
176
|
+
[1m[35m (0.1ms)[0m begin transaction
|
177
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
178
|
+
[1m[35m (0.1ms)[0m begin transaction
|
179
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
180
|
+
[1m[35m (0.1ms)[0m begin transaction
|
181
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
182
|
+
[1m[35m (0.1ms)[0m begin transaction
|
183
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
184
|
+
[1m[35m (0.1ms)[0m begin transaction
|
185
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
186
|
+
[1m[35m (0.1ms)[0m begin transaction
|
187
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
188
|
+
[1m[35m (0.1ms)[0m begin transaction
|
189
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
190
|
+
[1m[35m (0.1ms)[0m begin transaction
|
191
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
192
|
+
[1m[35m (0.1ms)[0m begin transaction
|
193
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
194
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
195
|
+
[1m[35m (0.1ms)[0m begin transaction
|
196
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
197
|
+
[1m[35m (0.1ms)[0m begin transaction
|
198
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
199
|
+
[1m[35m (0.1ms)[0m begin transaction
|
200
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
201
|
+
[1m[35m (0.1ms)[0m begin transaction
|
202
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
203
|
+
[1m[35m (0.1ms)[0m begin transaction
|
204
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
205
|
+
[1m[35m (0.1ms)[0m begin transaction
|
206
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
207
|
+
[1m[35m (0.1ms)[0m begin transaction
|
208
|
+
[1m[36m (0.2ms)[0m [1mrollback transaction[0m
|
209
|
+
[1m[35m (0.1ms)[0m begin transaction
|
210
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
211
|
+
[1m[35m (0.1ms)[0m begin transaction
|
212
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
213
|
+
[1m[35m (0.1ms)[0m begin transaction
|
214
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
215
|
+
[1m[35m (0.1ms)[0m begin transaction
|
216
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
217
|
+
[1m[35m (0.1ms)[0m begin transaction
|
218
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
219
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
220
|
+
[1m[35m (0.1ms)[0m begin transaction
|
221
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
222
|
+
[1m[35m (0.1ms)[0m begin transaction
|
223
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
224
|
+
[1m[35m (0.1ms)[0m begin transaction
|
225
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
226
|
+
[1m[35m (0.1ms)[0m begin transaction
|
227
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
228
|
+
[1m[35m (0.1ms)[0m begin transaction
|
229
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
230
|
+
[1m[35m (0.1ms)[0m begin transaction
|
231
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
232
|
+
[1m[35m (0.1ms)[0m begin transaction
|
233
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
234
|
+
[1m[35m (0.1ms)[0m begin transaction
|
235
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
236
|
+
[1m[35m (0.1ms)[0m begin transaction
|
237
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
238
|
+
[1m[35m (0.1ms)[0m begin transaction
|
239
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
240
|
+
[1m[35m (0.1ms)[0m begin transaction
|
241
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
242
|
+
[1m[35m (0.1ms)[0m begin transaction
|
243
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
244
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.5ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
245
|
+
[1m[35m (0.1ms)[0m begin transaction
|
246
|
+
[1m[36m (0.2ms)[0m [1mrollback transaction[0m
|
247
|
+
[1m[35m (0.1ms)[0m begin transaction
|
248
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
249
|
+
[1m[35m (0.1ms)[0m begin transaction
|
250
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
251
|
+
[1m[35m (0.1ms)[0m begin transaction
|
252
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
253
|
+
[1m[35m (0.1ms)[0m begin transaction
|
254
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
255
|
+
[1m[35m (0.1ms)[0m begin transaction
|
256
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
257
|
+
[1m[35m (0.1ms)[0m begin transaction
|
258
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
259
|
+
[1m[35m (0.1ms)[0m begin transaction
|
260
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
261
|
+
[1m[35m (0.1ms)[0m begin transaction
|
262
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
263
|
+
[1m[35m (0.1ms)[0m begin transaction
|
264
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
265
|
+
[1m[35m (0.1ms)[0m begin transaction
|
266
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
267
|
+
[1m[35m (0.1ms)[0m begin transaction
|
268
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe User do
|
4
|
+
shared_examples 'it has the enum module' do
|
5
|
+
it 'has the enum module' do
|
6
|
+
expect(User::Role).to be_a Module
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
shared_examples 'it sets default values' do
|
11
|
+
it 'sets the default values' do
|
12
|
+
expect(User::Role::ADMIN).to eq(0)
|
13
|
+
expect(User::Role::AUTHOR).to eq(1)
|
14
|
+
expect(User::Role::EDITOR).to eq(2)
|
15
|
+
expect(User::Role::USER).to eq(3)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'sets the right value for an instance' do
|
19
|
+
expect(u_admin.role).to eq(0)
|
20
|
+
expect(u_author.role).to eq(1)
|
21
|
+
expect(u_editor.role).to eq(2)
|
22
|
+
expect(u_user.role).to eq(3)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
shared_examples 'it sets default labels' do
|
27
|
+
it 'sets the default labels' do
|
28
|
+
expect(User::Role::ADMIN_LABEL).to eq('Admin')
|
29
|
+
expect(User::Role::AUTHOR_LABEL).to eq('Author')
|
30
|
+
expect(User::Role::EDITOR_LABEL).to eq('Editor')
|
31
|
+
expect(User::Role::USER_LABEL).to eq('User')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
shared_examples 'it sets the appropriate values' do |admin_value, author_value, editor_value, user_value|
|
36
|
+
it 'sets the appropriate values' do
|
37
|
+
expect(User::Role::ADMIN).to eq(admin_value)
|
38
|
+
expect(User::Role::AUTHOR).to eq(author_value)
|
39
|
+
expect(User::Role::EDITOR).to eq(editor_value)
|
40
|
+
expect(User::Role::USER).to eq(user_value)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'sets the right value for an instance' do
|
44
|
+
expect(u_admin.role).to eq(admin_value)
|
45
|
+
expect(u_author.role).to eq(author_value)
|
46
|
+
expect(u_editor.role).to eq(editor_value)
|
47
|
+
expect(u_user.role).to eq(user_value)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
let(:u_admin) { User.new(role: User::Role::ADMIN) }
|
52
|
+
let(:u_author) { User.new(role: User::Role::AUTHOR) }
|
53
|
+
let(:u_editor) { User.new(role: User::Role::EDITOR) }
|
54
|
+
let(:u_user) { User.new(role: User::Role::USER) }
|
55
|
+
|
56
|
+
def clear_user
|
57
|
+
if User.const_defined?(:Role)
|
58
|
+
User.send(:remove_const, :Role)
|
59
|
+
end
|
60
|
+
|
61
|
+
if User.instance_variable_defined?(:@_attr_enums)
|
62
|
+
User.remove_instance_variable(:@_attr_enums)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when passing all symbols' do
|
67
|
+
before :each do
|
68
|
+
clear_user
|
69
|
+
|
70
|
+
User.class_eval do
|
71
|
+
extend RailsAttrEnum
|
72
|
+
attr_enum :role, :admin, :author, :editor, :user
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it_behaves_like 'it has the enum module'
|
77
|
+
it_behaves_like 'it sets default values'
|
78
|
+
it_behaves_like 'it sets default labels'
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'when specifying values' do
|
82
|
+
context 'with a mix of symbols and hashes and a symbol is first' do
|
83
|
+
before :each do
|
84
|
+
clear_user
|
85
|
+
|
86
|
+
User.class_eval do
|
87
|
+
extend RailsAttrEnum
|
88
|
+
attr_enum :role, :admin, { author: 12 }, :editor, { user: 42 }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it_behaves_like 'it has the enum module'
|
93
|
+
it_behaves_like 'it sets the appropriate values', 0, 12, 1, 42
|
94
|
+
it_behaves_like 'it sets default labels'
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'with a hash' do
|
98
|
+
before :each do
|
99
|
+
clear_user
|
100
|
+
|
101
|
+
User.class_eval do
|
102
|
+
extend RailsAttrEnum
|
103
|
+
attr_enum :role, admin: 1, author: 2, editor: 4, user: 8
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
it_behaves_like 'it has the enum module'
|
108
|
+
it_behaves_like 'it sets the appropriate values', 1, 2, 4, 8
|
109
|
+
it_behaves_like 'it sets default labels'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
2
|
+
ENV["RAILS_ENV"] ||= 'test'
|
3
|
+
require File.expand_path("../../config/environment", __FILE__)
|
4
|
+
require 'rspec/rails'
|
5
|
+
require 'rspec/autorun'
|
6
|
+
|
7
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
8
|
+
# in spec/support/ and its subdirectories.
|
9
|
+
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
|
10
|
+
|
11
|
+
# Checks for pending migrations before tests are run.
|
12
|
+
# If you are not using ActiveRecord, you can remove this line.
|
13
|
+
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
# ## Mock Framework
|
17
|
+
#
|
18
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
19
|
+
#
|
20
|
+
# config.mock_with :mocha
|
21
|
+
# config.mock_with :flexmock
|
22
|
+
# config.mock_with :rr
|
23
|
+
|
24
|
+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
25
|
+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
26
|
+
|
27
|
+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
28
|
+
# examples within a transaction, remove the following line or assign false
|
29
|
+
# instead of true.
|
30
|
+
config.use_transactional_fixtures = true
|
31
|
+
|
32
|
+
# If true, the base class of anonymous controllers will be inferred
|
33
|
+
# automatically. This will be the default behavior in future versions of
|
34
|
+
# rspec-rails.
|
35
|
+
config.infer_base_class_for_anonymous_controllers = false
|
36
|
+
|
37
|
+
# Run specs in random order to surface order dependencies. If you find an
|
38
|
+
# order dependency and want to debug it, you can fix the order by providing
|
39
|
+
# the seed, which is printed after each run.
|
40
|
+
# --seed 1234
|
41
|
+
config.order = "random"
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_attr_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Fairbank
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description: Create
|
41
|
+
description: Create enums for Rails model attributes like enums in C languages
|
42
42
|
email:
|
43
43
|
- elpapapollo@gmail.com
|
44
44
|
executables: []
|
@@ -55,7 +55,7 @@ files:
|
|
55
55
|
- lib/tasks/rails_attr_enum_tasks.rake
|
56
56
|
- LICENSE
|
57
57
|
- Rakefile
|
58
|
-
- README.
|
58
|
+
- README.md
|
59
59
|
- test/dummy/app/assets/javascripts/application.js
|
60
60
|
- test/dummy/app/assets/stylesheets/application.css
|
61
61
|
- test/dummy/app/controllers/application_controller.rb
|
@@ -85,19 +85,20 @@ files:
|
|
85
85
|
- test/dummy/db/development.sqlite3
|
86
86
|
- test/dummy/db/migrate/20131023212242_create_users.rb
|
87
87
|
- test/dummy/db/schema.rb
|
88
|
+
- test/dummy/db/test.sqlite3
|
88
89
|
- test/dummy/log/development.log
|
90
|
+
- test/dummy/log/test.log
|
89
91
|
- test/dummy/public/404.html
|
90
92
|
- test/dummy/public/422.html
|
91
93
|
- test/dummy/public/500.html
|
92
94
|
- test/dummy/public/favicon.ico
|
93
95
|
- test/dummy/Rakefile
|
94
96
|
- test/dummy/README.rdoc
|
95
|
-
- test/dummy/
|
96
|
-
- test/dummy/
|
97
|
-
- test/rails_attr_enum_test.rb
|
98
|
-
- test/test_helper.rb
|
97
|
+
- test/dummy/spec/models/user_spec.rb
|
98
|
+
- test/dummy/spec/spec_helper.rb
|
99
99
|
homepage: https://github.com/jfairbank/rails_attr_enum
|
100
|
-
licenses:
|
100
|
+
licenses:
|
101
|
+
- MIT
|
101
102
|
metadata: {}
|
102
103
|
post_install_message:
|
103
104
|
rdoc_options: []
|
@@ -149,14 +150,14 @@ test_files:
|
|
149
150
|
- test/dummy/db/development.sqlite3
|
150
151
|
- test/dummy/db/migrate/20131023212242_create_users.rb
|
151
152
|
- test/dummy/db/schema.rb
|
153
|
+
- test/dummy/db/test.sqlite3
|
152
154
|
- test/dummy/log/development.log
|
155
|
+
- test/dummy/log/test.log
|
153
156
|
- test/dummy/public/404.html
|
154
157
|
- test/dummy/public/422.html
|
155
158
|
- test/dummy/public/500.html
|
156
159
|
- test/dummy/public/favicon.ico
|
157
160
|
- test/dummy/Rakefile
|
158
161
|
- test/dummy/README.rdoc
|
159
|
-
- test/dummy/
|
160
|
-
- test/dummy/
|
161
|
-
- test/rails_attr_enum_test.rb
|
162
|
-
- test/test_helper.rb
|
162
|
+
- test/dummy/spec/models/user_spec.rb
|
163
|
+
- test/dummy/spec/spec_helper.rb
|
data/README.rdoc
DELETED
data/test/test_helper.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# Configure Rails Environment
|
2
|
-
ENV["RAILS_ENV"] = "test"
|
3
|
-
|
4
|
-
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
5
|
-
require "rails/test_help"
|
6
|
-
|
7
|
-
Rails.backtrace_cleaner.remove_silencers!
|
8
|
-
|
9
|
-
# Load support files
|
10
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
11
|
-
|
12
|
-
# Load fixtures from the engine
|
13
|
-
if ActiveSupport::TestCase.method_defined?(:fixture_path=)
|
14
|
-
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
|
15
|
-
end
|