<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>cucumber.yml</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -18,3 +18,25 @@ Feature: Use step definitions generated by factories
       | title | three |
       | body  | third |
     And there should be 3 posts
+
+  Scenario: create a post with a new author
+    Given the following post exists:
+      | Title  | a title |
+      | Author | ID: 123  |
+    Then I should find the following for the last post:
+      | title     | a title   |
+      | author_id | 123       |
+    And I should find the following for the last user:
+      | id | 123 |
+
+  Scenario: create a post with an existing author
+    Given the following user exists:
+      | ID   | 123 |
+      | Name | Joe |
+    And the following post exists:
+      | Title  | a title |
+      | Author | Name: Joe |
+    Then I should find the following for the last post:
+      | title     | a title   |
+      | author_id | 123       |
+    And there should be 1 user</diff>
      <filename>features/factory_girl_steps.feature</filename>
    </modified>
    <modified>
      <diff>@@ -1,15 +1,18 @@
-Then /^I should find the following for the last post:$/ do |table|
-  last_post = Post.last or raise &quot;No posts exist&quot;
-  attributes = table.rows.inject({}) {|res, (key, value)| res.merge(key =&gt; value) }
+Then /^I should find the following for the last (.*):$/ do |model, table|
+  model_class = model.camelize.constantize
+  last_instance = model_class.last or raise &quot;No #{model.pluralize} exist&quot;
+  attributes = table.raw.inject({}) {|res, (key, value)| res.merge(key =&gt; value) }
   attributes.each do |key, value|
-    last_post.attributes[key].should == value
+    last_instance.attributes[key].to_s.should == value
   end
 end
 
-Then /^there should be (\d+) posts$/ do |count|
-  Post.count.should == count.to_i
+Then /^there should be (\d+) (.*)$/ do |count, model|
+  model_class = model.singularize.camelize.constantize
+  model_class.count.should == count.to_i
 end
 
 Before do
   Post.delete_all
+  User.delete_all
 end</diff>
      <filename>features/step_definitions/database_steps.rb</filename>
    </modified>
    <modified>
      <diff>@@ -6,18 +6,31 @@ ActiveRecord::Base.establish_connection(
 class CreateSchema &lt; ActiveRecord::Migration
   def self.up
     create_table :posts, :force =&gt; true do |t|
+      t.integer :author_id
       t.string  :title
       t.string  :body
     end
+
+    create_table :users, :force =&gt; true do |t|
+      t.string  :name
+    end
   end
 end
 
 CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
 
+class User &lt; ActiveRecord::Base
+end
+
 class Post &lt; ActiveRecord::Base
+  belongs_to :author, :class_name =&gt; 'User'
+end
+
+Factory.define :user do |f|
 end
 
 Factory.define :post do |f|
+  f.association :author, :factory =&gt; :user
 end
 
 require 'factory_girl/step_definitions'</diff>
      <filename>features/support/factories.rb</filename>
    </modified>
    <modified>
      <diff>@@ -3,6 +3,8 @@ class Factory
 
     class Association &lt; Attribute  #:nodoc:
 
+      attr_reader :factory
+
       def initialize(name, factory, overrides)
         super(name)
         @factory   = factory</diff>
      <filename>lib/factory_girl/attribute/association.rb</filename>
    </modified>
    <modified>
      <diff>@@ -305,6 +305,10 @@ class Factory
     end
   end
 
+  def associations
+    attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
+  end
+
   private
 
   def class_for (class_or_to_s)</diff>
      <filename>lib/factory_girl/factory.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,14 +1,25 @@
 module FactoryGirlStepHelpers
-  def convert_ast_table_to_attribute_hash(table)
-    table.rows.inject({}) do |result, (human_key, value)|
-      key = human_key.downcase.gsub(' ', '_').to_sym
+  def convert_vertical_table_to_hash(table)
+    table.raw.inject({}) do |result, (key, value)|
       result.merge(key =&gt; value)
     end
   end
 
-  def convert_human_hash_to_attribute_hash(human_hash)
+  def convert_association_string_to_instance(factory_name, assignment)
+    attribute, value = assignment.split(':', 2)
+    attributes = convert_human_hash_to_attribute_hash(attribute =&gt; value.strip)
+    factory = Factory.factory_by_name(factory_name)
+    model_class = factory.build_class
+    model_class.find(:first, :conditions =&gt; attributes) or
+      Factory(factory_name, attributes)
+  end
+
+  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 =&gt; value)
     end
   end
@@ -18,14 +29,15 @@ World(FactoryGirlStepHelpers)
 
 Factory.factories.values.each do |factory|
   Given &quot;the following #{factory.human_name} exists:&quot; do |table|
-    attributes = convert_ast_table_to_attribute_hash(table)
+    human_hash = convert_vertical_table_to_hash(table)
+    attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
     Factory.create(factory.factory_name, attributes)
   end
 
   # TODO: support irregular pluralizations
   Given &quot;the following #{factory.human_name}s exist:&quot; do |table|
     table.hashes.each do |human_hash|
-      attributes = convert_human_hash_to_attribute_hash(human_hash)
+      attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
       Factory.create(factory.factory_name, attributes)
     end
   end</diff>
      <filename>lib/factory_girl/step_definitions.rb</filename>
    </modified>
    <modified>
      <diff>@@ -12,6 +12,10 @@ describe Factory::Attribute::Association do
     @attr.name.should == @name
   end
 
+  it &quot;should have a factory&quot; do
+    @attr.factory.should == @factory
+  end
+
   it &quot;should tell the proxy to associate when being added to a proxy&quot; do
     proxy = &quot;proxy&quot;
     stub(proxy).associate</diff>
      <filename>spec/factory_girl/attribute/association_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -134,6 +134,16 @@ describe Factory do
       factory.attributes.should include(attr)
     end
 
+    it &quot;should return associations&quot; do
+      factory = Factory.new(:post)
+      factory.association(:author)
+      factory.association(:editor)
+      factory.associations.each do |association|
+        association.should be_a(Factory::Attribute::Association)
+      end
+      factory.associations.size.should == 2
+    end
+
     it &quot;should add an association with overrides&quot; do
       factory   = Factory.new(:post)
       name      = :user</diff>
      <filename>spec/factory_girl/factory_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>15f36c8fc97b0930be37c89fbaf27665bd223d6d</id>
    </parent>
  </parents>
  <author>
    <name>Joe Ferris</name>
    <email>jferris@metaphor.local</email>
  </author>
  <url>http://github.com/thoughtbot/factory_girl/commit/33b61c3bfbf5e330e92329dc1bb9d5ef49332773</url>
  <id>33b61c3bfbf5e330e92329dc1bb9d5ef49332773</id>
  <committed-date>2009-09-15T13:56:20-07:00</committed-date>
  <authored-date>2009-09-15T13:56:20-07:00</authored-date>
  <message>Fixed issues with some attributes being skipped and added support for linked associations in step definitions</message>
  <tree>33f752c8c31b1a8bdc22fdbb5f5364d779880355</tree>
  <committer>
    <name>Joe Ferris</name>
    <email>jferris@metaphor.local</email>
  </committer>
</commit>
