Clarify documentation on using initialize_with
Using ignore blocks to avoid duplicate assignment is obsolete since factory_girl 4.0. Closes #622
This commit is contained in:
parent
b97b36a42b
commit
493c9ad031
|
@ -873,20 +873,15 @@ end
|
||||||
sequence(:email) { |n| "person#{n}@example.com" }
|
sequence(:email) { |n| "person#{n}@example.com" }
|
||||||
|
|
||||||
factory :user do
|
factory :user do
|
||||||
ignore do
|
name "Jane Doe"
|
||||||
name "Jane Doe"
|
|
||||||
end
|
|
||||||
|
|
||||||
email
|
email
|
||||||
|
|
||||||
initialize_with { new(name) }
|
initialize_with { new(name) }
|
||||||
end
|
end
|
||||||
|
|
||||||
build(:user).name # Jane Doe
|
build(:user).name # Jane Doe
|
||||||
```
|
```
|
||||||
|
|
||||||
Notice that I ignored the `name` attribute. If you don't want attributes
|
|
||||||
reassigned after your object has been instantiated, you'll want to `ignore` them.
|
|
||||||
|
|
||||||
Although factory_girl is written to work with ActiveRecord out of the box, it
|
Although factory_girl is written to work with ActiveRecord out of the box, it
|
||||||
can also work with any Ruby class. For maximum compatibiltiy with ActiveRecord,
|
can also work with any Ruby class. For maximum compatibiltiy with ActiveRecord,
|
||||||
the default initializer builds all instances by calling `new` on your build class
|
the default initializer builds all instances by calling `new` on your build class
|
||||||
|
@ -908,9 +903,7 @@ For example:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
factory :user do
|
factory :user do
|
||||||
ignore do
|
name "John Doe"
|
||||||
name "John Doe"
|
|
||||||
end
|
|
||||||
|
|
||||||
initialize_with { User.build_with_name(name) }
|
initialize_with { User.build_with_name(name) }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue