Sha256: 9318ac541549bd9c7968f6335868d849208423dbf8eaffe8b038c40397673766

Contents?: true

Size: 980 Bytes

Versions: 99

Compression:

Stored size: 980 Bytes

Contents

# frozen_string_literal: true

class MigrateUserRolesToParticipatoryProcessRoles < ActiveRecord::Migration[5.1]
  class ParticipatoryProcess < ApplicationRecord
    self.table_name = :decidim_participatory_processes
  end

  class User < ApplicationRecord
    self.table_name = :decidim_users
  end

  def up
    User.find_each do |user|
      next if user.roles.empty? || user.roles.include?("admin")

      values = processes(user).flat_map do |process|
        user.roles.map do |role|
          "(#{user.id}, #{process.id}, '#{role}', NOW(), NOW())"
        end
      end

      execute("
        INSERT INTO decidim_admin_participatory_process_user_roles
        (decidim_user_id, decidim_participatory_process_id, role, created_at, updated_at)
        VALUES #{values.join(", ")}
      ")
    end
    remove_column :decidim_users, :roles
  end

  private

  def processes(user)
    ParticipatoryProcess.where(decidim_organization_id: user.decidim_organization_id)
  end
end

Version data entries

99 entries across 99 versions & 1 rubygems

Version Path
decidim-core-0.13.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.12.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.13.0.pre1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.12.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.11.2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.12.0.pre db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.11.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.11.0.pre1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.10.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.10.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.9.3 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.9.2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.9.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.9.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.8.4 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.8.3 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.8.2 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.8.1 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb
decidim-core-0.8.0 db/migrate/20170713131308_migrate_user_roles_to_participatory_process_roles.rb