Add documentation for enum traits [ci skip]

This commit adds top-level documentation for the enum traits feature
adding in #1380
This commit is contained in:
Daniel Colson 2020-06-18 20:29:29 -04:00
parent 3cef28546f
commit 69ec70b2de
1 changed files with 93 additions and 0 deletions

View File

@ -61,6 +61,7 @@ Getting Started
+ [With associations](#with-associations-1)
+ [Traits within traits](#traits-within-traits)
+ [With transient attributes](#with-transient-attributes)
+ [Enum traits](#enum-traits)
* [Callbacks](#callbacks)
+ [Default callbacks](#default-callbacks)
+ [Multiple callbacks](#multiple-callbacks)
@ -1107,6 +1108,98 @@ end
create :invoice, :with_amount, amount: 2
```
### Enum traits
Given an Active Record model with an enum attribute:
```rb
class Task < ActiveRecord::Base
enum status: {queued: 0, started: 1, finished: 2}
end
```
factory\_bot will automatically define traits for each possible value of the
enum:
```rb
FactoryBot.define do
factory :task
end
FactoryBot.build(:task, :queued)
FactoryBot.build(:task, :started)
FactoryBot.build(:task, :finished)
```
Writing the traits out manually would be cumbersome, and is not necessary:
```rb
FactoryBot.define do
factory :task do
trait :queued do
status { :queued }
end
trait :started do
status { :started }
end
trait :finished do
status { :finished }
end
end
end
```
If automatically defining traits for enum attributes on every factory is not
desired, it is possible to disable the feature by setting
`FactoryBot.automatically_define_enum_traits = false`
In that case, it is still possible to explicitly define traits for an enum
attribute in a particular factory:
```rb
FactoryBot.automatically_define_enum_traits = false
FactoryBot.define do
factory :task do
traits_for_enum(:status)
end
end
```
It is also possible to use this feature for other enumerable values, not
specifically tied to Active Record enum attributes.
With an array:
```rb
class Task
attr_accessor :status
end
FactoryBot.define do
factory :task do
traits_for_enum(:status, ["queued", "started", "finished"])
end
end
```
Or with a hash:
```rb
class Task
attr_accessor :status
end
FactoryBot.define do
factory :task do
traits_for_enum(:status, { queued: 0, started: 1, finished: 2 })
end
end
```
Callbacks
---------