From 5271440411df67e72a159ac2e7a2d374cb2fba74 Mon Sep 17 00:00:00 2001 From: Antti Hukkanen Date: Fri, 22 Dec 2023 13:39:36 +0200 Subject: [PATCH] Do not change the project's selected state when updated through the API --- lib/decidim/api/budget_mutation_type.rb | 5 ++++- .../budget_mutation_type_spec.rb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/decidim/api/budget_mutation_type.rb b/lib/decidim/api/budget_mutation_type.rb index bf20792..f3a5142 100644 --- a/lib/decidim/api/budget_mutation_type.rb +++ b/lib/decidim/api/budget_mutation_type.rb @@ -58,7 +58,7 @@ def update_project(id:, attributes:) enforce_permission_to :update, :project, project: project - form = project_form_from(attributes) + form = project_form_from(attributes, project) Decidim::Budgets::Admin::UpdateProject.call(form, project) do on(:ok) do return project @@ -105,6 +105,7 @@ def project_form_from(attributes, project = nil) ) end + # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity def project_params(attributes, project = nil) { "title" => attributes.title, @@ -121,9 +122,11 @@ def project_params(attributes, project = nil) "idea_ids" => attributes.idea_ids || related_ids_for(project, :ideas), "plan_ids" => attributes.plan_ids || related_ids_for(project, :plans) }.tap do |attrs| + attrs["selected"] ||= project&.selected? attrs.merge!(attributes.main_image_attributes) if attributes.main_image_attributes end end + # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity def related_ids_for(project, resource) return [] unless project diff --git a/spec/types/decidim/budgeting_pipeline/budget_mutation_type_spec.rb b/spec/types/decidim/budgeting_pipeline/budget_mutation_type_spec.rb index d6ce8a3..c686bea 100644 --- a/spec/types/decidim/budgeting_pipeline/budget_mutation_type_spec.rb +++ b/spec/types/decidim/budgeting_pipeline/budget_mutation_type_spec.rb @@ -78,6 +78,8 @@ expect(project.linked_resources(:proposals, "included_proposals").map(&:id)).to match_array(proposals.map(&:id)) expect(project.linked_resources(:ideas, "included_ideas").map(&:id)).to match_array(ideas.map(&:id)) expect(project.linked_resources(:plans, "included_plans").map(&:id)).to match_array(plans.map(&:id)) + + expect(project.selected?).to be(false) end context "with a blob" do @@ -171,6 +173,21 @@ expect(project.linked_resources(:proposals, "included_proposals").map(&:id)).to match_array(proposals.map(&:id)) expect(project.linked_resources(:ideas, "included_ideas").map(&:id)).to match_array(ideas.map(&:id)) expect(project.linked_resources(:plans, "included_plans").map(&:id)).to match_array(plans.map(&:id)) + + expect(project.selected?).to be(false) + end + + context "when the project is selected" do + before { project.update!(selected_at: Time.current) } + + it "does not change the selected state" do + expect { response }.not_to change(Decidim::Budgets::Project, :count) + + expect(response["updateProject"]).to eq("id" => project.id.to_s) + project.reload + + expect(project.selected?).to be(true) + end end context "with a blob" do