# frozen_string_literal: true

namespace :ip do
  desc 'Validates all IPs in DB'
  task validate: :environment do
    Ad.find_each do |ad|
      ip = ad.ip
      next if ip.nil? || IPAddress.valid?(ip)

      valid_ip = ip.split(',').map(&:strip).find { |s| IPAddress.valid?(s) }
      ad.update!(ip: valid_ip)
    end
  end
end

Add a rake task to fix most invalid IPs in DBs
Add a rake task to fix most invalid IPs in DBs

It looks like previously if one of the headers of the request had comma
separated values, the field would be incorrectly parsed and invalid data
would be stored in DB. Fix those values.
