Sha256: bc47f0d54763bfe3df959894b1ef13e901c684ac310895e8025b8b344250ade2

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

#
# Assumes to authorizations in the old format (as rectify form classes) will be
# registered as the underscored class name using the new API. For example, a
# previous
#
# ```
# config.authorization_handlers = ["Decidim::ExampleCensusHandler"]
# ```
#
# will now be
#
# ```
# Decidim::Verifications.register_workflow(:example_census_handler) do |auth|
#   auth.form = "Decidim::ExampleCensusHandler"
# end
# ```
#
class MoveAuthorizationsToNewApi < ActiveRecord::Migration[5.1]
  class Organization < ApplicationRecord
    self.table_name = :decidim_organizations
  end

  class Feature < ApplicationRecord
    self.table_name = :decidim_features
  end

  def up
    Organization.find_each do |organization|
      migrated_authorizations = organization.available_authorizations.map do |authorization|
        authorization.demodulize.underscore
      end

      organization.update!(available_authorizations: migrated_authorizations)
    end

    Feature.find_each do |feature|
      feature.permissions.transform_values! do |value|
        value["authorization_handler_name"].classify.demodulize.underscore
      end

      feature.save!
    end
  end

  def down
    Organization.find_each do |organization|
      migrated_authorizations = organization.available_authorizations.map do |authorization|
        Decidim::Verifications.find_workflow_manifest(authorization).form
      end

      organization.update!(available_authorizations: migrated_authorizations)
    end

    Feature.find_each do |feature|
      feature.permissions.transform_values! do |value|
        workflow = Decidim::Verifications.find_workflow_manifest(value)

        workflow.form.underscore
      end

      feature.save!
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-verifications-0.8.1 db/migrate/20171030133426_move_authorizations_to_new_api.rb
decidim-verifications-0.8.0 db/migrate/20171030133426_move_authorizations_to_new_api.rb