Module: FactoryGirlStepHelpers

Defined in:
lib/factory_girl/step_definitions.rb

Instance Method Summary collapse

Instance Method Details

#convert_association_string_to_instance(factory_name, assignment) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/factory_girl/step_definitions.rb', line 2

def convert_association_string_to_instance(factory_name, assignment)
  attribute, value = assignment.split(':', 2)
  return if value.blank?
  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

#convert_human_hash_to_attribute_hash(human_hash, associations = []) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/factory_girl/step_definitions.rb', line 12

def convert_human_hash_to_attribute_hash(human_hash, associations = [])
  human_hash.inject({}) do |attribute_hash, (human_key, value)|
    key = human_key.downcase.gsub(' ', '_').to_sym
    if association = associations.detect {|association| association.name == key }
      value = convert_association_string_to_instance(association.factory, value)
    end
    attribute_hash.merge(key => value)
  end
end