2009-09-15 15:47:47 -04:00
|
|
|
module FactoryGirlStepHelpers
|
2009-09-15 16:56:20 -04:00
|
|
|
def convert_association_string_to_instance(factory_name, assignment)
|
|
|
|
attribute, value = assignment.split(':', 2)
|
|
|
|
attributes = convert_human_hash_to_attribute_hash(attribute => value.strip)
|
|
|
|
factory = Factory.factory_by_name(factory_name)
|
|
|
|
model_class = factory.build_class
|
|
|
|
model_class.find(:first, :conditions => attributes) or
|
|
|
|
Factory(factory_name, attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_human_hash_to_attribute_hash(human_hash, associations = [])
|
2009-09-15 15:54:21 -04:00
|
|
|
human_hash.inject({}) do |attribute_hash, (human_key, value)|
|
|
|
|
key = human_key.downcase.gsub(' ', '_').to_sym
|
2009-09-15 16:56:20 -04:00
|
|
|
if association = associations.detect {|association| association.name == key }
|
|
|
|
value = convert_association_string_to_instance(association.factory, value)
|
|
|
|
end
|
2009-09-15 15:54:21 -04:00
|
|
|
attribute_hash.merge(key => value)
|
|
|
|
end
|
|
|
|
end
|
2009-09-15 15:47:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
World(FactoryGirlStepHelpers)
|
|
|
|
|
|
|
|
Factory.factories.values.each do |factory|
|
2009-09-15 15:56:54 -04:00
|
|
|
# TODO: support irregular pluralizations
|
2009-09-15 17:38:40 -04:00
|
|
|
Given /^the following #{factory.human_name}s? exists?:$/ do |table|
|
2009-09-15 15:54:21 -04:00
|
|
|
table.hashes.each do |human_hash|
|
2009-09-15 16:56:20 -04:00
|
|
|
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
|
2009-09-15 15:54:21 -04:00
|
|
|
Factory.create(factory.factory_name, attributes)
|
|
|
|
end
|
|
|
|
end
|
2009-09-15 17:58:23 -04:00
|
|
|
|
2009-09-15 18:06:24 -04:00
|
|
|
Given /^an? #{factory.human_name} exists$/ do
|
|
|
|
Factory(factory.factory_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
Given /^(\d+) #{factory.human_name}s exist$/ do |count|
|
|
|
|
count.to_i.times { Factory(factory.human_name) }
|
|
|
|
end
|
|
|
|
|
2009-09-15 17:58:23 -04:00
|
|
|
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|
|
2009-09-15 18:11:16 -04:00
|
|
|
Factory(factory.factory_name, column.name => value)
|
2009-09-15 17:58:23 -04:00
|
|
|
end
|
2009-09-15 18:06:24 -04:00
|
|
|
|
|
|
|
Given /^(\d+) #{factory.human_name}s exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
|
2009-09-15 18:11:16 -04:00
|
|
|
count.to_i.times { Factory(factory.factory_name, column.name => value) }
|
2009-09-15 18:06:24 -04:00
|
|
|
end
|
2009-09-15 17:58:23 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-15 15:47:47 -04:00
|
|
|
end
|
|
|
|
|