From af8af23e705636d885f96394fa75d7cbfaf114d8 Mon Sep 17 00:00:00 2001 From: quentinchampenois <26109239+Quentinchampenois@users.noreply.github.com> Date: Thu, 18 Apr 2024 08:35:17 +0200 Subject: [PATCH] fix: Modify default proposal states --- ...l_states.decidim_custom_proposal_states.rb | 36 +++++++++++++++++++ db/schema.rb | 4 +-- 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20240115111975_modify_default_proposal_states.decidim_custom_proposal_states.rb diff --git a/db/migrate/20240115111975_modify_default_proposal_states.decidim_custom_proposal_states.rb b/db/migrate/20240115111975_modify_default_proposal_states.decidim_custom_proposal_states.rb new file mode 100644 index 00000000..f97d0952 --- /dev/null +++ b/db/migrate/20240115111975_modify_default_proposal_states.decidim_custom_proposal_states.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +# This migration comes from decidim_custom_proposal_states (originally 20231102234909) + +class ModifyDefaultProposalStates < ActiveRecord::Migration[6.0] + class Proposal < ApplicationRecord + belongs_to :proposal_state, + class_name: "Decidim::CustomProposalStates::ProposalState", + foreign_key: "decidim_proposals_proposal_state_id", + inverse_of: :proposals, + optional: true + + self.table_name = :decidim_proposals_proposals + end + + def up + Decidim::Proposals::Proposal.where(state: "") + .or(Decidim::Proposals::Proposal.where(state: nil)) + .update_all(state: "not_answered") + + Decidim::Component.where(manifest_name: "proposals").find_each do |component| + admin_user = component.organization.admins.first + system_custom_proposal_states = Decidim::CustomProposalStates::ProposalState.where(component: component, system: true) + next if system_custom_proposal_states.present? + + default_states = Decidim::CustomProposalStates.create_default_states!(component, admin_user).with_indifferent_access + Proposal.where(decidim_component_id: component.id).find_each do |proposal| + proposal.proposal_state = default_states.dig(proposal.state.to_s, :object) + proposal.save! + end + end + change_column_null :decidim_proposals_proposals, :decidim_proposals_proposal_state_id, false + end + + def down; end +end diff --git a/db/schema.rb b/db/schema.rb index 71c9f705..9a75370d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2024_01_15_111974) do +ActiveRecord::Schema.define(version: 2024_01_15_111975) do # These are extensions that must be enabled in order to support this database enable_extension "ltree" @@ -1360,7 +1360,7 @@ t.jsonb "body" t.integer "comments_count", default: 0, null: false t.integer "follows_count", default: 0, null: false - t.integer "decidim_proposals_proposal_state_id" + t.integer "decidim_proposals_proposal_state_id", null: false t.index "md5((body)::text)", name: "decidim_proposals_proposal_body_search" t.index "md5((title)::text)", name: "decidim_proposals_proposal_title_search" t.index ["answered_at"], name: "index_decidim_proposals_proposals_on_answered_at"