|
Enum type manager. It creates a separated class to hold each enum set that can be used by multiple models, it also keeps the database data consistent. The enum type is known to have a better performance against string- and integer-like enums. [PostgreSQL Docs](https://www.postgresql.org/docs/9.2/static/datatype-enum.html)
|
|
Enum type manager. It creates a separated class to hold each enum set that can be used by multiple models, it also keeps the database consistent. The enum type is known to have better performance against string- and integer-like enums. [PostgreSQL Docs](https://www.postgresql.org/docs/9.2/static/datatype-enum.html)
|
|
|
|
|
|
# How it works
|
|
# How it works
|
|
|
|
|
... | @@ -34,7 +34,7 @@ add_enum_values :status, %i(baz qux), suffix: 'tst' # With a specific suff |
... | @@ -34,7 +34,7 @@ add_enum_values :status, %i(baz qux), suffix: 'tst' # With a specific suff |
|
drop_type :status
|
|
drop_type :status
|
|
```
|
|
```
|
|
|
|
|
|
Once you've created the type, you can use while creating your table in three ways
|
|
Once you've created the type, you can use it while creating your table in three ways
|
|
```ruby
|
|
```ruby
|
|
create_table :users do |t|
|
|
create_table :users do |t|
|
|
t.string :name
|
|
t.string :name
|
... | @@ -46,7 +46,7 @@ end |
... | @@ -46,7 +46,7 @@ end |
|
|
|
|
|
### The type class
|
|
### 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.
|
|
Each enum type 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
|
|
```ruby
|
|
Enum::Roles
|
|
Enum::Roles
|
|
|
|
|
... | @@ -56,19 +56,19 @@ Enum::Roles.admin |
... | @@ -56,19 +56,19 @@ Enum::Roles.admin |
|
# Or you can get the list of values
|
|
# Or you can get the list of values
|
|
Enum::Roles.values
|
|
Enum::Roles.values
|
|
|
|
|
|
# Allows you to iterate over the values direct from the class
|
|
# Allows you to iterate over the values directly from the class
|
|
Enum::Roles.each do |role|
|
|
Enum::Roles.each do |role|
|
|
puts role
|
|
puts role
|
|
end
|
|
end
|
|
|
|
|
|
# You can use index-based reference of a value
|
|
# You can use index-based references of a value
|
|
Enum::Roles.new(0) # #<Enum::Roles "visitor">
|
|
Enum::Roles.new(0) # #<Enum::Roles "visitor">
|
|
Enum::Roles.admin.to_i # 2
|
|
Enum::Roles.admin.to_i # 2
|
|
```
|
|
```
|
|
|
|
|
|
### Models
|
|
### 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).
|
|
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 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
|
|
```ruby
|
|
# models/user.rb
|
|
# models/user.rb
|
|
class User < ActiveRecord::Base
|
|
class User < ActiveRecord::Base
|
... | | ... | |