database_cleaner icon indicating copy to clipboard operation
database_cleaner copied to clipboard

Add cleaning_with method

Open datibbaw opened this issue 9 years ago • 0 comments

Hi,

I'm slowly transitioning my tests to use transaction based cleaning instead of truncation. To do this, I'm currently using an around method which will use the strategy I want only for that test;

around(:each) { |example| DatabaseCleaner.cleaning_with(:transaction, &example) }

This is my extension:

module DatabaseCleaner
  class Base
    def cleaning_with(*args, &block)
      create_strategy(*args).cleaning(&block)
    end
  end

  class << self
    def cleaning_with(*args, &inner_block)
      connections.reduce(inner_block) do |curr_block, connection|
        proc { connection.cleaning_with(*args, &curr_block) }
      end.call
    end
  end
end

Would you consider adding this to the project itself? Btw, I realise doing this introduces some code repetition, I haven't yet looked at that because I didn't want to touch the original source code yet ;-)

datibbaw avatar Feb 08 '16 00:02 datibbaw