Skip to content

Commit

Permalink
Do not change the project's selected state when updated through the API
Browse files Browse the repository at this point in the history
  • Loading branch information
ahukkanen committed Dec 22, 2023
1 parent 7062332 commit 5271440
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/decidim/api/budget_mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/types/decidim/budgeting_pipeline/budget_mutation_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5271440

Please sign in to comment.