Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PrintCount
- def diff
- before = count_all_models_with_table
- after = if block_given?
- yield
- count_all_models_with_table
- else
- Hash.new(0)
- end
- print_diff(before, after)
- end
- private
- def models_with_table
- ActiveRecord::Base.connection.tables.map do |x|
- x.classify.safe_constantize
- end.compact
- end
- def count_all_models_with_table
- models_with_table.each_with_object(Hash.new(0)) do |klass, collection|
- collection[klass.name] = if klass.respond_to? :with_deleted
- klass.with_deleted.count
- else
- klass.count
- end
- end
- end
- def print_diff(before, after)
- before.keys.each do |key|
- next if before[key] == after[key]
- diff = before[key] - after[key]
- Rails.logger.warn("#{key}: #{diff}")
- end
- end
- end
Add Comment
Please, Sign In to add comment