Class: Upgrade::DisambiguateUsernamesJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/upgrade/disambiguate_usernames_job.rb

Instance Method Summary collapse

Instance Method Details

#perform ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/jobs/upgrade/disambiguate_usernames_job.rb', line 7

def perform
  duplicates = duplicated_usernames
  return if duplicates.empty?
  suffix = 0
  FederailsCommon::FEDIVERSE_USERNAMES.each_pair do |model_name, attr|
    finder_scope(model_name).where(attr => duplicates).find_each do
      it.validate
      if it.errors.of_kind?(attr, :taken)
        suffix += 1
        new_value = "#{it.send(attr)}#{suffix}"
        it.update_attribute attr, new_value # rubocop:disable Rails/SkipsModelValidations
      end
    end
  end
end