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

Activate associated validations for project life cycle steps only when validating via the ProjectLifeCycleSteps contracts.

Dombi Attila851f5044474d
diff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rb
index efa06281f33..35cdf5b58c6 100644
--- a/app/contracts/model_contract.rb
+++ b/app/contracts/model_contract.rb
@@ -48,7 +48,7 @@ class ModelContract < BaseContract
# This of course is only true if that contract validates the model and
# if the model has an errors object.
def valid?(context = nil)
- model.valid? if validate_model?
+ model.valid?(context) if validate_model?
contract_valid?(context, clear_errors: !validate_model?)
end
@@ -61,7 +61,8 @@ class ModelContract < BaseContract
# Clearing would then be done in the #valid? method by calling model.valid?
# * Checks for readonly attributes being changed
def contract_valid?(context = nil, clear_errors: false)
- current_context, self.validation_context = validation_context, context # rubocop:disable Style/ParallelAssignment
+ current_context = validation_context
+ self.validation_context = context
errors.clear if clear_errors
diff --git a/app/contracts/project_life_cycle_steps/base_contract.rb b/app/contracts/project_life_cycle_steps/base_contract.rb
index bb0e43a0227..a8289ceb27a 100644
--- a/app/contracts/project_life_cycle_steps/base_contract.rb
+++ b/app/contracts/project_life_cycle_steps/base_contract.rb
@@ -31,6 +31,10 @@ module ProjectLifeCycleSteps
validate :select_custom_fields_permission
validate :consecutive_steps_have_increasing_dates
+ def valid?(context = :saving_life_cycle_steps)
+ super
+ end
+
def select_custom_fields_permission
return if user.allowed_in_project?(:edit_project_stages_and_gates, model)
diff --git a/app/models/project.rb b/app/models/project.rb
index 6226b48729c..5694839fb13 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -92,7 +92,9 @@ class Project < ApplicationRecord
class_name: "Project::LifeCycleStep",
inverse_of: :project,
dependent: :destroy
+
accepts_nested_attributes_for :available_life_cycle_steps
+ validates_associated :available_life_cycle_steps, on: :saving_life_cycle_steps
store_attribute :settings, :deactivate_work_package_attachments, :boolean
diff --git a/app/models/project/life_cycle_step.rb b/app/models/project/life_cycle_step.rb
index 9a6b26a6e35..30fbd04ece8 100644
--- a/app/models/project/life_cycle_step.rb
+++ b/app/models/project/life_cycle_step.rb
@@ -27,7 +27,7 @@
#++
class Project::LifeCycleStep < ApplicationRecord
- belongs_to :project, optional: false
+ belongs_to :project, optional: false, inverse_of: :available_life_cycle_steps
belongs_to :definition,
optional: false,
class_name: "Project::LifeCycleStepDefinition"
diff --git a/spec/contracts/project_life_cycle_steps/base_contract_spec.rb b/spec/contracts/project_life_cycle_steps/base_contract_spec.rb
index 63b69e43c33..2646b6a1df5 100644
--- a/spec/contracts/project_life_cycle_steps/base_contract_spec.rb
+++ b/spec/contracts/project_life_cycle_steps/base_contract_spec.rb
@@ -133,6 +133,13 @@ RSpec.describe ProjectLifeCycleSteps::BaseContract do
end
end
end
+
+ describe "triggering validations on the model" do
+ it "sets the :saving_life_cycle_steps validation context" do
+ contract.validate
+ expect(project).to have_receive(:valid?).with(:saving_life_cycle_steps)
+ end
+ end
end
end
diff --git a/spec/factories/project_life_cycle_step_factory.rb b/spec/factories/project_life_cycle_step_factory.rb
index cc64e09b02e..465dd702bfc 100644
--- a/spec/factories/project_life_cycle_step_factory.rb
+++ b/spec/factories/project_life_cycle_step_factory.rb
@@ -31,6 +31,10 @@ FactoryBot.define do
project
active { true }
+ trait :skip_validate do
+ to_create { |instance| instance.save(validate: false) }
+ end
+
factory :project_stage, class: "Project::Stage" do
definition factory: :project_stage_definition
start_date { Date.current - 2.days }
diff --git a/spec/lib/api/contracts/model_contract_spec.rb b/spec/lib/api/contracts/model_contract_spec.rb
index dc47b35544b..84c8baaf405 100644
--- a/spec/lib/api/contracts/model_contract_spec.rb
+++ b/spec/lib/api/contracts/model_contract_spec.rb
@@ -149,8 +149,7 @@ RSpec.describe ModelContract do
context "when the model extends both modules" do
before do
- allow(model).to receive(:changed_by_user).and_return([:custom_field1])
- allow(model).to receive(:changed_with_custom_fields).and_return([:no_allowed])
+ allow(model).to receive_messages(changed_by_user: [:custom_field1], changed_with_custom_fields: [:no_allowed])
end
it "adds an error to the custom field attribute from the OpenProject::ChangedBySystem module" do
@@ -159,5 +158,12 @@ RSpec.describe ModelContract do
.to include(:error_readonly)
end
end
+
+ context "when a context is provided" do
+ it "propagates the contex to the model as well" do
+ model_contract.valid?(:custom_context)
+ expect(model).to have_receive(:valid?).with(:custom_context)
+ end
+ end
end
end
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 2f191a053f3..0a446f85d63 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -383,6 +383,32 @@ RSpec.describe Project do
describe "life_cycles" do
it { is_expected.to have_many(:life_cycle_steps).class_name("Project::LifeCycleStep").dependent(:destroy) }
+
+ it "has many available_life_cycle_steps" do
+ expect(subject).to have_many(:available_life_cycle_steps)
+ .class_name("Project::LifeCycleStep")
+ .inverse_of(:project)
+ .dependent(:destroy)
+ .conditions(active: true)
+ .order(position: :asc)
+ end
+
+ it "eager loads :definition" do
+ expect(subject.available_life_cycle_steps.to_sql)
+ .to include("LEFT OUTER JOIN \"project_life_cycle_step_definitions\" ON")
+ end
+
+ describe ".validates_associated" do
+ let!(:project_stage) { create :project_stage, :skip_validate, project:, start_date: nil, end_date: nil }
+
+ it "is valid without a validation context" do
+ expect(project).to be_valid
+ end
+
+ it "is invalid with the :saving_life_cycle_steps validation context" do
+ expect(project).not_to be_valid(:saving_life_cycle_steps)
+ end
+ end
end
describe "#enabled_module_names=", with_settings: { default_projects_modules: %w(work_package_tracking repository) } do