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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#64348] Review feedback: increase code efficiency
Tobias Dillmann82ab7f8de635
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rbindex c286eae587b..2534eff9e58 100644--- a/app/models/custom_field/calculated_value.rb+++ b/app/models/custom_field/calculated_value.rb@@ -41,7 +41,6 @@ module CustomField::CalculatedValueincluded dovalidate :validate_formula, if: :field_format_calculated_value?- validate :validate_referenced_custom_fields_allowed, if: :field_format_calculated_value?def validate_formulaif formula_string.blank?@@ -50,6 +49,8 @@ module CustomField::CalculatedValueerrors.add(:formula, :invalid_characters)elsif !valid_formula_syntax?errors.add(:formula, :invalid)+ else+ validate_referenced_custom_fieldsendend@@ -77,7 +78,7 @@ module CustomField::CalculatedValueendend- def validate_referenced_custom_fields_allowed+ def validate_referenced_custom_fields# We can only validate used custom fields from a high-level perspective, since at this point in validation,# we do not have a project context to check against. So we cannot check if the custom fields are actually# enabled for a project and visible to a non-admin user.@@ -88,7 +89,7 @@ module CustomField::CalculatedValueif surplus_cfs.any?custom_field_names = CustomField.where(id: surplus_cfs).pluck(:name)- errors.add(:formula, :invalid_custom_fields, custom_fields: custom_field_names.join(", "))+ errors.add(:formula, :not_allowed_custom_fields_referenced, custom_fields: custom_field_names.join(", "))endend@@ -118,15 +119,15 @@ module CustomField::CalculatedValueallowed_chars = MATH_OPERATORS_FOR_FORMULA + [" "]allowed_tokens = /\A(\{\{cf_\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)).all? do |token|+ token.empty? || token.match?(allowed_tokens)endend# 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(/(?<=\{\{cf_)\d+(?=}})/).map(&:to_i)+ formula_str.scan(/(?<=\{\{cf_)\d+(?=}})/).map(&:to_i).uniqend# Returns `formula_string` with all custom field references detokenized so that they are parseable by Dentaku.@@ -136,18 +137,15 @@ module CustomField::CalculatedValueenddef has_circular_reference?(custom_field, original_id, visited = Set.new)- return true if visited.include?(custom_field.id)-- visited.add(custom_field.id)+ return true unless visited.add?(custom_field.id)if custom_field.field_format_calculated_value? && custom_field.formulareferenced_custom_fields = custom_field.formula.fetch("referenced_custom_fields", [])return true if referenced_custom_fields.include?(original_id)- referenced_custom_fields.any? do |ref_id|- referenced_field = ProjectCustomField.find_by(id: ref_id)- referenced_field && has_circular_reference?(referenced_field, original_id, visited)+ ProjectCustomField.where(id: referenced_custom_fields).any? do |referenced_field|+ has_circular_reference?(referenced_field, original_id, visited)endelsefalsediff --git a/config/locales/en.yml b/config/locales/en.ymlindex 4a1f5d5defe..d733fa16300 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1349,7 +1349,7 @@ en:inclusion_nested: "is not set to one of the allowed values at path '%{path}'."invalid: "is invalid."invalid_characters: "contains invalid characters."- invalid_custom_fields: "contains custom fields that are not allowed: %{custom_fields}."+ not_allowed_custom_fields_referenced: "contains custom fields that are not allowed: %{custom_fields}."invalid_url: "is not a valid URL."invalid_url_scheme: "is not a supported protocol (allowed: %{allowed_schemes})."less_than_or_equal_to: "must be less than or equal to %{count}."