2012-06-22 13:51:33 -04:00
require 'active_support/deprecation'
2012-05-05 02:31:31 -04:00
# @api private
2009-09-15 15:47:47 -04:00
module FactoryGirlStepHelpers
2011-06-30 11:00:25 -04:00
def convert_human_hash_to_attribute_hash ( human_hash , associations = [ ] )
HumanHashToAttributeHash . new ( human_hash , associations ) . attributes
2009-09-15 16:56:20 -04:00
end
2011-06-30 11:00:25 -04:00
class HumanHashToAttributeHash
attr_reader :associations
def initialize ( human_hash , associations )
@human_hash = human_hash
@associations = associations
end
def attributes ( strategy = CreateAttributes )
@human_hash . inject ( { } ) do | attribute_hash , ( human_key , value ) |
attributes = strategy . new ( self , * process_key_value ( human_key , value ) )
attribute_hash . merge ( { attributes . key = > attributes . value } )
end
end
private
def process_key_value ( key , value )
2011-08-22 13:16:32 -04:00
value = value . strip if value . is_a? ( String )
[ key . downcase . gsub ( ' ' , '_' ) . to_sym , value ]
2011-06-30 11:00:25 -04:00
end
class AssociationManager
def initialize ( human_hash_to_attributes_hash , key , value )
@human_hash_to_attributes_hash = human_hash_to_attributes_hash
@key = key
@value = value
end
def association
@human_hash_to_attributes_hash . associations . detect { | association | association . name == @key }
end
def association_instance
return unless association
if attributes_hash = nested_attribute_hash
2012-03-09 17:20:38 -05:00
factory . build_class . first ( conditions : attributes_hash . attributes ( FindAttributes ) ) or
2011-06-30 11:00:25 -04:00
FactoryGirl . create ( association . factory , attributes_hash . attributes )
end
end
private
def factory
FactoryGirl . factory_by_name ( association . factory )
end
def nested_attribute_hash
attribute , value = @value . split ( ':' , 2 )
return if value . blank?
HumanHashToAttributeHash . new ( { attribute = > value } , factory . associations )
end
end
class AttributeStrategy
attr_reader :key , :value , :association_manager
def initialize ( human_hash_to_attributes_hash , key , value )
@association_manager = AssociationManager . new ( human_hash_to_attributes_hash , key , value )
@key = key
@value = value
end
end
class FindAttributes < AttributeStrategy
def initialize ( human_hash_to_attributes_hash , key , value )
super
if association_manager . association
@key = " #{ @key } _id "
@value = association_manager . association_instance . try ( :id )
end
end
end
class CreateAttributes < AttributeStrategy
def initialize ( human_hash_to_attributes_hash , key , value )
super
if association_manager . association
@value = association_manager . association_instance
end
end
2009-09-15 15:54:21 -04:00
end
end
2009-09-15 15:47:47 -04:00
end
World ( FactoryGirlStepHelpers )
2011-05-19 10:56:45 -04:00
FactoryGirl . factories . each do | factory |
2011-10-28 10:59:49 -04:00
factory . compile
2011-06-30 18:27:25 -04:00
factory . human_names . each do | human_name |
2012-03-30 11:47:46 -04:00
attribute_names_for_model = if factory . build_class . respond_to? ( :attribute_names )
factory . build_class . attribute_names
elsif factory . build_class . respond_to? ( :columns )
factory . build_class . columns . map do | column |
column . respond_to? ( :name ) ? column . name : column . to_s
end
else
[ ]
end
2012-03-08 12:18:40 -06:00
Given / ^the following (?: #{ human_name } | #{ human_name . pluralize } ) exists?:?$ /i do | table |
2012-06-22 13:51:33 -04:00
ActiveSupport :: Deprecation . warn %{ The step 'Given the following #{ human_name } exists:' is deprecated and will be removed in 4.0 }
2011-06-30 18:27:25 -04:00
table . hashes . each do | human_hash |
attributes = convert_human_hash_to_attribute_hash ( human_hash , factory . associations )
FactoryGirl . create ( factory . name , attributes )
end
2009-09-15 15:54:21 -04:00
end
2009-09-15 17:58:23 -04:00
2011-06-30 18:27:25 -04:00
Given / ^an? #{ human_name } exists$ /i do
2012-06-22 13:51:33 -04:00
ActiveSupport :: Deprecation . warn %{ The step 'Given a #{ human_name } exists' is deprecated and will be removed in 4.0 }
2011-06-30 18:27:25 -04:00
FactoryGirl . create ( factory . name )
end
2009-09-15 18:06:24 -04:00
2011-06-30 18:27:25 -04:00
Given / ^( \ d+) #{ human_name . pluralize } exist$ /i do | count |
2012-06-22 13:51:33 -04:00
ActiveSupport :: Deprecation . warn %{ The step 'Given #{ count } #{ human_name . pluralize } exist' is deprecated and will be removed in 4.0 }
2011-10-10 19:22:14 +03:00
FactoryGirl . create_list ( factory . name , count . to_i )
2011-06-30 18:27:25 -04:00
end
2009-09-15 18:06:24 -04:00
2012-03-30 11:47:46 -04:00
attribute_names_for_model . each do | attribute_name |
2012-03-11 23:27:14 -05:00
human_column_name = attribute_name . downcase . gsub ( '_' , ' ' )
2012-03-30 11:47:46 -04:00
2012-03-11 23:27:14 -05:00
Given / ^an? #{ human_name } exists with an? #{ human_column_name } of "([^"]*)"$ /i do | value |
2012-06-22 13:51:33 -04:00
ActiveSupport :: Deprecation . warn %{ The step 'Given a #{ human_name } exists with a #{ human_column_name } of " #{ value } "' is deprecated and will be removed in 4.0 }
2012-03-11 23:27:14 -05:00
FactoryGirl . create ( factory . name , attribute_name = > value )
end
Given / ^( \ d+) #{ human_name . pluralize } exist with an? #{ human_column_name } of "([^"]*)"$ /i do | count , value |
2012-06-22 13:51:33 -04:00
ActiveSupport :: Deprecation . warn %{ The step 'Given #{ count } #{ human_name . pluralize } exists with a #{ human_column_name } of " #{ value } "' is deprecated and will be removed in 4.0 }
2012-03-11 23:27:14 -05:00
FactoryGirl . create_list ( factory . name , count . to_i , attribute_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
2010-10-28 14:20:24 -02:00