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

[#64348] make operators a constant, i18n

Tobias Dillmann4f94f665409b
diff --git a/app/forms/custom_fields/calculated_values/details_form.rb b/app/forms/custom_fields/calculated_values/details_form.rb
index f1ccc918c15..83751de7595 100644
--- a/app/forms/custom_fields/calculated_values/details_form.rb
+++ b/app/forms/custom_fields/calculated_values/details_form.rb
@@ -80,16 +80,14 @@ module CustomFields
private
def formula_suggestions
- # TODO: move allowed operators to a constant of the model
- operators = %w[+ - * / ( )].map { |op| { key: op, label: op } }
+ operators = CustomField::CalculatedValue::OPERATORS.map { |op| { key: op, label: op } }
custom_fields = ProjectCustomField.where(field_format: %w[int float calculated_value]).map do |cf|
{ key: "cf_#{cf.id}", label: "#{cf.name} (#{helpers.label_for_custom_field_format(cf.field_format)})" }
end
- # TODO: localize titles
{
- custom_fields: { title: "Custom Fields", tokens: custom_fields },
- operators: { title: "Operators", tokens: operators }
+ custom_fields: { title: I18n.t("label_custom_field_plural"), tokens: custom_fields },
+ operators: { title: I18n.t("label_mathematical_operators"), tokens: operators }
}
end
end
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rb
index ef607f226f4..36835bb4efb 100644
--- a/app/models/custom_field/calculated_value.rb
+++ b/app/models/custom_field/calculated_value.rb
@@ -33,6 +33,8 @@
module CustomField::CalculatedValue
extend ActiveSupport::Concern
+ OPERATORS = %w[+ - * / ( )].freeze
+
included do
validate :validate_formula
@@ -92,14 +94,14 @@ module CustomField::CalculatedValue
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
+ # The allowed characters are:
+ # Our mathematical operators, whitespace, digits and decimal points
# Additionally, the formula may contain references to custom fields in the form of `cf_123` or `{{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_chars = OPERATORS + [" "]
allowed_tokens = /\A(cf_\d+|\{\{cf_\d+}}|\d+\.?\d*|\.\d+)\z/
formula_string.split(Regexp.union(allowed_chars)).reject(&:empty?).all? do |token|
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 75a592c9e02..9aa2b59506d 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -3039,6 +3039,7 @@ en:
label_manage: "Manage"
label_manage_groups: "Manage groups"
label_managed_repositories_vendor: "Managed %{vendor} repositories"
+ label_mathematical_operators: "Mathematical operators"
label_max_size: "Maximum size"
label_me: "me"
label_member_new: "New member"