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

add ability to toggle the active state of a step

ulferts2c7b1bc3a81c
diff --git a/app/components/projects/settings/life_cycles/element_component.html.erb b/app/components/projects/settings/life_cycles/element_component.html.erb
index f5a249fb1ff..b992db25d90 100644
--- a/app/components/projects/settings/life_cycles/element_component.html.erb
+++ b/app/components/projects/settings/life_cycles/element_component.html.erb
@@ -7,6 +7,29 @@
render(Primer::Beta::Text.new) { element.name }
end
end
+ # py: 1 quick fix: prevents the row from bouncing as the toggle switch currently changes height while toggling
+ element_container.with_column(py: 1, mr: 2) do
+ # buggy currently:
+ # small variant + status_label_position: :start leads to a small bounce while toggling
+ # behavior can be seen on primer's viewbook as well -> https://view-components-storybook.eastus.cloudapp.azure.com/view-components/lookbook/inspect/primer/alpha/toggle_switch/small
+ # quick fix: don't display loading indicator which is causing the bounce
+ render(Primer::Alpha::ToggleSwitch.new(
+ src: toggle_project_settings_life_cycle_path(
+ project_life_cycle: {
+ project_id: project.id,
+ life_cycle_id: element.id,
+ type: element.type
+ }
+ ),
+ csrf_token: form_authenticity_token,
+ data: { test_selector: "toggle-project-life-cycle-#{element.id}" },
+ aria: { label: toggle_aria_label },
+ checked: active_in_project?,
+ size: :small,
+ status_label_position: :start,
+ classes: "op-primer-adjustments__toggle-switch--hidden-loading-indicator",
+ ))
+ end
end
end
%>
diff --git a/app/components/projects/settings/life_cycles/element_component.rb b/app/components/projects/settings/life_cycles/element_component.rb
index 39d1f5ac39e..5593bd072fa 100644
--- a/app/components/projects/settings/life_cycles/element_component.rb
+++ b/app/components/projects/settings/life_cycles/element_component.rb
@@ -34,12 +34,25 @@ module Projects
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- attr_reader :element
+ attr_reader :element,
+ :project
- def initialize(life_cycle_element)
+ def initialize(project:, element:)
super
- @element = life_cycle_element
+ @element = element
+ @project = project
+ end
+
+ def active_in_project?
+ project
+ .project_life_cycles
+ .detect { |project_lc| project_lc.life_cycle_id == element.id }
+ &.active
+ end
+
+ def toggle_aria_label
+ I18n.t("projects.settings.life_cycle.element.use_in_project", element: element.name)
end
private
diff --git a/app/components/projects/settings/life_cycles/index_component.html.erb b/app/components/projects/settings/life_cycles/index_component.html.erb
index 2cb0c2a50af..7f3fc67985d 100644
--- a/app/components/projects/settings/life_cycles/index_component.html.erb
+++ b/app/components/projects/settings/life_cycles/index_component.html.erb
@@ -17,10 +17,10 @@
render(Primer::Beta::Text.new(color: :subtle)) { t("projects.settings.life_cycle.non_defined") }
end
else
- life_cycle_elements.each do |life_cycle_element|
+ life_cycle_elements.each do |element|
component.with_row(data: { 'projects--settings--project-custom-fields-mapping-filter-target': 'searchItem' },
- test_selector: "project-life-cycle-element-#{life_cycle_element.id}") do
- render(Projects::Settings::LifeCycles::ElementComponent.new(life_cycle_element))
+ test_selector: "project-life-cycle-element-#{element.id}") do
+ render(Projects::Settings::LifeCycles::ElementComponent.new(project:, element:))
end
end
end
diff --git a/app/controllers/projects/settings/life_cycles_controller.rb b/app/controllers/projects/settings/life_cycles_controller.rb
index 6ac4af23cad..ff897746272 100644
--- a/app/controllers/projects/settings/life_cycles_controller.rb
+++ b/app/controllers/projects/settings/life_cycles_controller.rb
@@ -30,14 +30,34 @@ class Projects::Settings::LifeCyclesController < Projects::SettingsController
include OpTurbo::ComponentStream
before_action :load_life_cycle_elements, only: %i[show]
+ before_action :load_or_create_life_cycle_element, only: %i[toggle]
menu_item :settings_life_cycles
def show; end
+ def toggle
+ @life_cycle_element.toggle!(:active)
+ end
+
private
def load_life_cycle_elements
@life_cycle_elements = LifeCycle.all
end
+
+ def load_or_create_life_cycle_element
+ element_params = params.require(:project_life_cycle).permit(:life_cycle_id, :project_id, :type)
+
+ klass = case element_params.delete(:type)
+ when Stage.name
+ Project::Stage
+ when Gate.name
+ Project::Gate
+ else
+ raise NotImplementedError, "Unknown life cycle element type"
+ end
+
+ @life_cycle_element = klass.find_or_create_by(element_params)
+ end
end
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 2efe64416e0..4151672327f 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -135,7 +135,7 @@ Rails.application.reloader.to_prepare do
map.permission :select_project_life_cycle,
{
- "projects/settings/life_cycles": %i[show]
+ "projects/settings/life_cycles": %i[show toggle]
},
permissible_on: :project,
require: :member
diff --git a/config/locales/en.yml b/config/locales/en.yml
index b164afc1622..c9bc5bc2844 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -393,6 +393,8 @@ en:
title: "Project lifecycle"
description: 'These project stages and gates in the project lifecycle will be displayed in your <a href=%{overview_url} target="_blank">project overview page</a> and are defined in the <a href=%{admin_settings_url} target="_blank">administration settings</a> by the administrator of the instance. You can enable or disable individual attributes.'
non_defined: "Neither stages nor gates are currently defined."
+ element:
+ use_in_project: "Use %{element} in this project"
project_custom_fields:
header:
title: "Project attributes"
diff --git a/config/routes.rb b/config/routes.rb
index 07707700078..6ac9d0feb86 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -244,7 +244,11 @@ Rails.application.routes.draw do
put :disable_all_of_section
end
end
- resource :life_cycle, only: %i[show]
+ resource :life_cycle, only: %i[show] do
+ member do
+ post :toggle
+ end
+ end
resource :custom_fields, only: %i[show update]
resource :repository, only: %i[show], controller: "repository"
resource :versions, only: %i[show]
diff --git a/spec/features/projects/life_cycle/active_in_project_spec.rb b/spec/features/projects/life_cycle/active_in_project_spec.rb
index 94009b45dfa..63666b11a43 100644
--- a/spec/features/projects/life_cycle/active_in_project_spec.rb
+++ b/spec/features/projects/life_cycle/active_in_project_spec.rb
@@ -41,9 +41,9 @@ RSpec.describe "Projects life cycle settings", :js, :with_cuprite do
end
shared_let(:initiating_stage) { create(:stage, name: "Initiating") }
- shared_let(:read_to_execute_gate) { create(:gate, name: "Read to Execute") }
+ shared_let(:ready_to_execute_gate) { create(:gate, name: "Ready to Execute") }
shared_let(:executing_stage) { create(:stage, name: "Executing") }
- shared_let(:read_to_close_gate) { create(:gate, name: "Read to Close") }
+ shared_let(:ready_to_close_gate) { create(:gate, name: "Ready to Close") }
shared_let(:closing_stage) { create(:stage, name: "Closing") }
let(:project_lifecycle_page) { Pages::Projects::Settings::LifeCycle.new(project) }
@@ -54,11 +54,28 @@ RSpec.describe "Projects life cycle settings", :js, :with_cuprite do
it "allows toggling the active/inactive state of lifecycle elements" do
project_lifecycle_page.visit!
- project_lifecycle_page.expect_listed(initiating_stage,
- read_to_execute_gate,
- executing_stage,
- read_to_close_gate,
- closing_stage)
+ project_lifecycle_page.expect_listed(initiating_stage => false,
+ ready_to_execute_gate => false,
+ executing_stage => false,
+ ready_to_close_gate => false,
+ closing_stage => false)
+
+ # Activate the stages to be found within the project
+ project_lifecycle_page.toggle(initiating_stage)
+ project_lifecycle_page.toggle(ready_to_close_gate)
+ project_lifecycle_page.toggle(closing_stage)
+
+ wait_for_network_idle
+
+ # Expect the activation state to be kept after a reload
+ visit home_path
+ project_lifecycle_page.visit!
+
+ project_lifecycle_page.expect_listed(initiating_stage => true,
+ ready_to_execute_gate => false,
+ executing_stage => false,
+ ready_to_close_gate => true,
+ closing_stage => true)
end
end
end
diff --git a/spec/support/pages/projects/settings/life_cycle.rb b/spec/support/pages/projects/settings/life_cycle.rb
index 63e08eac7fe..07ddb2544d5 100644
--- a/spec/support/pages/projects/settings/life_cycle.rb
+++ b/spec/support/pages/projects/settings/life_cycle.rb
@@ -45,17 +45,44 @@ module Pages
"/projects/#{project.identifier}/settings/life_cycle"
end
- def expect_listed(*life_cycle_elements)
- raise "Currently depends on checking for at least two life cycle events" if life_cycle_elements.size < 2
+ # Checks if the life cycle elements are listed in the order given and with the correct toggle state.
+ # @param life_cycle_elements [Hash{LifeCycleElement => Boolean}]
+ def expect_listed(**life_cycle_elements)
+ if life_cycle_elements.size > 1
+ life_cycle_elements.each_cons(2) do |(predecessor, _), (successor, _)|
+ expect(page).to have_css("#{life_cycle_test_selector(predecessor)} + #{life_cycle_test_selector(successor)}")
+ end
+ end
- life_cycle_elements.each_cons(2) do |predecessor, successor|
- expect(page).to have_css("#{life_cycle_test_selector(predecessor)} + #{life_cycle_test_selector(successor)}")
+ life_cycle_elements.each do |element, active|
+ expect_toggle_state(element, active)
end
end
+ def expect_toggle_state(element, active)
+ within toggle_element(element) do
+ expect(page)
+ .to have_css(".ToggleSwitch-status#{expected_toggle_status(active)}"),
+ "Expected toggle for '#{element.name}' to be #{expected_toggle_status(active)} " \
+ "but was #{expected_toggle_status(!active)}"
+ end
+ end
+
+ def toggle(element)
+ toggle_element(element).click
+ end
+
def life_cycle_test_selector(element)
test_selector("project-life-cycle-element-#{element.id}")
end
+
+ def toggle_element(element)
+ find_test_selector("toggle-project-life-cycle-#{element.id}")
+ end
+
+ def expected_toggle_status(active)
+ active ? "On" : "Off"
+ end
end
end
end