... | @@ -46,7 +46,7 @@ end |
... | @@ -46,7 +46,7 @@ end |
|
|
|
|
|
### The type class
|
|
### The type class
|
|
|
|
|
|
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.
|
|
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/Configuring#enum.namespace) namespace.
|
|
```ruby
|
|
```ruby
|
|
Enum::Roles
|
|
Enum::Roles
|
|
|
|
|
... | @@ -68,7 +68,7 @@ Enum::Roles.admin.to_i # 2 |
... | @@ -68,7 +68,7 @@ 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 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/Configuring#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/Configuring#enum.base_method).
|
|
```ruby
|
|
```ruby
|
|
# models/user.rb
|
|
# models/user.rb
|
|
class User < ActiveRecord::Base
|
|
class User < ActiveRecord::Base
|
... | @@ -100,14 +100,14 @@ user.role.manager? # true |
... | @@ -100,14 +100,14 @@ user.role.manager? # true |
|
user.visitor? # false
|
|
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):
|
|
The `bang!` methods are controlled by the [`enum.save_on_bang`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#enum.save_on_bang):
|
|
```ruby
|
|
```ruby
|
|
# The following will only perform a save on the database if enum.save_on_bang is set to true
|
|
# 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 = User.new(role: :manager)
|
|
user.admin!
|
|
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.
|
|
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/Configuring#enum.i18n_scopes). On the third one, only the scopes on [`enum.i18n_type_scope`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#enum.i18n_type_scope) are used, that allows per-model customization.
|
|
```ruby
|
|
```ruby
|
|
user = User.new(role: :manager)
|
|
user = User.new(role: :manager)
|
|
user.role.text # User's manager
|
|
user.role.text # User's manager
|
... | | ... | |