Skip to content

Instantly share code, notes, and snippets.

@petervandenabeele
Created December 17, 2011 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petervandenabeele/1491018 to your computer and use it in GitHub Desktop.
Save petervandenabeele/1491018 to your computer and use it in GitHub Desktop.
factory with many belongs_to
The specs:
peterv@ASUS:~/data/backed_up/rails-apps/apps/temp/fg/spec/model$ find . -type f -exec cat \{\} \;
require 'spec_helper'
describe "Client" do
it "builds a client" do
FactoryGirl.build(:client)
end
it "builds a client_x" do
client = FactoryGirl.build(:client_x)
client.last_name.should == 'X'
end
end
require 'spec_helper'
describe "DocumentUpload" do
it "builds a document" do
FactoryGirl.build(:document_upload)
end
it "builds a document_upload with a document" do
du = FactoryGirl.build(:document_upload_with_document)
du.document.should_not be_nil
end
it "builds a document_upload with a document and an order" do
du = FactoryGirl.build(:document_upload_with_document_and_order)
du.document.should_not be_nil
du.document.order.should_not be_nil
end
it "builds a document_upload with a document and an order and a client x" do
du = FactoryGirl.build(:document_upload_with_document_and_order_and_client_x)
du.document.should_not be_nil
du.document.order.should_not be_nil
du.document.order.client.should_not be_nil
du.document.order.client.last_name.should == "X"
du.by_client.should_not be_nil
end
end
require 'spec_helper'
describe "Document" do
it "builds a document" do
FactoryGirl.build(:document)
end
it "builds a document with an order" do
d = FactoryGirl.build(:document_with_order)
d.order.should_not be_nil
end
it "builds a document with an order and a client x" do
d = FactoryGirl.build(:document_with_order_and_client_x)
d.order.should_not be_nil
d.order.client.should_not be_nil
end
end
require 'spec_helper'
describe "Order" do
it "builds an order" do
FactoryGirl.build(:order)
end
it "builds an order with a client x" do
order = FactoryGirl.build(:order_with_client_x)
order.client.should_not be_nil
end
end
The models:
peterv@ASUS:~/data/backed_up/rails-apps/apps/temp/fg/app/models$ find . -type f -exec cat \{\} \;
class Client < ActiveRecord::Base
end
class Order < ActiveRecord::Base
belongs_to :client
has_many :documents
end
class DocumentUpload < ActiveRecord::Base
belongs_to :document
belongs_to :by_client, :class_name => 'Client'
validates_presence_of :by_client_id
end
class Document < ActiveRecord::Base
belongs_to :order
has_many :document_uploads
end
Factories:
peterv@ASUS:~/data/backed_up/rails-apps/apps/temp/fg/test/factories$ find . -type f -exec cat \{\} \;
FactoryGirl.define do
factory :document_upload do
end
factory :document_upload_with_document, :parent => :document_upload do |du|
du.association :document
end
factory :document_upload_with_document_and_order, :parent => :document_upload do |du|
du.association :document, :factory => :document_with_order
end
factory :document_upload_with_document_and_order_and_client_x, :parent => :document_upload do |du|
du.association :document, :factory => :document_with_order_and_client_x
by_client {document.order.client}
end
=begin
factory :document_with_order, :parent => :document do |d|
d.association :order
end
factory :document_with_order_and_client, :parent => :document do |d|
d.association :order, :factory => :order_with_client
end
factory :document_with_order_and_client_x, :parent => :document do |d|
d.association :order, :fa
it "builds a document_upload with a document and an order and a client" do
du = FactoryGirl.build(:document_upload_with_document_and_order_and_client)
du.document.should_not be_nil
du.document.order.should_not be_nil
du.document.order.client.should_not be_nil
du.by_client.should_not be_nil
end
=end
=begin
factory :document_uploaded, :class => Document do |d|
#d.association :order, :factory => :order_uploaded, :method => :build
ignore do
order nil
end
#d.after_build do |doc|
# doc.uploads = FactoryGirl.build_list(:document_upload_from_client_x, 1, :document => doc )
#end
# Error on the following line: NoMethodError: undefined method `client' for nil:NilClass
d.uploads {|u| [u.association(:document_upload_from_client_x, :document => d, :by_client_id => u.order.client.id)]}
end
factory :document_upload_from_client_x, :class => DocumentUpload do |du|
du.association :document
#Error on the following line: NoMethodError: undefined method `client' for #<FactoryGirl::Declaration::Implicit:0x106499e48>
after_build { |obj| obj.by_client_id = obj.document.order.client.id }
end
=end
end
FactoryGirl.define do
factory :client do
end
factory :client_x, :parent => :client do
last_name "X"
end
end
# Read about factories at http://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :order do
end
factory :order_with_client_x, :parent => :order do |order|
order.association :client, :factory => :client_x
end
factory :order_uploaded, :class => Order do |o|
o.association :client, :factory => :client_x
o.last_name { client.last_name }
o.after_build do |order|
order.documents = FactoryGirl.build_list(:document_uploaded, 1)
end
end
end
=begin
factory :document_uploaded, :class => Document do |d|
#d.association :order, :factory => :order_uploaded, :method => :build
ignore do
order nil
end
#d.after_build do |doc|
# doc.uploads = FactoryGirl.build_list(:document_upload_from_client_x, 1, :document => doc )
#end
# Error on the following line: NoMethodError: undefined method `client' for nil:NilClass
d.uploads {|u| [u.association(:document_upload_from_client_x, :document => d, :by_client_id => u.order.client.id)]}
end
factory :document_upload_from_client_x, :class => DocumentUpload do |du|
du.association :document
#Error on the following line: NoMethodError: undefined method `client' for #<FactoryGirl::Declaration::Implicit:0x106499e48>
after_build { |obj| obj.by_client_id = obj.document.order.client.id }
end
end
=end
FactoryGirl.define do
factory :document do |d|
end
factory :document_with_order, :parent => :document do |d|
d.association :order
end
factory :document_with_order_and_client_x, :parent => :document do |d|
d.association :order, :factory => :order_with_client_x
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment