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

[#64347] write/read formula, prepare validation

Tobias Dillmanned7c27c20823
diff --git a/app/components/admin/custom_fields/calculated_values/details_form.rb b/app/components/admin/custom_fields/calculated_values/details_form.rb
index d051e60cf89..79abd41c760 100644
--- a/app/components/admin/custom_fields/calculated_values/details_form.rb
+++ b/app/components/admin/custom_fields/calculated_values/details_form.rb
@@ -49,6 +49,7 @@ module Admin::CustomFields::CalculatedValues
details_form.text_field(
name: :formula,
+ value: model.formula_string,
label: I18n.t(:label_formula),
required: true,
caption: I18n.t("custom_fields.instructions.formula")
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index f95a61aac62..5f03bc3fddf 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -80,6 +80,7 @@ class CustomField < ApplicationRecord
validate :validate_default_value
validate :validate_regex
+ validate :validate_formula
validates :min_length, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :max_length, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
@@ -137,6 +138,15 @@ class CustomField < ApplicationRecord
errors.add(:regexp, :invalid)
end
+ def validate_formula
+ return unless field_format_calculated_value?
+
+ # TODO: add meaningful validation
+ if formula_string == "foo"
+ errors.add(:formula, :invalid)
+ end
+ end
+
def has_regexp?
regexp.present?
end
@@ -311,6 +321,19 @@ class CustomField < ApplicationRecord
field_format == "calculated_value"
end
+ def formula=(value)
+ if value.is_a?(String)
+ # TODO perform string parsing here
+ super({ formula: value })
+ else
+ super
+ end
+ end
+
+ def formula_string
+ formula.is_a?(Hash) ? formula["formula"] : formula
+ end
+
def multi_value_possible?
OpenProject::CustomFieldFormat.find_by(name: field_format)&.multi_value_possible?
end