mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Improve ability to define associations with traits and add documentation
This commit is contained in:
parent
330eed801b
commit
c9ed3d88e5
4 changed files with 75 additions and 6 deletions
|
@ -632,7 +632,45 @@ end
|
||||||
FactoryGirl.create_list(:user, 3, :admin, :male, name: "Jon Snow")
|
FactoryGirl.create_list(:user, 3, :admin, :male, name: "Jon Snow")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Traits can be used with associations easily too:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
factory :user do
|
||||||
|
name "Friendly User"
|
||||||
|
|
||||||
|
trait :admin do
|
||||||
|
admin true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
factory :post do
|
||||||
|
association :user, :admin, name: 'John Doe'
|
||||||
|
end
|
||||||
|
|
||||||
|
# creates an admin user with named "John Doe"
|
||||||
|
FactoryGirl.create(:post).user
|
||||||
|
```
|
||||||
|
|
||||||
|
When you're using association names that're different than the factory:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
factory :user do
|
||||||
|
name "Friendly User"
|
||||||
|
|
||||||
|
trait :admin do
|
||||||
|
admin true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
factory :post do
|
||||||
|
association :author, :admin, factory: :user, name: 'John Doe'
|
||||||
|
# or
|
||||||
|
association :author, factory: [:user, :admin], name: 'John Doe'
|
||||||
|
end
|
||||||
|
|
||||||
|
# creates an admin user with named "John Doe"
|
||||||
|
FactoryGirl.create(:post).user
|
||||||
|
```
|
||||||
Callbacks
|
Callbacks
|
||||||
---------
|
---------
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,11 @@ module FactoryGirl
|
||||||
class Declaration
|
class Declaration
|
||||||
# @api private
|
# @api private
|
||||||
class Association < Declaration
|
class Association < Declaration
|
||||||
def initialize(name, options)
|
def initialize(name, *options)
|
||||||
super(name, false)
|
super(name, false)
|
||||||
@options = options
|
@options = options.dup
|
||||||
|
@overrides = options.extract_options!
|
||||||
|
@traits = options
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
|
@ -18,8 +20,8 @@ module FactoryGirl
|
||||||
private
|
private
|
||||||
|
|
||||||
def build
|
def build
|
||||||
factory_name = @options[:factory] || name
|
factory_name = @overrides[:factory] || name
|
||||||
[Attribute::Association.new(name, factory_name, @options.except(:factory))]
|
[Attribute::Association.new(name, factory_name, [@traits, @overrides.except(:factory)].flatten)]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -138,8 +138,8 @@ module FactoryGirl
|
||||||
# If no name is given, the name of the attribute is assumed to be the
|
# If no name is given, the name of the attribute is assumed to be the
|
||||||
# name of the factory. For example, a "user" association will by
|
# name of the factory. For example, a "user" association will by
|
||||||
# default use the "user" factory.
|
# default use the "user" factory.
|
||||||
def association(name, options = {})
|
def association(name, *options)
|
||||||
@definition.declare_attribute(Declaration::Association.new(name, options))
|
@definition.declare_attribute(Declaration::Association.new(name, *options))
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_create(&block)
|
def to_create(&block)
|
||||||
|
|
|
@ -673,6 +673,15 @@ end
|
||||||
describe "traits used in associations" do
|
describe "traits used in associations" do
|
||||||
before do
|
before do
|
||||||
define_model("User", admin: :boolean, name: :string)
|
define_model("User", admin: :boolean, name: :string)
|
||||||
|
|
||||||
|
define_model("Comment", user_id: :integer) do
|
||||||
|
belongs_to :user
|
||||||
|
end
|
||||||
|
|
||||||
|
define_model("Order", creator_id: :integer) do
|
||||||
|
belongs_to :creator, class_name: 'User'
|
||||||
|
end
|
||||||
|
|
||||||
define_model("Post", author_id: :integer) do
|
define_model("Post", author_id: :integer) do
|
||||||
belongs_to :author, class_name: 'User'
|
belongs_to :author, class_name: 'User'
|
||||||
end
|
end
|
||||||
|
@ -689,6 +698,14 @@ describe "traits used in associations" do
|
||||||
factory :post do
|
factory :post do
|
||||||
association :author, factory: [:user, :admin], name: 'John Doe'
|
association :author, factory: [:user, :admin], name: 'John Doe'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :comment do
|
||||||
|
association :user, :admin, name: 'Joe Slick'
|
||||||
|
end
|
||||||
|
|
||||||
|
factory :order do
|
||||||
|
association :creator, :admin, factory: :user, name: 'Joe Creator'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -697,4 +714,16 @@ describe "traits used in associations" do
|
||||||
author.should be_admin
|
author.should be_admin
|
||||||
author.name.should == 'John Doe'
|
author.name.should == 'John Doe'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "allows inline traits with the default association" do
|
||||||
|
user = FactoryGirl.create(:comment).user
|
||||||
|
user.should be_admin
|
||||||
|
user.name.should == 'Joe Slick'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows inline traits with a specific factory for an association" do
|
||||||
|
creator = FactoryGirl.create(:order).creator
|
||||||
|
creator.should be_admin
|
||||||
|
creator.name.should == 'Joe Creator'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue