<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -36,7 +36,7 @@ module ModelStubbing
     name    ||= is_a?(Class) ? self : :default
     base_name = options[:copy] || :default
     base      = name == base_name ? nil : ModelStubbing.definitions[base_name]
-    defn      = ModelStubbing.definitions[name] ||= (base &amp;&amp; options[:copy] != false) ? base.dup : ModelStubbing::Definition.new
+    defn      = ModelStubbing.definitions[name] ||= (base &amp;&amp; options[:copy] != false) ? base.dup : ModelStubbing::Definition.new(name)
     options   = base.options.merge(options) if base
     defn.setup_on options[:target] || self, options, &amp;block
   end</diff>
      <filename>lib/model_stubbing.rb</filename>
    </modified>
    <modified>
      <diff>@@ -4,7 +4,7 @@ module ModelStubbing
   # rspec example.
   class Definition
     attr_writer :insert, :current_time
-    attr_reader :models, :stubs, :ordered_models, :options
+    attr_reader :models, :stubs, :ordered_models, :options, :name
 
     # Sets the time that Time.now is mocked to (in UTC)
     def time(*args)
@@ -33,7 +33,8 @@ module ModelStubbing
       @models[m.name]
     end
     
-    def initialize(&amp;block)
+    def initialize(name = nil, &amp;block)
+      @name           = name
       @ordered_models = []
       @models         = {}
       @stubs          = {}
@@ -114,17 +115,11 @@ module ModelStubbing
     def setup_test_run
       ModelStubbing.records.clear
       ModelStubbing.stub_current_time_with(current_time) if current_time
-      return unless database?
-      ActiveRecord::Base.connection.increment_open_transactions
-      ActiveRecord::Base.connection.begin_db_transaction
     end
     
     def teardown_test_run
       ModelStubbing.records.clear
       # TODO: teardown Time.stubs(:now)
-      return unless database?
-      ActiveRecord::Base.connection.rollback_db_transaction
-      ActiveRecord::Base.verify_active_connections!
     end
     
     def database?
@@ -135,4 +130,4 @@ module ModelStubbing
       &quot;(ModelStubbing::Definition(:models =&gt; [#{@models.keys.collect { |k| k.to_s }.sort.join(&quot;, &quot;)}]))&quot;
     end
   end
-end
\ No newline at end of file
+end</diff>
      <filename>lib/model_stubbing/definition.rb</filename>
    </modified>
    <modified>
      <diff>@@ -23,18 +23,33 @@ module ModelStubbing
       end
     end
 
+    def database?
+      defined?(ActiveRecord)
+    end
+    
     def setup_fixtures
       ModelStubbing.records.clear
-      return unless self.class.definition
-      unless self.class.definition_inserted
-        self.class.definition.insert!
-        self.class.definition_inserted = true
+      if self.class.definition
+        unless self.class.definition_inserted
+          self.class.definition.insert!
+          self.class.definition_inserted = true
+        end
+        self.class.definition.setup_test_run
+      end
+      if database?
+        ActiveRecord::Base.connection.increment_open_transactions
+        ActiveRecord::Base.connection.begin_db_transaction
       end
-      self.class.definition.setup_test_run
     end
 
     def teardown_fixtures
-      self.class.definition &amp;&amp; self.class.definition.teardown_test_run
+      if self.class.definition 
+        self.class.definition.teardown_test_run
+      end
+      if database?
+        ActiveRecord::Base.connection.rollback_db_transaction
+        ActiveRecord::Base.verify_active_connections!
+      end
     end
 
     def stubs(key)
@@ -45,4 +60,4 @@ module ModelStubbing
       self.class.definition &amp;&amp; self.class.definition.current_time
     end
   end
-end
\ No newline at end of file
+end</diff>
      <filename>lib/model_stubbing/extensions.rb</filename>
    </modified>
    <modified>
      <diff>@@ -85,14 +85,22 @@ module ModelStubbing
       &lt;&lt;-END
       def #{@plural}(key, attrs = {})
         klass = self.class
-        until klass == Object
-          if defined?(klass.definition) and klass.definition
-            return klass.definition.models[#{@plural.inspect}].retrieve_record(key, attrs)
-          else
-            klass = klass.parent
+        unless defined?(klass.definition) and klass.definition
+          # If we are in a subclass where define_models was called on a superclass but
+          # not on this subclass (e.g. in a nested define block) then define_models
+          # needs to be called again for this subclass in order for the teardown to
+          # happen correctly.
+          k = klass.superclass
+          name = nil
+          until k == Object or k.nil?
+            if defined?(k.definition) and k.definition
+              name = k.definition.name
+              k = nil
+            end
           end
+          klass.module_eval { define_models name }
         end
-        raise 'Could not find models in #{ self.class.name }'
+        klass.definition.models[#{@plural.inspect}].retrieve_record(key, attrs)
       end
       def new_#{@singular}(key = :default, attrs = {})
         key, attrs = :default, key if key.is_a?(Hash)</diff>
      <filename>lib/model_stubbing/model.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>2c1e89fae938b04c3591445973774ed442fcba7d</id>
    </parent>
  </parents>
  <author>
    <name>Darrick Wiebe</name>
    <email>darrick@innatesoftware.com</email>
  </author>
  <url>http://github.com/technoweenie/model_stubbing/commit/5f2268bc96b6f08caf535622495b05ea41cec2df</url>
  <id>5f2268bc96b6f08caf535622495b05ea41cec2df</id>
  <committed-date>2009-02-10T18:17:21-08:00</committed-date>
  <authored-date>2009-02-09T22:56:50-08:00</authored-date>
  <message>Bettor support for nested definitions.

The database rollback needs to happen even if no models were initiated because they could always be manually created. Allow Definition instances to know their own name. Improve upon the previous method of searching the class heirarchy for definitions, now search for what was defined and then redefine it in the current subclass -- that actually got all of my tests passing in my app.

Signed-off-by: rick &lt;technoweenie@gmail.com&gt;</message>
  <tree>2a64a8682f453d6cdadf6233c7b94f80adad7081</tree>
  <committer>
    <name>rick</name>
    <email>technoweenie@gmail.com</email>
  </committer>
</commit>
