Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created June 9, 2011 07:51
Show Gist options
  • Save yuroyoro/1016275 to your computer and use it in GitHub Desktop.
Save yuroyoro/1016275 to your computer and use it in GitHub Desktop.
RSpecでexsample実行後に残ってるレコードをすべて削除する何らかのアレ
# for rspec1.3.1
require 'benchmark'
module Spec
module Example
module ExampleGroupMethods
# alias_method :orig_run, :run
MODEL_DIR = File.join(RAILS_ROOT, "app/models")
def clean_up_models
models = nil
tms = Benchmark.measure {
models = Dir.glob(File.join(MODEL_DIR, "**/*.rb")).map{|m|
begin
m.sub(File.join(MODEL_DIR, "/"), '').sub(/\.rb$/,'').camelize.constantize
rescue LoadError
nil
end
}.reject(&:nil?).select{|c| c <= ActiveRecord::Base }.uniq.each{|c|
if c.count > 0
puts "Delete #{c} -> count: #{c.count}"
c.delete_all
end
}
}
if models.present?
puts "-" * 80
puts Benchmark::CAPTION
puts tms
puts "-" * 80
end
end
clean_up_models
# def run(run_options)
# success = orig_run(run_options)
# clean_up_models
# success
# end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment