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
Assume next logical unit when parsing a string
Aaron Contrerasa37eb72a4094
diff --git a/app/services/duration_converter.rb b/app/services/duration_converter.rbindex 921a4784c44..6e632812a1b 100644--- a/app/services/duration_converter.rb+++ b/app/services/duration_converter.rb@@ -34,11 +34,71 @@require "bigdecimal"class DurationConverter+ UNIT_ABBREVIATION_MAP = {+ "seconds" => "seconds",+ "second" => "seconds",+ "secs" => "seconds",+ "sec" => "seconds",+ "s" => "seconds",+ "minutes" => "minutes",+ "minute" => "minutes",+ "mins" => "minutes",+ "min" => "minutes",+ "m" => "minutes",+ "hours" => "hours",+ "hour" => "hours",+ "hrs" => "hours",+ "hr" => "hours",+ "h" => "hours",+ "days" => "days",+ "day" => "days",+ "dy" => "days",+ "d" => "days",+ "weeks" => "weeks",+ "week" => "weeks",+ "wks" => "weeks",+ "wk" => "weeks",+ "w" => "weeks",+ "months" => "months",+ "mo" => "months",+ "mos" => "months",+ "month" => "months",+ "years" => "years",+ "year" => "years",+ "yrs" => "years",+ "yr" => "years",+ "y" => "years"+ }.freeze++ NEXT_UNIT_MAP = {+ "years" => "months",+ "months" => "weeks",+ "weeks" => "days",+ "days" => "hours",+ "hours" => "minutes",+ "minutes" => "seconds"+ }.freeze+class << selfdef parse(duration_string)# Keep 0 values and convert the extracted duration to hours# by dividing by 3600.- ChronicDuration.parse(duration_string, keep_zero: true, default_unit: "hours", **duration_length_options) / 3600.to_f++ last_unit_in_string = duration_string.scan(/[a-zA-Z]+/)+ .last++ default_unit = if last_unit_in_string+ last_unit_in_string+ .then { |last_unit| UNIT_ABBREVIATION_MAP[last_unit.downcase] }+ .then { |last_unit| NEXT_UNIT_MAP[last_unit] }+ else+ "hours"+ end++ ChronicDuration.parse(duration_string,+ keep_zero: true,+ default_unit:,+ **duration_length_options) / 3600.to_fenddef output(duration_in_hours)diff --git a/spec/services/duration_converter_spec.rb b/spec/services/duration_converter_spec.rbindex e2346afac08..0ca04115c25 100644--- a/spec/services/duration_converter_spec.rb+++ b/spec/services/duration_converter_spec.rb@@ -40,9 +40,17 @@ RSpec.describe DurationConverter doexpect(described_class.parse("5 hrs 30 mins")).to eq(5.5)end- it "assumes hours as the default unit for input" do+ it "assumes hours as the default unit for input if no other units given" doexpect(described_class.parse("5.75")).to eq(5.75)end++ it "assumes the next logical unit if at least one unit is given" do+ expect(described_class.parse("2h 15")).to eq(2.25)+ expect(described_class.parse("1d 24")).to eq(32)+ expect(described_class.parse("1w 1")).to eq(48)+ expect(described_class.parse("1mo 1")).to eq(200)+ expect(described_class.parse("1mo 1w 1d 1h 30")).to eq(209.5)+ endenddescribe ".output" do