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

make formula_str_without_patterns public

Ivan Kuchin2833108ac83b
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rb
index c92b041a8a1..e6179003892 100644
--- a/app/models/custom_field/calculated_value.rb
+++ b/app/models/custom_field/calculated_value.rb
@@ -114,6 +114,12 @@ module CustomField::CalculatedValue
end
end
+ # Returns `formula_string` with all custom field references detokenized so that they are parseable by Dentaku.
+ # For example, for `2 + {{cf_12}} + {{cf_4}}` it returns `2 + cf_12 + cf_4`.
+ def formula_str_without_patterns
+ formula_string.gsub(/\{\{(cf_\d+)}}/, '\1')
+ end
+
private
def calculator
@@ -150,11 +156,5 @@ module CustomField::CalculatedValue
def cf_ids_used_in_formula(formula_str)
formula_str.scan(/(?<=\{\{cf_)\d+(?=}})/).map(&:to_i).uniq
end
-
- # Returns `formula_string` with all custom field references detokenized so that they are parseable by Dentaku.
- # For example, for `2 + {{cf_12}} + {{cf_4}}` it returns `2 + cf_12 + cf_4`.
- def formula_str_without_patterns
- formula_string.gsub(/\{\{(cf_\d+)}}/, '\1')
- end
end
end
diff --git a/spec/models/custom_field/calculated_value_spec.rb b/spec/models/custom_field/calculated_value_spec.rb
index 41dec192a40..c8affe87086 100644
--- a/spec/models/custom_field/calculated_value_spec.rb
+++ b/spec/models/custom_field/calculated_value_spec.rb
@@ -175,6 +175,26 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_proje
end
end
+ describe "#formula_str_without_patterns" do
+ it "returns an empty string if no formula is set" do
+ subject.formula = nil
+
+ expect(subject.formula_str_without_patterns).to eq("")
+ end
+
+ it "returns the formula as is if formula doesn't reference custom fields" do
+ subject.formula = "2 + 2"
+
+ expect(subject.formula_str_without_patterns).to eq("2 + 2")
+ end
+
+ it "returns ids if formula references custom fields" do
+ subject.formula = "1 * {{cf_7}} + {{cf_42}}"
+
+ expect(subject.formula_str_without_patterns).to eq("1 * cf_7 + cf_42")
+ end
+ end
+
describe "#formula_referenced_custom_field_ids" do
it "returns an empty array if no formula is set" do
subject.formula = nil