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

[#66093] Make CalculatedValueError polymorphic

Tobias Dillmann67215388541c
diff --git a/app/components/projects/row_component.rb b/app/components/projects/row_component.rb
index 8b899a69fcc..ffaa8a69cb4 100644
--- a/app/components/projects/row_component.rb
+++ b/app/components/projects/row_component.rb
@@ -103,7 +103,7 @@ module Projects
end
def render_calculated_value(custom_field, custom_value)
- if (error = custom_field.first_calculation_error(project:))
+ if (error = custom_field.first_calculation_error(project))
render(Primer::Alpha::Dialog.new(title: I18n.t("calculated_values.error_dialog.title"),
data: {
test_selector: "calculated-value-error-dialog-#{custom_field.id}"
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rb
index 8e94d006635..61414d2d6c2 100644
--- a/app/models/acts_as_customizable/calculated_value.rb
+++ b/app/models/acts_as_customizable/calculated_value.rb
@@ -130,7 +130,7 @@ module ActsAsCustomizable::CalculatedValue
end
def create_calculated_value_error(custom_field_id, error_code, missing_custom_field_ids = [])
- CalculatedValueError.create(project: self, custom_field_id:, error_code:, missing_custom_field_ids:)
+ CalculatedValueError.create(customized: self, custom_field_id:, error_code:, missing_custom_field_ids:)
end
def create_errors_for_missing_attributes(referenced_values, calculated_fields, result)
@@ -164,7 +164,7 @@ module ActsAsCustomizable::CalculatedValue
def remove_calculated_value_errors(custom_field_ids = [])
return if custom_field_ids.empty?
- CalculatedValueError.where(project: self, custom_field_id: custom_field_ids).delete_all
+ CalculatedValueError.where(customized: self, custom_field_id: custom_field_ids).delete_all
end
# Creates an error if a value that is directly required for the calculation is missing.
diff --git a/app/models/calculated_value_error.rb b/app/models/calculated_value_error.rb
index 9dc6d6c56c7..0fd5b481190 100644
--- a/app/models/calculated_value_error.rb
+++ b/app/models/calculated_value_error.rb
@@ -29,18 +29,18 @@
#++
class CalculatedValueError < ApplicationRecord
- belongs_to :project
+ belongs_to :customized, polymorphic: true
belongs_to :custom_field
VALID_ERROR_CODES = %w[ERROR_MATHEMATICAL
ERROR_MISSING_VALUE
ERROR_DISABLED_VALUE].freeze
- validates :project_id, presence: true
+ validates :customized, presence: true
validates :custom_field_id, presence: true
validates :error_code, inclusion: { in: VALID_ERROR_CODES }
# It makes no sense to have the exact same error multiple times.
- validates :project_id, uniqueness: { scope: %i[custom_field_id error_code] }
+ validates :customized_type, uniqueness: { scope: %i[customized_id custom_field_id error_code] }
end
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 81b8478f245..1355f8fee0f 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -353,10 +353,10 @@ class CustomField < ApplicationRecord
#
# When there is at least one calculation error, will return the first one - or `nil` if there are none.
# Use this method when you want to present a calculation error to the user.
- def first_calculation_error(project:)
+ def first_calculation_error(customized)
return nil unless calculated_value?
- calculated_value_errors.where(project:).first
+ calculated_value_errors.where(customized:).first
end
private
diff --git a/app/models/project.rb b/app/models/project.rb
index c995f029077..3159e5b6cb2 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -68,7 +68,7 @@ class Project < ApplicationRecord
class_name: "Member"
has_many :users, through: :members, source: :principal
has_many :principals, through: :member_principals, source: :principal
- has_many :calculated_value_errors, dependent: :delete_all, inverse_of: :project
+ has_many :calculated_value_errors, dependent: :delete_all, as: :customized
has_many :enabled_modules, dependent: :delete_all
has_and_belongs_to_many :types, -> {
diff --git a/config/locales/en.yml b/config/locales/en.yml
index fe909f6c87b..827381032e8 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1063,6 +1063,9 @@ en:
scim_clients: "SCIM clients"
calculated_value_error:
error_code: "Error code"
+ customized: "Customized"
+ customized_id: "Customized ID"
+ customized_type: "Customized type"
capability:
context: "Context"
changeset:
diff --git a/db/migrate/20250923135048_convert_project_to_customized_in_calculated_value_errors.rb b/db/migrate/20250923135048_convert_project_to_customized_in_calculated_value_errors.rb
new file mode 100644
index 00000000000..0b14920b5f1
--- /dev/null
+++ b/db/migrate/20250923135048_convert_project_to_customized_in_calculated_value_errors.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+class ConvertProjectToCustomizedInCalculatedValueErrors < ActiveRecord::Migration[8.0]
+ def up
+ rename_column :calculated_value_errors, :project_id, :customized_id
+ add_column :calculated_value_errors, :customized_type, :string, null: false
+
+ CalculatedValueError.reset_column_information # Reload schema
+ CalculatedValueError.update_all(customized_type: "Project")
+
+ remove_index :calculated_value_errors, name: "index_unique_calc_val_error_per_project_and_cf"
+
+ add_index :calculated_value_errors,
+ %i[customized_type customized_id custom_field_id error_code],
+ unique: true,
+ name: "index_unique_calc_val_error_per_customized_and_cf"
+ end
+
+ def down
+ rename_column :calculated_value_errors, :customized_id, :project_id
+ remove_column :calculated_value_errors, :customized_type
+
+ remove_index :calculated_value_errors, name: "index_unique_calc_val_error_per_project_and_cf"
+
+ add_index :calculated_value_errors,
+ %i[project_id custom_field_id error_code],
+ unique: true,
+ name: "index_unique_calc_val_error_per_project_and_cf"
+ end
+end
diff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rb
index ebb71427ffc..b8f645f0e6a 100644
--- a/lib/api/v3/utilities/custom_field_injector.rb
+++ b/lib/api/v3/utilities/custom_field_injector.rb
@@ -312,7 +312,7 @@ module API
next unless custom_field.calculated_value?
next unless available_custom_fields.include?(custom_field)
- errors = custom_field.calculated_value_errors.where(project_id: id)
+ errors = custom_field.calculated_value_errors.where(customized_id: id, customized_type: "Project")
errors.map do |err|
{ error_code: err.error_code,
error_message: CalculatedValues::ErrorsHelper.calculated_value_error_msg(err) }
diff --git a/modules/overviews/app/components/overviews/project_custom_fields/item_component.rb b/modules/overviews/app/components/overviews/project_custom_fields/item_component.rb
index 508dd068422..42a4c744760 100644
--- a/modules/overviews/app/components/overviews/project_custom_fields/item_component.rb
+++ b/modules/overviews/app/components/overviews/project_custom_fields/item_component.rb
@@ -51,11 +51,11 @@ module Overviews
end
def calculation_error?
- @project_custom_field.first_calculation_error(project: @project).present?
+ @project_custom_field.first_calculation_error(@project).present?
end
def render_calculation_error
- error = @project_custom_field.first_calculation_error(project: @project)
+ error = @project_custom_field.first_calculation_error(@project)
render(Primer::OpenProject::FlexLayout.new(align_items: :flex_start,
data: {
diff --git a/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb b/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
index 08e05476932..5d813b77b20 100644
--- a/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
@@ -599,7 +599,7 @@ RSpec.describe "Show project custom fields on project overview page", :js do
calculated_from_int_and_float_project_custom_field.custom_values.where(customized: project).first.update!(value: "")
calculated_from_int_and_float_project_custom_field
- .calculated_value_errors.create!(project_id: project.id, error_code: "ERROR_MATHEMATICAL")
+ .calculated_value_errors.create!(customized: project, error_code: "ERROR_MATHEMATICAL")
end
it "shows the error message for the project custom field" do
diff --git a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
index 4805b3c935c..38afd06c5fc 100644
--- a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
@@ -220,13 +220,13 @@ RSpec.describe API::V3::Projects::ProjectRepresenter, "rendering" do
let(:cf_path) { "customField#{calculated_value_custom_field.id}errors" }
let!(:calculated_value_error) do
- build_stubbed(:calculated_value_error, custom_field: calculated_value_custom_field, project: project)
+ build_stubbed(:calculated_value_error, custom_field: calculated_value_custom_field, customized: project)
end
before do
value_errors_double = double
allow(value_errors_double).to receive(:where)
- .with(project_id: project.id)
+ .with(customized_id: project.id, customized_type: "Project")
.and_return([calculated_value_error])
allow(calculated_value_custom_field).to receive(:calculated_value_errors)
diff --git a/spec/models/acts_as_customizable/calculated_value_spec.rb b/spec/models/acts_as_customizable/calculated_value_spec.rb
index dec1cde630c..bff5881d7f0 100644
--- a/spec/models/acts_as_customizable/calculated_value_spec.rb
+++ b/spec/models/acts_as_customizable/calculated_value_spec.rb
@@ -385,7 +385,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
describe "#calculate_custom_fields error handling" do
def expect_calculated_value_error(calc_val_cf, project, error_code, message_part = nil)
- error = calc_val_cf.first_calculation_error(project:)
+ error = calc_val_cf.first_calculation_error(project)
expect(error&.error_code).to eq(error_code)
if message_part
@@ -413,9 +413,9 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
it "creates a mathematical error for division by zero" do
project.calculate_custom_fields([cf_div, cf_add])
- expect(cf_add.calculated_value_errors.where(project:)).to be_empty
+ expect(cf_add.calculated_value_errors.where(customized: project)).to be_empty
- errors = cf_div.calculated_value_errors.where(project:)
+ errors = cf_div.calculated_value_errors.where(customized: project)
expect(errors.size).to eq(1)
expect(errors.first&.error_code).to eq("ERROR_MATHEMATICAL")
end
@@ -436,7 +436,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
it "creates a missing value error" do
project.calculate_custom_fields([cv1, cv2, cv3])
- expect(cv3.calculated_value_errors.where(project:)).to be_empty
+ expect(cv3.calculated_value_errors.where(customized: project)).to be_empty
expect_calculated_value_error(cv1, project, "ERROR_MISSING_VALUE", cf_int.name)
expect_calculated_value_error(cv2, project, "ERROR_MISSING_VALUE", cv1.name)
@@ -458,7 +458,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
it "creates a disabled value error" do
project.calculate_custom_fields([cv1, cv2, cv3])
- expect(cv3.first_calculation_error(project:)).to be_blank
+ expect(cv3.first_calculation_error(project)).to be_blank
# The referenced int field is disabled, we thus expect a `disabled` error.
expect_calculated_value_error(cv1, project, "ERROR_DISABLED_VALUE", cf_int.name)
@@ -484,7 +484,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
project.calculate_custom_fields([disabled_cv, enabled_cv])
expect_calculated_value_error(enabled_cv, project, "ERROR_MATHEMATICAL")
- expect(disabled_cv.first_calculation_error(project:)).to be_blank
+ expect(disabled_cv.first_calculation_error(project)).to be_blank
end
end
@@ -518,9 +518,9 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
nested_calculation.id => "-3.0"
})
- expect(a_plus_b.first_calculation_error(project:)).to be_blank
- expect(a_minus_b.first_calculation_error(project:)).to be_blank
- expect(nested_calculation.first_calculation_error(project:)).to be_blank
+ expect(a_plus_b.first_calculation_error(project)).to be_blank
+ expect(a_minus_b.first_calculation_error(project)).to be_blank
+ expect(nested_calculation.first_calculation_error(project)).to be_blank
end
describe "with a_minus_b being disabled" do
@@ -535,7 +535,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
it "produces an error for the disabled field" do
project.calculate_custom_fields([a_plus_b, a_minus_b, nested_calculation])
- expect(a_plus_b.first_calculation_error(project:)).to be_blank
+ expect(a_plus_b.first_calculation_error(project)).to be_blank
expect_calculated_value_error(nested_calculation, project, "ERROR_DISABLED_VALUE", a_minus_b.name)
end
end
@@ -552,7 +552,7 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
it "produces an error for the disabled field" do
project.calculate_custom_fields([a_plus_b, a_minus_b, nested_calculation])
- expect(a_minus_b.first_calculation_error(project:)).to be_blank
+ expect(a_minus_b.first_calculation_error(project)).to be_blank
expect_calculated_value_error(nested_calculation, project, "ERROR_DISABLED_VALUE", a_plus_b.name)
end
end
@@ -565,8 +565,8 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valu
it "produces a mathematical error" do
project.calculate_custom_fields([a_plus_b, a_minus_b, nested_calculation])
- expect(a_plus_b.first_calculation_error(project:)).to be_blank
- expect(a_minus_b.first_calculation_error(project:)).to be_blank
+ expect(a_plus_b.first_calculation_error(project)).to be_blank
+ expect(a_minus_b.first_calculation_error(project)).to be_blank
expect_calculated_value_error(nested_calculation, project, "ERROR_MATHEMATICAL")
end
end
diff --git a/spec/models/calculated_value_error_spec.rb b/spec/models/calculated_value_error_spec.rb
index 594dc8950cc..71a5b2014ff 100644
--- a/spec/models/calculated_value_error_spec.rb
+++ b/spec/models/calculated_value_error_spec.rb
@@ -33,22 +33,35 @@ require "spec_helper"
RSpec.describe CalculatedValueError do
shared_let(:project) { create(:project) }
shared_let(:custom_field) { create(:custom_field) }
- shared_let(:other_custom_field) { create(:custom_field) }
describe "validations" do
- it "validates project_id is present" do
- expect(subject).to validate_presence_of(:project_id)
+ it "validates customized_id and customized_type is present" do
+ expect(subject).to validate_presence_of(:customized)
end
it "validates custom_field_id is present" do
expect(subject).to validate_presence_of(:custom_field_id)
end
- it "validates uniqueness of project_id scoped to custom_field_id and error_code" do
- subject.project = project
- subject.custom_field = custom_field
- subject.error_code = "ERROR_MATHEMATICAL"
- expect(subject).to validate_uniqueness_of(:project_id).scoped_to(%i[custom_field_id error_code])
+ it "validates uniqueness of customized scoped to custom_field_id and error_code" do
+ # Create an initial record
+ create(
+ :calculated_value_error,
+ customized: project,
+ custom_field: custom_field,
+ error_code: "ERROR_MATHEMATICAL"
+ )
+
+ # Try to create a duplicate record
+ duplicate = build(
+ :calculated_value_error,
+ customized: project,
+ custom_field: custom_field,
+ error_code: "ERROR_MATHEMATICAL"
+ )
+
+ expect(duplicate).not_to be_valid
+ expect(duplicate.errors[:customized_type]).to include("has already been taken.")
end
it "validates the error_code is one of the allowed values" do