lib/fixture_background/background.rb in fixture_background-0.9.6 vs lib/fixture_background/background.rb in fixture_background-0.9.7

- old
+ new

@@ -1,6 +1,15 @@ module FixtureBackground + class << self + + def clean_database! + (ActiveRecord::Base.connection.tables - ["schema_migrations"]).each do |table_name| + ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}" + end + end + end + class Background class << self def class_for_test(full_class_name, background_to_use, test_unit_class) full_class_name = full_class_name.gsub("::", "__") @@ -18,18 +27,25 @@ EOT klass.class_eval <<-EOT cattr_accessor :fixture_background @@background_generated = false + @@fixtures_enabled = false def initialize(*args) super - if (background = fixture_background) && !@@background_generated - background.generate! - @@background_generated = true - self.class.fixtures :all + if background = fixture_background + if !@@background_generated + background.generate! + @@background_generated = true + end + if !@@fixtures_enabled + self.class.fixtures :all + self.class.teardown_suite { FixtureBackground.clean_database! } + @@fixtures_enabled = true + end end end EOT klass.fixture_background = background_to_use @@ -51,10 +67,10 @@ def initialize(full_class_name, test_unit_class, parent, blk) @test_unit_class = test_unit_class @full_class_name = full_class_name @parent = parent @background_block = blk - + FixtureBackground.clean_database! @generator = Generator.new(@full_class_name, background_signature, fixture_path, ancestors_and_own_background_blocks, @test_unit_class) unless background_valid? end def generate! @generator.generate! if @generator