Renamed Factory#factory_name to Factory#name; cleaned up Factory#human_name; bumped version

This commit is contained in:
Joe Ferris 2010-10-02 00:00:58 -04:00
parent 0388e8530f
commit 5c88bb058b
7 changed files with 43 additions and 29 deletions

View File

@ -1,5 +1,5 @@
$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
require 'factory_girl'
require 'factory_girl/version'
Gem::Specification.new do |s|
s.name = %q{factory_girl}
s.version = FactoryGirl::VERSION

View File

@ -16,10 +16,7 @@ require 'factory_girl/syntax/default'
require 'factory_girl/syntax/vintage'
require 'factory_girl/find_definitions'
require 'factory_girl/deprecated'
module FactoryGirl
VERSION = "1.3.1"
end
require 'factory_girl/version'
if defined?(Rails) && Rails::VERSION::MAJOR == 2
require 'factory_girl/rails2'

View File

@ -10,7 +10,7 @@ module FactoryGirl
end
def self.register_factory(factory, options = {})
name = options[:as] || factory.factory_name
name = options[:as] || factory.name
if self.factories[name]
raise DuplicateDefinitionError, "Factory already defined: #{name}"
end
@ -30,11 +30,16 @@ module FactoryGirl
end
class Factory
attr_reader :factory_name #:nodoc:
attr_reader :name #:nodoc:
attr_reader :attributes #:nodoc:
def factory_name
puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
name
end
def class_name #:nodoc:
@options[:class] || factory_name
@options[:class] || name
end
def build_class #:nodoc:
@ -45,11 +50,11 @@ module FactoryGirl
@options[:default_strategy] || :create
end
def initialize (name, options = {}) #:nodoc:
def initialize(name, options = {}) #:nodoc:
assert_valid_options(options)
@factory_name = factory_name_for(name)
@options = options
@attributes = []
@name = factory_name_for(name)
@options = options
@attributes = []
end
def inherit_from(parent) #:nodoc:
@ -71,8 +76,8 @@ module FactoryGirl
if attribute_defined?(name)
raise AttributeDefinitionError, "Attribute already defined: #{name}"
end
if attribute.respond_to?(:factory) && attribute.factory == self.factory_name
raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.factory_name}'"
if attribute.respond_to?(:factory) && attribute.factory == self.name
raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.name}'"
end
@attributes << attribute
end
@ -98,11 +103,7 @@ module FactoryGirl
end
def human_name(*args, &block)
if args.size == 0 && block.nil?
factory_name.to_s.gsub('_', ' ')
else
add_attribute(:human_name, *args, &block)
end
name.to_s.gsub('_', ' ')
end
def associations
@ -122,7 +123,7 @@ module FactoryGirl
end
end
def factory_name_for (class_or_to_s)
def factory_name_for(class_or_to_s)
if class_or_to_s.respond_to?(:to_sym)
class_or_to_s.to_sym
else

View File

@ -31,22 +31,22 @@ FactoryGirl.factories.values.each do |factory|
end
Given /^an? #{factory.human_name} exists$/ do
Factory(factory.factory_name)
Factory(factory.name)
end
Given /^(\d+) #{factory.human_name.pluralize} exist$/ do |count|
count.to_i.times { Factory(factory.factory_name) }
count.to_i.times { Factory(factory.name) }
end
if factory.build_class.respond_to?(:columns)
factory.build_class.columns.each do |column|
human_column_name = column.name.downcase.gsub('_', ' ')
Given /^an? #{factory.human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
Factory(factory.factory_name, column.name => value)
Factory(factory.name, column.name => value)
end
Given /^(\d+) #{factory.human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
count.to_i.times { Factory(factory.factory_name, column.name => value) }
count.to_i.times { Factory(factory.name, column.name => value) }
end
end
end

View File

@ -0,0 +1,4 @@
module FactoryGirl
VERSION = "2.0.0.beta1"
end

View File

@ -52,7 +52,7 @@ describe "defining a factory" do
@name = :user
@factory = "factory"
@proxy = "proxy"
stub(@factory).factory_name { @name }
stub(@factory).name { @name }
@options = { :class => 'magic' }
stub(FactoryGirl::Factory).new { @factory }
stub(FactoryGirl::DefinitionProxy).new { @proxy }

View File

@ -4,7 +4,7 @@ describe FactoryGirl::Factory, "registering a factory" do
before do
@name = :user
@factory = "factory"
stub(@factory).factory_name { @name }
stub(@factory).name { @name }
end
it "should add the factory to the list of factories" do
@ -29,6 +29,10 @@ describe FactoryGirl::Factory do
end
it "should have a factory name" do
@factory.name.should == @name
end
it "responds to factory_name" do
@factory.factory_name.should == @name
end
@ -238,7 +242,7 @@ describe FactoryGirl::Factory, "when defined with a class instead of a name" do
end
it "should guess the name from the class" do
@factory.factory_name.should == @name
@factory.name.should == @name
end
it "should use the class as the build class" do
@ -268,7 +272,7 @@ describe FactoryGirl::Factory, "with a name ending in s" do
end
it "should have a factory name" do
@factory.factory_name.should == @name
@factory.name.should == @name
end
it "should have a build class" do
@ -283,7 +287,7 @@ describe FactoryGirl::Factory, "with a string for a name" do
end
it "should convert the string to a symbol" do
@factory.factory_name.should == @name
@factory.name.should == @name
end
end
@ -380,3 +384,11 @@ describe FactoryGirl::Factory do
end
describe FactoryGirl::Factory, "with an underscore in the name" do
subject { FactoryGirl::Factory.new("happy_users") }
it "has a human name" do
subject.human_name.should == 'happy users'
end
end