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 CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZjQyNmMyOWJkNGFjZmViNjBlZTg5OGEzYTAyZjc2N2MwOTUyYjk5Ng==
5
- data.tar.gz: !binary |-
6
- OTNhYjFjOGFiZjQ1NTg3N2QzNjE5YmFkZWNjNmVkM2EwZWVjZDk2OA==
2
+ SHA1:
3
+ metadata.gz: 3bb8000f3dc7b7ea0f1cf0c782cb0e7b1494070d
4
+ data.tar.gz: 840eb22350179ec480a12db0964c937067963632
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZmNmNDYxMjFlY2VkNWQ3MjZmNTQwNTdkYzEwYTgyNDM0Yjg2MmU2ZmRjODgy
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
- 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.
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
- ### Usage
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
- Here's an example given a class `User` with an attribute `role`:
10
+ ## Installation
11
+ Add to your Gemfile:
12
12
 
13
- # Example model User for a blog app
14
- class User < ActiveRecord::Base
15
- extend RailsAttrEnum
13
+ ```ruby
14
+ gem 'rails_attr_enum'
15
+ ```
16
16
 
17
- attr_enum :role, :admin, :editor, :author, :user
18
- end
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
- # Creates module `User::Role` with constants for each possible value
21
- User::Role::ADMIN == 0
22
- User::Role::EDITOR == 1
23
- User::Role::AUTHOR == 2
24
- User::Role::USER == 3
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
- ### Helpers for Model Instances
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
- user = User.new(User::Role::ADMIN)
35
- user.display_role == 'Admin'
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
- 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
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
- 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
- }
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
- user = User.new(User::Role::EDITOR)
59
- alt_label = alt_label_map[user.role]
60
- alt_label == nil # not 'An editor'
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 than call the `.value` method on the
81
+ If you want the hash to work as expected then call the `.value` method on the
63
82
  attribute:
64
83
 
65
- alt_label = alt_label_map[user.role.value]
66
- alt_label == 'An editor'
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
- user = User.new(User::Role::ADMIN)
72
- user.role.key == :admin
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
- user = User.new
77
- user.role.user!
78
- user.display_role == 'User'
99
+ ```ruby
100
+ user = User.new
101
+ user.role.user!
102
+ user.display_role == 'User'
79
103
 
80
- user.role.author!
81
- user.display_role == 'Author'
104
+ user.role.author!
105
+ user.display_role == 'Author'
106
+ ```
82
107
 
83
- ### Scopes for Models
108
+ ## Scopes for Models
84
109
 
85
110
  Convenient scopes are created for each possible enum value on the model class:
86
111
 
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)
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
- ### Enum Helper Methods
119
+ ## Enum Helper Methods
93
120
 
94
121
  Helper methods are added to the actual `Enum` module as well.
95
122
 
96
- `get_label` and `get_key` get the (surprise!) label and key for a given enum
123
+ ### get_label and get_key
124
+ Get the (surprise!) label and key for a given enum
97
125
  value:
98
126
 
99
- User::Role.get_label(User::Role::ADMIN) == 'Admin'
100
- User::Role.get_key(User::Role::USER) == :user
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
- `attr_name` returns the attribute symbol
135
+ ```ruby
136
+ User::Role.attr_name == :role
137
+ ```
105
138
 
106
- User::Role.attr_name == :role
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
- `keys` returns all the enum keys
146
+ ### values
147
+ Return all the enum values
111
148
 
112
- User::Role.keys == [:admin, :editor, :author, :user]
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
- `values` returns all the enum values
156
+ ```ruby
157
+ User::Role.labels == ['Admin', 'Editor', 'Author', 'User']
158
+ ```
117
159
 
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
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 (which uses Formtastic)](https://github.com/gregbell/active_admin)
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
- User::Role.label_value_pairs ==
136
- [['Admin', 0], ['Editor', 1], ['Author', 2], ['User', 3]]
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
- # 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\"}}"
168
-
169
- ### Feedback and Pull Requests Welcome
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.
@@ -23,8 +23,15 @@ module RailsAttrEnum
23
23
  @entries << entry
24
24
  end
25
25
 
26
- def label_value_pairs
27
- labels.zip values
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)
@@ -1,3 +1,3 @@
1
1
  module RailsAttrEnum
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
Binary file
@@ -112,3 +112,21 @@ DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead. (
112
112
   (0.1ms) rollback transaction
113
113
   (0.3ms) begin transaction
114
114
   (0.1ms) rollback transaction
115
+  (1.7ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
116
+  (1.5ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
117
+  (1.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
118
+  (0.2ms) SELECT version FROM "schema_migrations"
119
+  (1.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
120
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
121
+  (1.3ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
122
+  (1.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
123
+  (1.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
124
+  (0.1ms) SELECT version FROM "schema_migrations"
125
+  (0.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
126
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
127
+  (18.1ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "role" integer, "created_at" datetime, "updated_at" datetime) 
128
+  (1.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
129
+  (1.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
130
+  (0.2ms) SELECT version FROM "schema_migrations"
131
+  (1.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20131023212242')
132
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"