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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#63482] Implement Project Status Button Component
Alexander Brandon Coles7d92ee2831fd
diff --git a/app/components/op_primer/status_button_component.rb b/app/components/op_primer/status_button_component.rbindex 4e863016fc2..74260921ff1 100644--- a/app/components/op_primer/status_button_component.rb+++ b/app/components/op_primer/status_button_component.rb@@ -36,6 +36,11 @@ module OpPrimerdef initialize(current_status:, items:, readonly: false, disabled: false, button_arguments: {}, menu_arguments: {})super+ menu_arguments[:classes] = class_names(+ menu_arguments[:classes],+ "op-status-button"+ )+@current_status = current_status@items = items@readonly = readonlydiff --git a/app/components/projects/status_button_component.rb b/app/components/projects/status_button_component.rbnew file mode 100644index 00000000000..7f7fb40b9d7--- /dev/null+++ b/app/components/projects/status_button_component.rb@@ -0,0 +1,95 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class Projects::StatusButtonComponent < ApplicationComponent+ include ApplicationHelper+ include OpTurbo::Streamable+ include OpPrimer::ComponentHelpers+ include ProjectStatusHelper++ attr_reader :project, :user++ def initialize(project:, user:, size: :medium)+ super++ @project = project+ @user = user+ @size = size++ @status = find_status(project.status_code)+ end++ def call+ component_wrapper do+ render(+ OpPrimer::StatusButtonComponent.new(+ current_status: build_item(@status),+ items: build_items,+ readonly: !edit_enabled?,+ disabled: !edit_enabled?,+ button_arguments: {+ title: t("projects.status.button_edit"),+ size: @size+ }+ )+ )+ end+ end++ private++ def edit_enabled?+ user.allowed_in_project?(:edit_project, project)+ end++ def find_status(code)+ Projects::Statuses::AVAILABLE.find(-> { Projects::Statuses::NOT_SET }) { it.code&.to_s == code }+ end++ def build_items+ Projects::Statuses::AVAILABLE.map { build_item(it) }+ end++ def build_item(status)+ OpPrimer::StatusButtonOption.new(+ name: project_status_name(status.code),+ color_namespace: :project_status,+ color_ref: status.id,+ icon: status.icon,+ item_id: status.id,+ tag: :a,+ href: project_status_path(project, status_code: status.value),+ content_arguments: {+ data: { turbo_method: :put },+ aria: { current: (true if status == @status) }+ }+ )+ end+enddiff --git a/app/constants/projects/statuses.rb b/app/constants/projects/statuses.rbnew file mode 100644index 00000000000..672796499d0--- /dev/null+++ b/app/constants/projects/statuses.rb@@ -0,0 +1,57 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Projects+ module Statuses+ Status = Data.define(:code, :color, :icon) do+ def id = code&.to_s || :none+ def value = code.to_s+ end++ NOT_SET = Status.new(code: nil, color: Color.new(hexcode: "#24292F"), icon: "issue-draft")+ ON_TRACK = Status.new(code: :on_track, color: Color.new(hexcode: "#1F883D"), icon: "issue-opened")+ AT_RISK = Status.new(code: :at_risk, color: Color.new(hexcode: "#BC4C00"), icon: "alert")+ OFF_TRACK = Status.new(code: :off_track, color: Color.new(hexcode: "#CF222E"), icon: "stop")+ NOT_STARTED = Status.new(code: :not_started, color: Color.new(hexcode: "#0969DA"), icon: "circle")+ FINISHED = Status.new(code: :finished, color: Color.new(hexcode: "#8250DF"), icon: "issue-closed")+ DISCONTINUED = Status.new(code: :discontinued, color: Color.new(hexcode: "#9A6700"), icon: "no-entry")++ VALID = [+ ON_TRACK,+ AT_RISK,+ OFF_TRACK,+ NOT_STARTED,+ FINISHED,+ DISCONTINUED+ ].freeze++ AVAILABLE = [NOT_SET, *VALID].freeze+ end+enddiff --git a/app/controllers/projects/status_controller.rb b/app/controllers/projects/status_controller.rbnew file mode 100644index 00000000000..f48e14125fb--- /dev/null+++ b/app/controllers/projects/status_controller.rb@@ -0,0 +1,68 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class Projects::StatusController < ApplicationController+ include OpTurbo::ComponentStream+ include OpTurbo::FlashStreamHelper++ before_action :find_project_by_project_id+ before_action :authorize++ def update+ call = Projects::UpdateService+ .new(model: @project, user: current_user)+ .call(status_code: params.fetch(:status_code).presence)++ if call.success?+ @project = call.result+ respond_with_update_status_button+ else+ message = t(:notice_unsuccessful_update_with_reason, reason: call.message)+ respond_with_flash_error(message:) do |format|+ fallback_responses_for(format, flash: { error: message })+ end+ 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))+ render_success_flash_message_via_turbo_stream(message:)+ respond_with_turbo_streams do |format|+ fallback_responses_for(format, flash: { notice: message })+ end+ end++ def fallback_responses_for(format, **)+ format.html { redirect_back_or_to(project_path(@project), **) }+ end+enddiff --git a/app/helpers/project_status_helper.rb b/app/helpers/project_status_helper.rbindex 1c054521cd0..42f82fb6147 100644--- a/app/helpers/project_status_helper.rb+++ b/app/helpers/project_status_helper.rb@@ -29,7 +29,7 @@module ProjectStatusHelperdef project_status_css_class(status_code)code = project_status_ensure_default_code(status_code)- "-" + code.tr("_", "-")+ "-#{code.dasherize}"enddef project_status_name(status_code)@@ -39,9 +39,11 @@ module ProjectStatusHelperdef project_status_name_for_code(code)code ||= "not_set"- I18n.t("js.grid.widgets.project_status." + code)+ I18n.t("js.grid.widgets.project_status.#{code}")end+ private+def project_status_ensure_default_code(status_code)status_code || "not_set"enddiff --git a/app/views/highlighting/styles.css.erb b/app/views/highlighting/styles.css.erbindex cfd9dc5e4b3..c98610f2b85 100644--- a/app/views/highlighting/styles.css.erb+++ b/app/views/highlighting/styles.css.erb@@ -9,6 +9,10 @@<%= resource_color_css("meeting_status", meeting_status) %><% end %>+<% Projects::Statuses::AVAILABLE.each do |project_status| %>+<%= resource_color_css("project_status", project_status) %>+<% end %>+<%= color_css %><%# usage classes %>diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex b29e02cf596..4a674231a00 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -103,7 +103,8 @@ Rails.application.reloader.to_prepare do"projects/settings/work_packages": %i[show],"projects/settings/work_packages/internal_comments": %i[show update],"projects/templated": %i[create destroy],- "projects/identifier": %i[show update]+ "projects/identifier": %i[show update],+ "projects/status": %i[update]},permissible_on: :project,require: :member,diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 2c7b53865a2..3eaf6d99d02 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -459,6 +459,8 @@ en:no_results_title_text: There is no additional recorded disk space consumed by this project.work_package_priorities:new_label: "New priority"+ status:+ button_edit: Edit project statusproject_phase:working_days_count:one: "Duration: %{count} working day"@@ -3429,6 +3431,7 @@ en:notice_successful_cancel: "Successful cancellation."notice_successful_update: "Successful update."notice_unsuccessful_update: "Update failed."+ notice_unsuccessful_update_with_reason: "Update failed: %{reason}"notice_successful_update_custom_fields_added_to_project: |Successful update. The custom fields of the activated types are automatically activatedon the work package form. <a href="%{url}" target="_blank">See more</a>.diff --git a/config/routes.rb b/config/routes.rbindex f860bff13aa..3d386c70016 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -293,6 +293,7 @@ Rails.application.routes.draw doresource :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"endmember dodiff --git a/lookbook/previews/open_project/projects/status_button_component_preview.rb b/lookbook/previews/open_project/projects/status_button_component_preview.rbnew file mode 100644index 00000000000..205a22ff44a--- /dev/null+++ b/lookbook/previews/open_project/projects/status_button_component_preview.rb@@ -0,0 +1,15 @@+# frozen_string_literal: true++module OpenProject::Projects+ # @logical_path OpenProject/Projects+ class StatusButtonComponentPreview < ViewComponent::Preview+ # !! Currently nothing happens when changing the status!!+ # @display min_height 400px+ # @param size [Symbol] select [small, medium, large]+ def playground(size: :medium)+ user = FactoryBot.build_stubbed(:admin)+ project = FactoryBot.build_stubbed(:project, :with_status)+ render(Projects::StatusButtonComponent.new(project:, user:, size:))+ end+ end+enddiff --git a/spec/components/projects/status_button_component_spec.rb b/spec/components/projects/status_button_component_spec.rbnew file mode 100644index 00000000000..e56f97047fb--- /dev/null+++ b/spec/components/projects/status_button_component_spec.rb@@ -0,0 +1,131 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "rails_helper"++RSpec.describe Projects::StatusButtonComponent, type: :component do+ let(:project) { build_stubbed(:project, status_code:) }+ let(:status_code) { nil }++ let(:user) { build_stubbed(:user) }++ current_user { user }++ subject do+ render_inline(described_class.new(project:, user:))+ page+ end++ shared_examples "component wrapper" do |id|+ it "renders component wrapper" do+ expect(subject).to have_xpath "//*[@id='#{id}']", count: 1+ end+ end++ it_behaves_like "component wrapper", "projects-status-button-component"++ context "when the user has no project edit permissions" do+ context "when status code is not set" do+ it "renders a disabled button" do+ expect(subject).to have_button "Not set", disabled: true+ end+ end++ context "when status code is set" do+ let(:status_code) { :off_track }++ it "renders a disabled button" do+ expect(subject).to have_button "Off track", disabled: true+ end+ end++ it "does not render a Primer ActionMenu" do+ expect(subject).not_to have_element "action-menu"+ end+ end++ context "when the user has project edit permissions" do+ let(:user) { build_stubbed(:admin) }++ context "when status code is not set" do+ it "renders an enabled button" do+ expect(subject).to have_button "Not set", disabled: false, aria: { label: "Edit project status" }+ end++ it "renders a Primer ActionMenu (single variant)" do+ expect(subject).to have_element "action-menu", "data-select-variant": "none"+ expect(subject).to have_element "action-menu", class: "op-status-button"+ end++ it "renders project status options" do+ subject++ 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: "On track"+ 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"+ expect(page).to have_selector :menuitem, text: "Discontinued"+ expect(page).to have_selector :menuitem, text: "Off track"+ end+ end+ end++ context "when status code is set" do+ let(:status_code) { :on_track }++ it "renders an enabled button" do+ expect(subject).to have_button "On track", disabled: false, aria: { label: "Edit project status" }+ end++ it "renders a Primer ActionMenu (single variant)" do+ expect(subject).to have_element "action-menu", "data-select-variant": "none"+ expect(subject).to have_element "action-menu", class: "op-status-button"+ end++ it "renders project status options" do+ subject++ 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: "At risk"+ expect(page).to have_selector :menuitem, text: "Not started"+ expect(page).to have_selector :menuitem, text: "Finished"+ expect(page).to have_selector :menuitem, text: "Discontinued"+ expect(page).to have_selector :menuitem, text: "Off track"+ end+ end+ end+ end+enddiff --git a/spec/controllers/projects/status_controller_spec.rb b/spec/controllers/projects/status_controller_spec.rbnew file mode 100644index 00000000000..cee7f1d8998--- /dev/null+++ b/spec/controllers/projects/status_controller_spec.rb@@ -0,0 +1,136 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "rails_helper"++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) }++ before do+ allow(Project)+ .to receive(:find)+ .with(project.identifier)+ .and_return(project)++ update_service = instance_double(Projects::UpdateService, call: service_result)++ allow(Projects::UpdateService)+ .to receive(:new)+ .with(user:, model: project)+ .and_return(update_service)+ end++ context "when service call succeeds" do+ context "with valid status_code param" do+ context "with a text/html request" do+ it "redirects back or to project show" do+ put :update, params: { project_id: project, status_code: :foo }++ 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+ put :update, params: { project_id: project, status_code: :foo }, 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 "with valid empty status_code param" do+ context "when service call succeeds" do+ context "with a text/html request" do+ it "redirects back or to project show" do+ put :update, params: { project_id: project, status_code: "" }++ 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+ put :update, params: { project_id: project, status_code: "" }, 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+ 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+ put :update, params: { project_id: project, status_code: :foo }++ 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+ put :update, params: { project_id: project, status_code: :foo }, 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++ context "with invalid params" do+ it "invalid params" do+ put :update, params: { project_id: project, not_status_code: "something" }, format: :turbo_stream++ expect(response).not_to be_successful+ expect(response).to have_http_status :bad_request+ end+ end+ end+enddiff --git a/spec/routing/project_status_routing_spec.rb b/spec/routing/project_status_routing_spec.rbnew file mode 100644index 00000000000..15987860851--- /dev/null+++ b/spec/routing/project_status_routing_spec.rb@@ -0,0 +1,41 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "spec_helper"++RSpec.describe Projects::StatusController do+ describe "update" do+ it do+ expect(put("/projects/123/status")).to route_to(+ controller: "projects/status", action: "update", project_id: "123"+ )+ end+ end+end