Sha256: 192b147403092ce0721599ed4f22070f338bb4f58c5df87bca1a2ae6b73e50ac

Contents?: true

Size: 1.71 KB

Versions: 19

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

namespace :decidim do
  namespace :upgrade do
    desc "Modify nicknames with random numbers when exists similar ones case insensitively"
    task fix_nickname_uniqueness: :environment do
      logger = Logger.new($stdout)
      logger.info("Updating conflicting user nicknames...")

      # list of users already changed in the process
      has_changed = []

      Decidim::User.not_deleted.find_each do |user|
        next if has_changed.include? user.id

        Decidim::User.where(organization: user.organization)
                     .where("nickname ILIKE ?", user.nickname.downcase)
                     .where.not(id: has_changed + [user.id])
                     .not_deleted
                     .order(:created_at)
                     .each do |similar_user|
          # change her nickname to the lowercased one with numbers if needed
          begin
            update_user_nickname(similar_user, Decidim::UserBaseEntity.nicknamize(similar_user.nickname, organization: similar_user.organization))
          rescue ActiveRecord::RecordInvalid => e
            logger.warn("User ID (#{similar_user.id}) : #{e}")
          end
          has_changed.append(similar_user.id)
        end
      end
      logger.info("Process terminated, #{has_changed.count} users nickname have been updated.")
    end

    private

    def send_notification_to(user)
      Decidim::EventsManager.publish(
        event: "decidim.events.nickname_event",
        event_class: Decidim::ChangeNicknameEvent,
        affected_users: [user],
        resource: user
      )
    end

    def update_user_nickname(user, new_nickname)
      user.update!(nickname: new_nickname)
      send_notification_to(user)
      user
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-core-0.28.4 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.9 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.28.3 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.8 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.28.2 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.7 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.28.1 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.6 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.28.0 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.5 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.28.0.rc5 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.28.0.rc4 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.4 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.3 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.2 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.1 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.0 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.0.rc2 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
decidim-core-0.27.0.rc1 lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake