Skip to content

Commit

Permalink
Split factories/sequences into separate registries most of the time;
Browse files Browse the repository at this point in the history
Only use factories from step definitions
  • Loading branch information
jferris committed May 26, 2011
1 parent 91d8614 commit c59981d
Show file tree
Hide file tree
Showing 44 changed files with 354 additions and 174 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end
namespace :spec do
desc "Run unit specs"
RSpec::Core::RakeTask.new('unit') do |t|
t.pattern = 'spec/factory_girl/**/*_spec.rb'
t.pattern = 'spec/{*_spec.rb,factory_girl/**/*_spec.rb}'
end

desc "Run acceptance specs"
Expand Down
43 changes: 25 additions & 18 deletions features/support/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,36 @@ class Post < ActiveRecord::Base
class NonActiveRecord
end

Factory.define :user do |f|
end
FactoryGirl.define do
# To make sure the step defs work with an email
sequence :email do |n|
"email#{n}@example.com"
end

Factory.define :admin_user, :parent => :user do |f|
f.admin true
end
factory :user do
end

Factory.define :category do |f|
f.name "programming"
f.association :category_group
end
factory :admin_user, :parent => :user do
admin true
end

Factory.define :category_group do |f|
f.name "tecnhology"
end
factory :category do
name "programming"
category_group
end

Factory.define :post do |f|
f.association :author, :factory => :user
f.association :category
end
factory :category_group do
name "tecnhology"
end

# This is here to ensure that factory step definitions don't raise for a non-AR factory
Factory.define :non_active_record do |f|
factory :post do
association :author, :factory => :user
category
end

# This is here to ensure that factory step definitions don't raise for a non-AR factory
factory :non_active_record do
end
end

require 'factory_girl/step_definitions'
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/2.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ gem "rr"
gem "activerecord", "~> 2.1"
gem "rake"
gem "sqlite3-ruby", {:require=>false}
gem "bluecloth"
gem "rspec", "~> 2.0"
gem "yard"
gem "rcov"
gem "cucumber"
gem "appraisal"
4 changes: 4 additions & 0 deletions gemfiles/2.1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ GEM
appraisal (0.1)
bundler
rake
bluecloth (2.1.0)
builder (2.1.2)
cucumber (0.9.4)
builder (~> 2.1.2)
Expand All @@ -32,16 +33,19 @@ GEM
rspec-mocks (2.1.0)
sqlite3-ruby (1.3.2)
term-ansicolor (1.0.5)
yard (0.7.1)

PLATFORMS
ruby

DEPENDENCIES
activerecord (~> 2.1)
appraisal
bluecloth
cucumber
rake
rcov
rr
rspec (~> 2.0)
sqlite3-ruby
yard
2 changes: 2 additions & 0 deletions gemfiles/2.3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ gem "rr"
gem "activerecord", "~> 2.3"
gem "rake"
gem "sqlite3-ruby", {:require=>false}
gem "bluecloth"
gem "rspec", "~> 2.0"
gem "yard"
gem "rcov"
gem "cucumber"
gem "appraisal"
4 changes: 4 additions & 0 deletions gemfiles/2.3.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ GEM
appraisal (0.1)
bundler
rake
bluecloth (2.1.0)
builder (2.1.2)
cucumber (0.9.4)
builder (~> 2.1.2)
Expand All @@ -32,16 +33,19 @@ GEM
rspec-mocks (2.1.0)
sqlite3-ruby (1.3.2)
term-ansicolor (1.0.5)
yard (0.7.1)

PLATFORMS
ruby

DEPENDENCIES
activerecord (~> 2.3)
appraisal
bluecloth
cucumber
rake
rcov
rr
rspec (~> 2.0)
sqlite3-ruby
yard
2 changes: 2 additions & 0 deletions gemfiles/3.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ gem "rr"
gem "activerecord", "~> 3.0"
gem "rake"
gem "sqlite3-ruby", {:require=>false}
gem "bluecloth"
gem "rspec", "~> 2.0"
gem "yard"
gem "rcov"
gem "cucumber"
gem "appraisal"
4 changes: 4 additions & 0 deletions gemfiles/3.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ GEM
rake
arel (1.0.1)
activesupport (~> 3.0.0)
bluecloth (2.1.0)
builder (2.1.2)
cucumber (0.9.4)
builder (~> 2.1.2)
Expand Down Expand Up @@ -43,16 +44,19 @@ GEM
sqlite3-ruby (1.3.2)
term-ansicolor (1.0.5)
tzinfo (0.3.23)
yard (0.7.1)

PLATFORMS
ruby

