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

Timestamp: extracting redundant code into method

Sebastian Fiedlschusterdebc37df078b
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index b5c37fc9589..7ce855c217f 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -150,10 +150,10 @@ class Timestamp
# map -1y to P-1Y, -1m to P-1M, -1w to P-1W, -1d to P-1D
# map -1y1d to P-1Y-1D
sign = "-" if string.start_with? "-"
- years = string.scan(/(\d+)y/).flatten.first
- months = string.scan(/(\d+)m/).flatten.first
- weeks = string.scan(/(\d+)w/).flatten.first
- days = string.scan(/(\d+)d/).flatten.first
+ years = scan_for_shortcut_value(string:, unit: "y")
+ months = scan_for_shortcut_value(string:, unit: "m")
+ weeks = scan_for_shortcut_value(string:, unit: "w")
+ days = scan_for_shortcut_value(string:, unit: "d")
if years || months || weeks || days
string = "P" \
"#{sign if years}#{years}#{'Y' if years}" \
@@ -166,4 +166,9 @@ class Timestamp
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/PerceivedComplexity
+
+ def self.scan_for_shortcut_value(string:, unit:)
+ string.scan(/(\d+)#{unit}/).flatten.first
+ end
+
end