Sha256: 0daca6995654670596b2d300808821be270739933fbaeb65a6356212330d26b7

Contents?: true

Size: 1.57 KB

Versions: 22

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

class FixProposalsData < ActiveRecord::Migration[5.2]
  class Proposal < ApplicationRecord
    self.table_name = :decidim_proposals_proposals
  end

  class Coauthorship < ApplicationRecord
    self.table_name = :decidim_coauthorships
  end

  class UserBaseEntity < ApplicationRecord
    self.table_name = :decidim_users
    self.inheritance_column = nil # disable the default inheritance
  end

  class Organization < ApplicationRecord
    self.table_name = :decidim_organizations
  end

  def up
    PaperTrail.request(enabled: false) do
      Proposal.find_each do |proposal|
        next if proposal.title.is_a?(Hash) && proposal.body.is_a?(Hash)

        coauthorship = Coauthorship.order(:id).find_by(coauthorable_type: "Decidim::Proposals::Proposal", coauthorable_id: proposal.id)
        author =
          if coauthorship.decidim_author_type == "Decidim::Organization"
            Organization.find_by(id: coauthorship.decidim_author_id)
          else
            UserBaseEntity.find_by(id: coauthorship.decidim_author_id)
          end

        locale = author.try(:locale).presence || author.try(:default_locale).presence || author.try(:organization).try(:default_locale).presence

        # rubocop:disable Rails/SkipsModelValidations
        values = {}
        values[:title] = { locale => proposal.title } unless proposal.title.is_a?(Hash)
        values[:body] = { locale => proposal.body } unless proposal.body.is_a?(Hash)

        proposal.update_columns(values)
        # rubocop:enable Rails/SkipsModelValidations
      end
    end
  end

  def down; end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
decidim-proposals-0.29.1 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.28.4 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.27.9 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.29.0 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.28.3 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.27.8 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.29.0.rc4 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.29.0.rc3 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.29.0.rc2 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.29.0.rc1 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.28.2 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.27.7 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.28.1 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.27.6 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.26.10 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.26.9 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.28.0 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.27.5 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.28.0.rc5 db/migrate/20201002085508_fix_proposals_data.rb
decidim-proposals-0.28.0.rc4 db/migrate/20201002085508_fix_proposals_data.rb