Guest User

Untitled

a guest
Jun 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. class PrintCount
  2. def diff
  3. before = count_all_models_with_table
  4. after = if block_given?
  5. yield
  6. count_all_models_with_table
  7. else
  8. Hash.new(0)
  9. end
  10. print_diff(before, after)
  11. end
  12.  
  13. private
  14.  
  15. def models_with_table
  16. ActiveRecord::Base.connection.tables.map do |x|
  17. x.classify.safe_constantize
  18. end.compact
  19. end
  20.  
  21.  
  22. def count_all_models_with_table
  23. models_with_table.each_with_object(Hash.new(0)) do |klass, collection|
  24. collection[klass.name] = if klass.respond_to? :with_deleted
  25. klass.with_deleted.count
  26. else
  27. klass.count
  28. end
  29. end
  30. end
  31.  
  32. def print_diff(before, after)
  33. before.keys.each do |key|
  34. next if before[key] == after[key]
  35.  
  36. diff = before[key] - after[key]
  37. Rails.logger.warn("#{key}: #{diff}")
  38. end
  39. end
  40. end
Add Comment
Please, Sign In to add comment