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

simplify variable handling of definition_id between view and controller

ulferts16d5bd943c68
diff --git a/app/components/projects/settings/life_cycles/step_component.html.erb b/app/components/projects/settings/life_cycles/step_component.html.erb
index 46e2899ecd5..d8bf4ebdd55 100644
--- a/app/components/projects/settings/life_cycles/step_component.html.erb
+++ b/app/components/projects/settings/life_cycles/step_component.html.erb
@@ -16,12 +16,7 @@
# 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: {
- definition_id: definition.id,
- type: definition.type
- }
- ),
+ src: toggle_project_settings_life_cycle_path(definition_id: definition.id),
csrf_token: form_authenticity_token,
data: { test_selector: "toggle-project-life-cycle-#{definition.id}" },
aria: { label: toggle_aria_label },
diff --git a/app/controllers/projects/settings/life_cycles_controller.rb b/app/controllers/projects/settings/life_cycles_controller.rb
index e7dcf078a76..a9e0a4cb3a7 100644
--- a/app/controllers/projects/settings/life_cycles_controller.rb
+++ b/app/controllers/projects/settings/life_cycles_controller.rb
@@ -38,24 +38,19 @@ class Projects::Settings::LifeCyclesController < Projects::SettingsController
def show; end
def toggle
- step_params = params
- .require(:project_life_cycle)
- .permit(:definition_id, :type)
- .to_h
- .symbolize_keys
- .merge(active: params["value"] == "1")
+ definition = Project::LifeCycleStepDefinition.where(id: params[:definition_id])
- upsert_one_step(**step_params)
+ upsert_steps(definition, active: params["value"])
end
def disable_all
- upsert_all_steps(active: false)
+ upsert_steps(@life_cycle_definitions, active: false)
redirect_to action: :show
end
def enable_all
- upsert_all_steps(active: true)
+ upsert_steps(@life_cycle_definitions, active: true)
redirect_to action: :show
end
@@ -70,45 +65,17 @@ class Projects::Settings::LifeCyclesController < Projects::SettingsController
deny_access unless OpenProject::FeatureDecisions.stages_and_gates_active?
end
- def upsert_one_step(definition_id:, type:, active: true)
- upsert_all(
- [{
- project_id: @project.id,
- definition_id:,
- active:,
- type: project_type_for_definition_type(type)
- }]
- )
- end
-
- def upsert_all_steps(active: true)
- upsert_all(
- @life_cycle_definitions.map do |definition|
+ def upsert_steps(definitions, active:)
+ Project::LifeCycleStep.upsert_all(
+ definitions.map do |definition|
{
project_id: @project.id,
definition_id: definition.id,
active:,
- type: project_type_for_definition_type(definition.type)
+ type: definition.step_class
}
- end
- )
- end
-
- def upsert_all(upserted_steps)
- Project::LifeCycleStep.upsert_all(
- upserted_steps,
+ end,
unique_by: %i[project_id definition_id]
)
end
-
- def project_type_for_definition_type(definition_type)
- case definition_type
- when Project::StageDefinition.name
- Project::Stage
- when Project::GateDefinition.name
- Project::Gate
- else
- raise NotImplementedError, "Unknown life cycle definition type"
- end
- end
end
diff --git a/app/models/project/gate_definition.rb b/app/models/project/gate_definition.rb
index a5156107572..a0cfef69352 100644
--- a/app/models/project/gate_definition.rb
+++ b/app/models/project/gate_definition.rb
@@ -32,4 +32,8 @@ class Project::GateDefinition < Project::LifeCycleStepDefinition
foreign_key: :definition_id,
inverse_of: :definition,
dependent: :destroy
+
+ def step_class
+ Project::Gate
+ end
end
diff --git a/app/models/project/life_cycle_step_definition.rb b/app/models/project/life_cycle_step_definition.rb
index d511cc284cc..48e8e00d94b 100644
--- a/app/models/project/life_cycle_step_definition.rb
+++ b/app/models/project/life_cycle_step_definition.rb
@@ -51,4 +51,8 @@ class Project::LifeCycleStepDefinition < ApplicationRecord
super
end
+
+ def step_class
+ raise NotImplementedError
+ end
end
diff --git a/app/models/project/stage_definition.rb b/app/models/project/stage_definition.rb
index 91ae98584de..f373d13cd64 100644
--- a/app/models/project/stage_definition.rb
+++ b/app/models/project/stage_definition.rb
@@ -32,4 +32,8 @@ class Project::StageDefinition < Project::LifeCycleStepDefinition
foreign_key: :definition_id,
inverse_of: :definition,
dependent: :destroy
+
+ def step_class
+ Project::Stage
+ end
end
diff --git a/spec/models/project/gate_definition_spec.rb b/spec/models/project/gate_definition_spec.rb
index 7d69be5a91e..f943a416d9b 100644
--- a/spec/models/project/gate_definition_spec.rb
+++ b/spec/models/project/gate_definition_spec.rb
@@ -40,4 +40,10 @@ RSpec.describe Project::GateDefinition do
.dependent(:destroy)
end
end
+
+ describe "#step_class" do
+ it "returns Project::Stage" do
+ expect(subject.step_class).to eq(Project::Gate)
+ end
+ end
end
diff --git a/spec/models/project/stage_definition_spec.rb b/spec/models/project/stage_definition_spec.rb
index f642056d4a3..1d94467a546 100644
--- a/spec/models/project/stage_definition_spec.rb
+++ b/spec/models/project/stage_definition_spec.rb
@@ -40,4 +40,10 @@ RSpec.describe Project::StageDefinition do
.dependent(:destroy)
end
end
+
+ describe "#step_class" do
+ it "returns Project::Stage" do
+ expect(subject.step_class).to eq(Project::Stage)
+ end
+ end
end