Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • T torque-postgresql
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 6
    • Issues 6
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Carlos
  • torque-postgresql
  • Wiki
  • Enum Set

Enum Set · Changes

Page history
Updated Enum Set (markdown) authored Dec 16, 2019 by Carlos's avatar Carlos
Hide whitespace changes
Inline Side-by-side
Enum-Set.md
View page @ afab8e1e
...@@ -31,6 +31,9 @@ Enum::PermissionsSet.read ...@@ -31,6 +31,9 @@ Enum::PermissionsSet.read
# Or you can get the list of values # Or you can get the list of values
Enum::PermissionsSet.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 # Allows you to iterate over the values directly from the class
Enum::PermissionsSet.each do |permission| Enum::PermissionsSet.each do |permission|
puts permission puts permission
...@@ -54,7 +57,8 @@ end ...@@ -54,7 +57,8 @@ end
You are able to access the list of values trough the class that holds the enum set: You are able to access the list of values trough the class that holds the enum set:
```ruby ```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`: You can set the column value from `String`, `Array`, `Symbol`, and `Integer`:
...@@ -105,6 +109,20 @@ post.creator_permissions | [:exec] # [:read, :write, :exec] ...@@ -105,6 +109,20 @@ post.creator_permissions | [:exec] # [:read, :write, :exec]
post.creator_permissions -= :write # [:read] 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 ### 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: 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:
......
Clone repository
  • Wiki
  • Configuring

Core Extensions

  • Range

Data types

  • Enum
  • Enum Set
  • Interval
  • Date/Time Range
  • Box
  • Circle
  • Line
  • Segment

Querying

  • Arel
  • Has Many
  • Belongs to Many
  • Dynamic Attributes
  • Distinct On
  • Auxiliary Statements
  • Inherited Tables