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

Enum · Changes

Page history
Complete usage authored Apr 11, 2017 by Carlos's avatar Carlos
Hide whitespace changes
Inline Side-by-side
Enum.md
View page @ d3eff6fa
......@@ -6,7 +6,7 @@ Enum type manager. It creates a separated class to hold each enum set that can b
First you have to create the enum during your migration, since it's the database that holds the list of possible values.
```ruby
create_enum :status, %i(created draft published archived)
create_enum :roles, %i(visitor manager admin)
```
Some other examples are:
......@@ -34,5 +34,83 @@ add_enum_values :status, %i(baz qux), suffix: 'tst' # With a specific suff
drop_type :status
```
Once you've created the type, you can use while creating your table in three ways
```ruby
create_table :users do |t|
t.string :name
t.role :role # Uses the type of the column, since enum is a type
t.enum :status # Figures the type name from the column name
t.enum :last_status, subtype: :status # Explicit tells the type to be used
end
```
### The type class
Each enum type that is loaded from the database will have it's own class type of value, created under the [`enum.namespace`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.namespace) namespace.
```ruby
Enum::Roles
# You can get a representative value from there
Enum::Roles.admin
# Or you can get the list of values
Enum::Roles.values
# Allows you to iterate over the values direct from the class
Enum::Roles.each do |role|
puts role
end
# You can use index-based reference of a value
Enum::Roles.new(0) # #<Enum::Roles "visitor">
Enum::Roles.admin.to_i # 2
```
### Models
If you kept the [`enum.initializer`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.initializer) setting as `false`, you have to go to each of your models and enable the functionality for each enum type of field. You don't need to provide the values since they will be loaded from the database. The method name is defined on [`enum.base_method`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.base_method).
```ruby
# models/user.rb
class User < ActiveRecord::Base
enum :role, :status
end
```
You are able to access the list of values trough the class that holds the enum:
```ruby
User.roles
```
You can set the column value from `String`, `Symbol`, and `Integer`:
```ruby
user = User.new
user.role = 'admin' # #<Enum::Roles "admin">
user.role = :manager # #<Enum::Roles "manager">
user.role = 0 # #<Enum::Roles "visitor">
```
This allows you to compare values, or make questions about it:
```ruby
other_user = User.new(role: :manager)
user = User.new(role: :manager)
user.role > :admin # false
user.role == 1 # true
user.role >= other_user.role # true
user.role.manager? # true
user.visitor? # false
```
The `bang!` methods are controlled by the [`enum.save_on_bang`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.save_on_bang):
```ruby
# The following will only perform a save on the database if enum.save_on_bang is set to true
user = User.new(role: :manager)
user.admin!
```
You can reach the I18n translations from three different ways, and the scopes are configured on [`enum.i18n_scopes`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.i18n_scopes). On the third one, only the scopes on [`enum.i18n_type_scope`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.i18n_type_scope) are used, that allows per-model customization.
```ruby
user = User.new(role: :manager)
user.role.text # User's manager
user.role_text # User's manager
Enum::Roles.manager.text # Manager
```
\ No newline at end of file
Clone repository
  • Arel
  • Auxiliary Statements
  • Belongs to Many
  • Box
  • Circle
  • Configuring
  • Date Time Range
  • Distinct On
  • Dynamic Attributes
  • Enum Set
  • Enum
  • Has Many
  • Home
  • Inherited Tables
  • Insert All
View All Pages