... | ... | @@ -31,6 +31,9 @@ Enum::PermissionsSet.read |
|
|
# Or you can get the list of values
|
|
|
Enum::PermissionsSet.values
|
|
|
|
|
|
# You can also get all the values as symbols
|
|
|
Enum::PermissionsSet.keys
|
|
|
|
|
|
# Allows you to iterate over the values directly from the class
|
|
|
Enum::PermissionsSet.each do |permission|
|
|
|
puts permission
|
... | ... | @@ -54,7 +57,8 @@ end |
|
|
|
|
|
You are able to access the list of values trough the class that holds the enum set:
|
|
|
```ruby
|
|
|
Post.creator_permissions
|
|
|
Post.creator_permissions # List of strings
|
|
|
Post.creator_permissions_keys # List of symbols
|
|
|
```
|
|
|
|
|
|
You can set the column value from `String`, `Array`, `Symbol`, and `Integer`:
|
... | ... | @@ -105,6 +109,20 @@ post.creator_permissions | [:exec] # [:read, :write, :exec] |
|
|
post.creator_permissions -= :write # [:read]
|
|
|
```
|
|
|
|
|
|
#### Scopes
|
|
|
|
|
|
For each value, the model where the enum was defined receives additional scopes, making it easy to filter records.
|
|
|
```ruby
|
|
|
Post.write # SELECT * FROM "posts" WHERE "posts"."creator_permissions" @> ARRAY['write']::permissions[]
|
|
|
# Where @> means 'contains', and ::permissions[] means 'as an array of values from permissions enum type'
|
|
|
```
|
|
|
|
|
|
There are also additional scopes exclusively for enum set, where you can filter by either all the values must be present or any value must be present.
|
|
|
```ruby
|
|
|
Post.has_creator_permissions('read', 'write') # Records that have both 'read' and 'write' values
|
|
|
Post.has_any_creator_permissions('write', 'exec') # Records that have 'write', 'exec', or both values
|
|
|
```
|
|
|
|
|
|
### Testing
|
|
|
|
|
|
When testing models or creating records using factories, `EnumSet` provides an easy way to get a random number of values from the list of any enum types. Besides the normal `Post.creator_permissions.sample`, you can also use:
|
... | ... | |