rails_attr_enum 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTZmNmQyMTg3YzVmN2VmYjgyNGFmZDJkYjZiMzhlYzFkYmRjMDRhMg==
4
+ ZjQyNmMyOWJkNGFjZmViNjBlZTg5OGEzYTAyZjc2N2MwOTUyYjk5Ng==
5
5
  data.tar.gz: !binary |-
6
- NzUyMTU2NTJhMDIzYzY0NjU4MmJjMGE5MmE5ZGVjYzIwNDJiNTllOQ==
6
+ OTNhYjFjOGFiZjQ1NTg3N2QzNjE5YmFkZWNjNmVkM2EwZWVjZDk2OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTM2MDQwMGRmYTU0ZWM5Njg1MTMxMzJjNWM1ZDUwZTFjNDI2MTZhMmZmZTI1
10
- ODEwZDMyOWNhZDkyNmJlNDM5MjFiMWI0ZjQ0YmYxNTczNGI1MThkYzlhMDU4
11
- NzRmNTQwYjZlM2JlNGU2ZmYzOThlMzhlYzJlYWMzNzI5Y2I3MGM=
9
+ ZmNmNDYxMjFlY2VkNWQ3MjZmNTQwNTdkYzEwYTgyNDM0Yjg2MmU2ZmRjODgy
10
+ MGI4YTJhMzU2MDQ5MTBhYWVmMTM4N2UzZmM0NDk4NjUzM2M3MGUxYTFmM2Ey
11
+ ZWFhN2Y1YjFlN2EwNjQ2YTNlZTM4ZGQzY2Q1OTM3ZGM3YjcyODE=
12
12
  data.tar.gz: !binary |-
13
- OWVhOTEwNDM0YWJlYTg3ZTc4NGExNmRlNGRiNzhmNDcyNDQyNTY0MzdlZDdk
14
- ZGNiM2M3NGQ1Y2QwMDIwNWE2OTU0Zjc1MjZjOTk1NTI0NDJkYWQ5NjYyNzY5
15
- Y2ZhNzIxYzRkYmNhOTM5NWFlMmNmOGZmNGRkMmMzNjdhYWZmZTU=
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, :author, :editor, :user
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::AUTHOR == 1
23
- User::Role::EDITOR == 2
22
+ User::Role::EDITOR == 1
23
+ User::Role::AUTHOR == 2
24
24
  User::Role::USER == 3
25
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`.
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
- 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.
28
+ ### Helpers for Model Instances
32
29
 
33
- # Target specific identifiers
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
- attr_enum :role, :admin, { author: 12 }, :editor, { user: 42 }
38
- end
32
+ Get the "display" label for the current value with the `display_*` method:
39
33
 
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
34
+ user = User.new(User::Role::ADMIN)
35
+ user.display_role == 'Admin'
44
36
 
45
- # Use a hash to specify all values
46
- class User < ActiveRecord::Base
47
- extend RailsAttrEnum
37
+ You can check for a specific value with a query `*?` method:
48
38
 
49
- attr_enum :role, {
50
- admin: 1,
51
- author: 2,
52
- editor: 4,
53
- user: 8
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
- User::Role::ADMIN == 1
58
- User::Role::AUTHOR == 2
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
- # Use a block to specify some (or all)
63
- class User < ActiveRecord::Base
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
- attr_enum :role do
67
- add admin: 42
68
- add :author
69
- add :editor
70
- add user: 7
71
- end
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::ADMIN == 42
75
- User::Role::AUTHOR == 0 # Again notice how `AUTHOR` and `EDITOR` defaulted
76
- User::Role::EDITOR == 1
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
- ### 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.
62
+ If you want the hash to work as expected than call the `.value` method on the
63
+ attribute:
84
64
 
85
- class User < ActiveRecord::Base
86
- extend RailsAttrEnum
65
+ alt_label = alt_label_map[user.role.value]
66
+ alt_label == 'An editor'
87
67
 
88
- attr_enum :role, :admin, :author, :editor, :user
89
- end
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::ADMIN_LABEL == 'Admin'
92
- User::Role::AUTHOR_LABEL == 'Author'
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
- user = User.new(role: User::Role::ADMIN)
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
- You can specify your own labels if you like. By default, RailAttrEnum calls
100
- `.to_s.titleize` on the symbol identifier.
76
+ user = User.new
77
+ user.role.user!
78
+ user.display_role == 'User'
101
79
 
102
- class User < ActiveRecord::Base
103
- extend RailsAttrEnum
80
+ user.role.author!
81
+ user.display_role == 'Author'
104
82
 
105
- attr_enum :role,
106
- { admin: 'Admin Role' }, :author, { editor: 'Editor Role' }, :user
107
- end
83
+ ### Scopes for Models
108
84
 
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'
85
+ Convenient scopes are created for each possible enum value on the model class:
113
86
 
114
- # With a hash
115
- class User < ActiveRecord::Base
116
- extend RailsAttrEnum
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
- attr_enum :role, {
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
- 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'
94
+ Helper methods are added to the actual `Enum` module as well.
130
95
 
131
- # With a block
132
- class User < ActiveRecord::Base
133
- extend RailsAttrEnum
96
+ `get_label` and `get_key` get the (surprise!) label and key for a given enum
97
+ value:
134
98
 
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
99
+ User::Role.get_label(User::Role::ADMIN) == 'Admin'
100
+ User::Role.get_key(User::Role::USER) == :user
142
101
 
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'
102
+ ---
147
103
 
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.
104
+ `attr_name` returns the attribute symbol
151
105
 
152
- class User < ActiveRecord::Base
153
- extend RailsAttrEnum
106
+ User::Role.attr_name == :role
154
107
 
155
- attr_enum :role, { admin: { label: 'Admin Role', value: 1 } },
156
- { author: 'Author Role' },
157
- { editor: 42 },
158
- :user
159
- end
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
- 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'
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.
@@ -6,7 +6,6 @@ module RailsAttrEnum
6
6
  # Add a to_h method if it doesn't exist (< ruby 2.0)
7
7
  unless nil.respond_to?(:to_h)
8
8
  def to_h
9
- # Hash[*(entry.members.flat_map { |key| [key, entry.send(key)] })]
10
9
  Hash[members.map { |key| [key, send(key)] }]
11
10
  end
12
11
  end
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  module RailsAttrEnum
2
4
 
3
5
  ## Enum representation
@@ -1,3 +1,3 @@
1
1
  module RailsAttrEnum
2
- VERSION = '0.0.6'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -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)
Binary file
@@ -97,3 +97,18 @@ DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead. (
97
97
   (0.1ms) SELECT version FROM "schema_migrations"
98
98
   (1.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
99
99
  ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
100
+  (3.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
101
+  (1.6ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
102
+  (1.6ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
103
+  (0.2ms) SELECT version FROM "schema_migrations"
104
+  (1.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
105
+  (0.1ms) begin transaction
106
+  (0.1ms) rollback transaction
107
+ User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."role" = 0
108
+ User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."role" = 1
109
+  (0.3ms) SELECT COUNT(*) FROM "users" WHERE "users"."role" = 1
110
+  (0.4ms) SELECT COUNT(*) FROM "users" WHERE "users"."role" = 0
111
+  (0.2ms) begin transaction
112
+  (0.1ms) rollback transaction
113
+  (0.3ms) begin transaction
114
+  (0.1ms) rollback transaction