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] use concern helpers

Tobias Dillmann3e4b8cb4c1f5
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index ce71762809a..2e4c6ba2230 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -73,7 +73,6 @@ class CustomField < ApplicationRecord
validate :validate_field_format_inclusion
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 }
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rb
index 64f2d4753d1..ffe933e6e45 100644
--- a/app/models/custom_field/calculated_value.rb
+++ b/app/models/custom_field/calculated_value.rb
@@ -31,65 +31,71 @@
# Methods for custom fields with a format of "calculated value".
# Should be included in the CustomField model.
module CustomField::CalculatedValue
- def validate_formula
- return unless field_format_calculated_value?
+ extend ActiveSupport::Concern
- if formula_string.blank?
- errors.add(:formula, :blank)
- return
- end
+ included do
+ validate :validate_formula
- unless formula_contains_only_allowed_characters?
- errors.add(:formula, :invalid_characters)
- return
- end
+ def validate_formula
+ return unless field_format_calculated_value?
- # WP-64348: check for valid (i.e., visible & enabled) custom field references (see #cf_ids_used_in_formula)
+ if formula_string.blank?
+ errors.add(:formula, :blank)
+ return
+ end
- # Dentaku will return nil if the formula is invalid.
- # TODO WP-64348: add support for referenced custom fields by injecting them as variables,
- # e.g. calculator.evaluate(formula_string, cf_123: CustomField.find(123).value)
- errors.add(:formula, :invalid) unless Dentaku::Calculator.new.evaluate(formula_string)
+ unless formula_contains_only_allowed_characters?
+ errors.add(:formula, :invalid_characters)
+ return
+ end
- # TODO: consider differentiating between a formula that contains missing variables, invalid
- # syntax, or mathematical errors.
- end
+ # WP-64348: check for valid (i.e., visible & enabled) custom field references (see #cf_ids_used_in_formula)
- def formula=(value)
- if value.is_a?(String)
- super({ formula: value, referenced_custom_fields: cf_ids_used_in_formula(value) })
- else
- super
+ # Dentaku will return nil if the formula is invalid.
+ # TODO WP-64348: add support for referenced custom fields by injecting them as variables,
+ # e.g. calculator.evaluate(formula_string, cf_123: CustomField.find(123).value)
+ errors.add(:formula, :invalid) unless Dentaku::Calculator.new.evaluate(formula_string)
+
+ # TODO: consider differentiating between a formula that contains missing variables, invalid
+ # syntax, or mathematical errors.
end
- end
- # Returns the formula as a string. Will return an empty string if the formula is not set.
- def formula_string
- formula ? formula.fetch("formula", "") : ""
- end
+ def formula=(value)
+ if value.is_a?(String)
+ super({ formula: value, referenced_custom_fields: cf_ids_used_in_formula(value) })
+ else
+ super
+ end
+ end
+
+ # Returns the formula as a string. Will return an empty string if the formula is not set.
+ def formula_string
+ formula ? formula.fetch("formula", "") : ""
+ end
- private
+ private
- def formula_contains_only_allowed_characters?
- # List of allowed characters in a formula. This only performs a very basic validation.
- # Allowed characters are:
- # + - / * ( ) whitespace digits and decimal points
- # Additionally, the formula may contain references to custom fields in the form of `cf_123` where 123 is the ID of
- # the custom field.
- # Once this basic validation passes, the formula will be parsed and validated by Dentaku, which builds an AST
- # and ensures that the formula is really valid. A welcome side effect of the basic validation done here is that
- # it prevents built-in functions from being used in the formula, which we do not want to allow.
- allowed_chars = %w[+ - / * ( )] + [" "]
- allowed_tokens = /\A(cf_\d+|\d+\.?\d*|\.\d+)\z/
+ def formula_contains_only_allowed_characters?
+ # List of allowed characters in a formula. This only performs a very basic validation.
+ # Allowed characters are:
+ # + - / * ( ) whitespace digits and decimal points
+ # Additionally, the formula may contain references to custom fields in the form of `cf_123` where 123 is the ID of
+ # the custom field.
+ # Once this basic validation passes, the formula will be parsed and validated by Dentaku, which builds an AST
+ # and ensures that the formula is really valid. A welcome side effect of the basic validation done here is that
+ # it prevents built-in functions from being used in the formula, which we do not want to allow.
+ allowed_chars = %w[+ - / * ( )] + [" "]
+ allowed_tokens = /\A(cf_\d+|\d+\.?\d*|\.\d+)\z/
- formula_string.split(Regexp.union(allowed_chars)).reject(&:empty?).all? do |token|
- token.match?(allowed_tokens)
+ formula_string.split(Regexp.union(allowed_chars)).reject(&:empty?).all? do |token|
+ token.match?(allowed_tokens)
+ end
end
- end
- # Returns a list of custom field IDs used in the formula.
- # For a formula like `2 + cf_12 + cf_4` it returns `[12, 4]`.
- def cf_ids_used_in_formula(formula_str)
- formula_str.scan(/\bcf_(\d+)\b/).flatten.map(&:to_i)
+ # Returns a list of custom field IDs used in the formula.
+ # For a formula like `2 + cf_12 + cf_4` it returns `[12, 4]`.
+ def cf_ids_used_in_formula(formula_str)
+ formula_str.scan(/\bcf_(\d+)\b/).flatten.map(&:to_i)
+ end
end
end