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

Handle conversion of duration strings to hours in model

Christophe Bliard69b61c3352a7
diff --git a/app/controllers/work_packages/progress_controller.rb b/app/controllers/work_packages/progress_controller.rb
index d9e5cfa196a..b522a5081a2 100644
--- a/app/controllers/work_packages/progress_controller.rb
+++ b/app/controllers/work_packages/progress_controller.rb
@@ -130,17 +130,7 @@ class WorkPackages::ProgressController < ApplicationController
def work_package_params
params.require(:work_package)
- .permit(allowed_params).tap do |wp_params|
- %w[estimated_hours remaining_hours].each do |attr|
- if wp_params[attr].present?
- begin
- wp_params[attr] = DurationConverter.parse(wp_params[attr])
- rescue ChronicDuration::DurationParseError
- @work_package.errors.add(attr.to_sym, :invalid)
- end
- end
- end
- end
+ .permit(allowed_params)
end
def allowed_params
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 0d32c30f11a..a1dd265d12a 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -315,13 +315,11 @@ class WorkPackage < ApplicationRecord
end
def estimated_hours=(hours)
- converted_hours = (hours.is_a?(String) ? hours.to_hours : hours)
- write_attribute :estimated_hours, !!converted_hours ? converted_hours : hours
+ write_attribute :estimated_hours, convert_duration_to_hours(hours)
end
def remaining_hours=(hours)
- converted_hours = (hours.is_a?(String) ? hours.to_hours : hours)
- write_attribute :remaining_hours, !!converted_hours ? converted_hours : hours
+ write_attribute :remaining_hours, convert_duration_to_hours(hours)
end
def duration_in_hours
@@ -546,6 +544,17 @@ class WorkPackage < ApplicationRecord
time_entries.build(attributes)
end
+ def convert_duration_to_hours(value)
+ if value.is_a?(String)
+ begin
+ value = value.blank? ? nil : DurationConverter.parse(value)
+ rescue ChronicDuration::DurationParseError
+ # keep invalid value, error shall be caught by numericality validator
+ end
+ end
+ value
+ end
+
##
# Checks if the time entry defined by the given attributes is blank.
# A time entry counts as blank despite a selected activity if that activity
diff --git a/spec/models/work_package_spec.rb b/spec/models/work_package_spec.rb
index 88ce100130d..e8885efdf74 100644
--- a/spec/models/work_package_spec.rb
+++ b/spec/models/work_package_spec.rb
@@ -56,7 +56,7 @@ RSpec.describe WorkPackage do
priority:,
subject: "test_create",
description: "WorkPackage#create",
- estimated_hours: "1:30" }
+ estimated_hours: "1h30" }
end
end
@@ -746,9 +746,22 @@ RSpec.describe WorkPackage do
end
describe "#remaining_hours" do
- it "allows empty values" do
+ it "allows empty value" do
+ work_package.remaining_hours = ""
+ expect(work_package).to be_valid
expect(work_package.remaining_hours).to be_nil
+ end
+
+ it "allows blank values" do
+ work_package.remaining_hours = " "
expect(work_package).to be_valid
+ expect(work_package.remaining_hours).to be_nil
+ end
+
+ it "allows nil value" do
+ work_package.remaining_hours = nil
+ expect(work_package).to be_valid
+ expect(work_package.remaining_hours).to be_nil
end
it "allows values greater than or equal to 0" do
@@ -773,5 +786,23 @@ RSpec.describe WorkPackage do
work_package.remaining_hours = "1.3"
expect(work_package).to be_valid
end
+
+ it "allows hours like '1h06'" do
+ work_package.remaining_hours = "1h06"
+ expect(work_package).to be_valid
+ expect(work_package.remaining_hours).to eq(1.1)
+ end
+
+ it "allows hours like '1h 24m'" do
+ work_package.remaining_hours = "1h 24m"
+ expect(work_package).to be_valid
+ expect(work_package.remaining_hours).to eq(1.4)
+ end
+
+ it "allows hours like '3d 1h 30m'" do
+ work_package.remaining_hours = "3d 1h 30m"
+ expect(work_package).to be_valid
+ expect(work_package.remaining_hours).to eq((3 * 8) + 1.5)
+ end
end
end