desc "Removes users with suspicious names that indicate they were generated by bots"
task :remove_bot_users, [:model] => :environment do
  puts "Searching for suspected bot users"

  suspected_bot_users = User.where("created_at > ?", 2.months.ago)
                            .where("length(first_name) > ? and length(last_name) > ?", 8, 8)
                            .where("initcap(first_name) != first_name and initcap(last_name) != last_name")

  puts "Preparing to remove #{suspected_bot_users.count} users:\n"
  suspected_bot_users.pluck(:first_name, :last_name).map(&:join).each do |full_name|
    puts full_name
  end
  puts "\nProceed? (y/N)"

  answer = STDIN.gets.chomp
  abort "Aborting." unless answer.upcase == "Y"

  start_time = Time.current

  failed_users = []

  suspected_bot_users.find_each do |bot_user|
    if bot_user.destroy
      puts "Removed #{bot_user.full_name}"
    else
      puts "Failed to remove #{bot_user.full_name}"
      failed_users << bot_user
    end
  end

  elapsed_time = Time.current - start_time
  puts "\nFinished removing bot users in #{elapsed_time} seconds"

  if failed_users.present?
    puts "\nFailed to remove #{failed_users.size} users:\n"
    failed_users.each do |user|
      puts user.full_name
    end
  end
end

desc "Removes users with suspicious names that indicate they were generated by bots"
task :remove_bot_users, [:model] => :environment do
  puts "Searching for suspected bot users"

  suspected_bot_users = User.where("created_at > ?", 2.months.ago)
                            .where("length(first_name) > ? and length(last_name) > ?", 8, 8)
                            .where("initcap(first_name) != first_name and initcap(last_name) != last_name")

  puts "Preparing to remove #{suspected_bot_users.count} users:\n"
  suspected_bot_users.pluck(:first_name, :last_name).map { |names| names.join(" ") }.each do |full_name|
    puts full_name
  end
  puts "\nProceed? (y/N)"

  answer = STDIN.gets.chomp
  abort "Aborting." unless answer.upcase == "Y"

  start_time = Time.current

  failed_users = []

  suspected_bot_users.find_each do |bot_user|
    if bot_user.destroy
      puts "Removed #{bot_user.full_name}"
    else
      puts "Failed to remove #{bot_user.full_name}"
      failed_users << bot_user
    end
  end

  elapsed_time = Time.current - start_time
  puts "\nFinished removing bot users in #{elapsed_time} seconds"

  if failed_users.present?
    puts "\nFailed to remove #{failed_users.size} users:\n"
    failed_users.each do |user|
      puts user.full_name
    end
  end
end

Fix name listing in rake task
Fix name listing in rake task