DEPENDENCIES
activerecord (~> 3.0)
appraisal
bluecloth
cucumber
rake
rcov
rr
rspec (~> 2.0)
sqlite3-ruby
yard
27 changes: 27 additions & 0 deletions lib/factory_girl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
require 'factory_girl/attribute/dynamic'
require 'factory_girl/attribute/association'
require 'factory_girl/attribute/callback'
require 'factory_girl/attribute/sequence'
require 'factory_girl/attribute/implicit'
require 'factory_girl/sequence'
require 'factory_girl/aliases'
require 'factory_girl/definition_proxy'
Expand All @@ -24,3 +26,28 @@
require 'factory_girl/rails2'
end

module FactoryGirl
def self.factories
@factories ||= Registry.new
end

def self.register_factory(factory)
factories.add(factory)
end

def self.factory_by_name(name)
factories.find(name)
end

def self.sequences
@sequences ||= Registry.new
end

def self.register_sequence(sequence)
sequences.add(sequence)
end

def self.sequence_by_name(name)
sequences.find(name)
end
end
4 changes: 4 additions & 0 deletions lib/factory_girl/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def initialize(name)

def add_to(proxy)
end

def association?
false
end
end

end
4 changes: 4 additions & 0 deletions lib/factory_girl/attribute/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def initialize(name, factory, overrides)
def add_to(proxy)
proxy.associate(name, @factory, @overrides)
end

def association?
true
end
end

end
Expand Down
36 changes: 36 additions & 0 deletions lib/factory_girl/attribute/implicit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module FactoryGirl
class Attribute

class Implicit < Attribute
def initialize(name)
super(name)
end

def add_to(proxy)
implementation.add_to(proxy)
end

def association?
implementation.association?
end

def factory
name
end

private

def implementation
@implementation ||= resolve_name
end

def resolve_name
if FactoryGirl.factories.registered?(name)
Attribute::Association.new(name, name, {})
else
Attribute::Sequence.new(name, name)
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/factory_girl/attribute/sequence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module FactoryGirl
class Attribute

class Sequence < Attribute
def initialize(name, sequence)
super(name)
@sequence = sequence
end

def add_to(proxy)
proxy.set(name, FactoryGirl.generate(@sequence))
end
end

end
end
4 changes: 2 additions & 2 deletions lib/factory_girl/definition_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def add_attribute(name, value = nil, &block)
# are equivilent.
def method_missing(name, *args, &block)
if args.empty? && block.nil?
association(name)
@factory.define_attribute(Attribute::Implicit.new(name))
else
add_attribute(name, *args, &block)
end
Expand All @@ -97,7 +97,7 @@ def method_missing(name, *args, &block)
# Except that no globally available sequence will be defined.
def sequence(name, start_value = 1, &block)
sequence = Sequence.new(name, start_value, &block)
add_attribute(name) { sequence.run }
add_attribute(name) { sequence.next }
end

# Adds an attribute that builds an association. The associated instance will
Expand Down
14 changes: 1 addition & 13 deletions lib/factory_girl/factory.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
module FactoryGirl
def self.factories
puts "WARNING: FactoryGirl.factories is deprecated."
puts "Use FactoryGirl.registry instead."
registry
end

def self.factory_by_name(name)
puts "WARNING: FactoryGirl.factory_by_name is deprecated."
puts "Use FactoryGirl.find instead."
registry.find(name)
end

# Raised when a factory is defined that attempts to instantiate itself.
class AssociationDefinitionError < RuntimeError
end
Expand Down Expand Up @@ -101,7 +89,7 @@ def human_name(*args, &block)
end

def associations
attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
attributes.select {|attribute| attribute.association? }
end

# Names for this factory, including aliases.
Expand Down
2 changes: 0 additions & 2 deletions lib/factory_girl/find_definitions.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module FactoryGirl

class << self
attr_accessor :factories #:nodoc:

# An Array of strings specifying locations that should be searched for
# factory definitions. By default, factory_girl will attempt to require
# "factories," "test/factories," and "spec/factories." Only the first
Expand Down
4 changes: 2 additions & 2 deletions lib/factory_girl/proxy/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def set(attribute, value)
end

def associate(name, factory_name, overrides)
factory = FactoryGirl.find(factory_name)
factory = FactoryGirl.factory_by_name(factory_name)
set(name, factory.run(Proxy::Create, overrides))
end

def association(factory_name, overrides = {})
factory = FactoryGirl.find(factory_name)
factory = FactoryGirl.factory_by_name(factory_name)
factory.run(Proxy::Create, overrides)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/factory_girl/proxy/stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def set(attribute, value)
end

def associate(name, factory_name, overrides)
factory = FactoryGirl.find(factory_name)
factory = FactoryGirl.factory_by_name(factory_name)
set(name, factory.run(Proxy::Stub, overrides))
end

def association(factory_name, overrides = {})
factory = FactoryGirl.find(factory_name)
factory = FactoryGirl.factory_by_name(factory_name)
factory.run(Proxy::Stub, overrides)
end

Expand Down
Loading

0 comments on commit c59981d

Please sign in to comment.