Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Use DELETE method to unset project status

Alexander Brandon Coles3550aade4926
diff --git a/app/components/projects/status_button_component.rb b/app/components/projects/status_button_component.rb
index 7f7fb40b9d7..dd7a9cc4d88 100644
--- a/app/components/projects/status_button_component.rb
+++ b/app/components/projects/status_button_component.rb
@@ -87,7 +87,7 @@ class Projects::StatusButtonComponent < ApplicationComponent
tag: :a,
href: project_status_path(project, status_code: status.value),
content_arguments: {
- data: { turbo_method: :put },
+ data: { turbo_method: status.value ? :put : :delete },
aria: { current: (true if status == @status) }
}
)
diff --git a/app/constants/projects/statuses.rb b/app/constants/projects/statuses.rb
index 672796499d0..93dff7bc10d 100644
--- a/app/constants/projects/statuses.rb
+++ b/app/constants/projects/statuses.rb
@@ -32,7 +32,7 @@ module Projects
module Statuses
Status = Data.define(:code, :color, :icon) do
def id = code&.to_s || :none
- def value = code.to_s
+ def value = code&.to_s
end
NOT_SET = Status.new(code: nil, color: Color.new(hexcode: "#24292F"), icon: "issue-draft")
diff --git a/app/controllers/projects/status_controller.rb b/app/controllers/projects/status_controller.rb
index f48e14125fb..8942b9316fd 100644
--- a/app/controllers/projects/status_controller.rb
+++ b/app/controllers/projects/status_controller.rb
@@ -36,9 +36,19 @@ class Projects::StatusController < ApplicationController
before_action :authorize
def update
+ change_status_action(params.fetch(:status_code).presence)
+ end
+
+ def destroy
+ change_status_action(nil)
+ end
+
+ private
+
+ def change_status_action(status_code)
call = Projects::UpdateService
.new(model: @project, user: current_user)
- .call(status_code: params.fetch(:status_code).presence)
+ .call(status_code:)
if call.success?
@project = call.result
@@ -51,8 +61,6 @@ class Projects::StatusController < ApplicationController
end
end
- private
-
def respond_with_update_status_button
message = t(:notice_successful_update)
update_via_turbo_stream(component: Projects::StatusButtonComponent.new(project: @project, user: current_user))
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 4a674231a00..2e7d7512812 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -104,7 +104,7 @@ Rails.application.reloader.to_prepare do
"projects/settings/work_packages/internal_comments": %i[show update],
"projects/templated": %i[create destroy],
"projects/identifier": %i[show update],
- "projects/status": %i[update]
+ "projects/status": %i[update destroy]
},
permissible_on: :project,
require: :member,
diff --git a/config/routes.rb b/config/routes.rb
index 3d386c70016..1d8d02521ce 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -293,7 +293,7 @@ Rails.application.routes.draw do
resource :templated, only: %i[create destroy], controller: "templated"
resource :archive, only: %i[create destroy], controller: "archive"
resource :identifier, only: %i[show update], controller: "identifier"
- resource :status, only: %i[update], controller: "status"
+ resource :status, only: %i[update destroy], controller: "status"
end
member do
diff --git a/spec/components/projects/status_button_component_spec.rb b/spec/components/projects/status_button_component_spec.rb
index e56f97047fb..c583a651b89 100644
--- a/spec/components/projects/status_button_component_spec.rb
+++ b/spec/components/projects/status_button_component_spec.rb
@@ -89,7 +89,9 @@ RSpec.describe Projects::StatusButtonComponent, type: :component do
expect(page).to have_menu do
expect(page).to have_selector :menuitem, count: 7
- expect(page).to have_selector :menuitem, text: "Not set", aria: { current: true }
+ expect(page).to (have_selector :menuitem, text: "Not set", aria: { current: true } do |link|
+ expect(link[:"data-turbo-method"]).to eq "delete"
+ end)
expect(page).to have_selector :menuitem, text: "On track"
expect(page).to have_selector :menuitem, text: "At risk"
expect(page).to have_selector :menuitem, text: "Not started"
@@ -118,7 +120,9 @@ RSpec.describe Projects::StatusButtonComponent, type: :component do
expect(page).to have_menu do
expect(page).to have_selector :menuitem, count: 7
expect(page).to have_selector :menuitem, text: "Not set"
- expect(page).to have_selector :menuitem, text: "On track", aria: { current: true }
+ expect(page).to (have_selector :menuitem, text: "On track", aria: { current: true } do |link|
+ expect(link[:"data-turbo-method"]).to eq "put"
+ end)
expect(page).to have_selector :menuitem, text: "At risk"
expect(page).to have_selector :menuitem, text: "Not started"
expect(page).to have_selector :menuitem, text: "Finished"
diff --git a/spec/controllers/projects/status_controller_spec.rb b/spec/controllers/projects/status_controller_spec.rb
index cee7f1d8998..125f142dad8 100644
--- a/spec/controllers/projects/status_controller_spec.rb
+++ b/spec/controllers/projects/status_controller_spec.rb
@@ -34,24 +34,24 @@ RSpec.describe Projects::StatusController do
shared_let(:user) { create(:admin) }
current_user { user }
- describe "PUT #update" do
- let(:project) { build_stubbed(:project) }
- let(:service_result) { ServiceResult.success(result: project) }
+ let(:project) { build_stubbed(:project) }
+ let(:service_result) { ServiceResult.success(result: project) }
- before do
- allow(Project)
- .to receive(:find)
- .with(project.identifier)
- .and_return(project)
+ before do
+ allow(Project)
+ .to receive(:find)
+ .with(project.identifier)
+ .and_return(project)
- update_service = instance_double(Projects::UpdateService, call: service_result)
+ update_service = instance_double(Projects::UpdateService, call: service_result)
- allow(Projects::UpdateService)
- .to receive(:new)
- .with(user:, model: project)
- .and_return(update_service)
- end
+ allow(Projects::UpdateService)
+ .to receive(:new)
+ .with(user:, model: project)
+ .and_return(update_service)
+ end
+ describe "PUT #update" do
context "when service call succeeds" do
context "with valid status_code param" do
context "with a text/html request" do
@@ -133,4 +133,52 @@ RSpec.describe Projects::StatusController do
end
end
end
+
+ describe "DELETE #destroy" do
+ context "when service call succeeds" do
+ context "with a text/html request" do
+ it "redirects back or to project show" do
+ delete :destroy, params: { project_id: project }
+
+ expect(response).to redirect_to project_path(project)
+ expect(flash[:notice]).to eq I18n.t(:notice_successful_update)
+ end
+ end
+
+ context "with a turbo stream request" do
+ it "renders turbo streams updating Projects::StatusButtonComponent and flash action" do
+ delete :destroy, params: { project_id: project }, format: :turbo_stream
+
+ expect(response).to be_successful
+ expect(assigns(:project)).to eq project
+ expect(response).to have_turbo_stream action: "update", target: "projects-status-button-component"
+ expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
+ end
+ end
+ end
+
+ context "when service call fails" do
+ let(:service_result) { ServiceResult.failure(result: project, message: "Custom Field 1 must not be blank") }
+
+ context "with a text/html request" do
+ it "redirects back or to project show" do
+ delete :destroy, params: { project_id: project }
+
+ expect(response).to redirect_to project_path(project)
+ expect(flash[:error]).to start_with I18n.t(:notice_unsuccessful_update_with_reason, reason: "")
+ end
+ end
+
+ context "with a turbo stream request" do
+ it "renders turbo stream flash action" do
+ delete :destroy, params: { project_id: project }, format: :turbo_stream
+
+ expect(response).not_to be_successful
+ expect(response).to have_http_status :unprocessable_entity
+ expect(assigns(:project)).to eq project
+ expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
+ end
+ end
+ end
+ end
end
diff --git a/spec/routing/project_status_routing_spec.rb b/spec/routing/project_status_routing_spec.rb
index 15987860851..6e374a27628 100644
--- a/spec/routing/project_status_routing_spec.rb
+++ b/spec/routing/project_status_routing_spec.rb
@@ -38,4 +38,12 @@ RSpec.describe Projects::StatusController do
)
end
end
+
+ describe "destroy" do
+ it do
+ expect(delete("/projects/123/status")).to route_to(
+ controller: "projects/status", action: "destroy", project_id: "123"
+ )
+ end
+ end
end