database_cleaner-active_record icon indicating copy to clipboard operation
database_cleaner-active_record copied to clipboard

Refactor

Open betesh opened this issue 1 year ago • 0 comments

I want to be able to display progress of truncation using ruby-progressbar.

I don't consider the progress bar itself to be a concern of this gem, so all I've done in this PR is some refactoring that was anyway reasonable and that makes it possible to do the following in my application code:

cleaner = DatabaseCleaner.cleaners.values.first
cleaner.strategy = [:truncation, { pre_count: true }]
tables_to_truncate = cleaner.strategy.send(:tables_to_truncate)
progress = ProgressBar.create(total: tables_to_truncate.size)
tables = tables_to_truncate

tables.each do |table|
  progress.log "Truncating #{table}"
  cleaner.strategy = [:truncation, { pre_count: true, only: [table]}]
  cleaner.strategy.clean
  progress.increment
end

I acknowledge the application code isn't very pretty, but the alternative is exposing the strategies directly, and some small refactoring to support some ugly application code is more likely to be accepted that a major refactor that break encapsulation of the strategies.

betesh avatar Feb 05 '24 22:02 betesh