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
Add duration setter for WorkPackageRepresenter
Dombi Attila0bb487ab269a
diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 20b7953f534..783f909fd8a 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -619,6 +619,7 @@ en:confirmation: "doesn't match %{attribute}."could_not_be_copied: "%{dependency} could not be (fully) copied."does_not_exist: "does not exist."+ duration_for_milestones_not_writable: "Milestone typed work packages don't have a duration."error_enterprise_only: "is only available in the OpenProject Enterprise Edition"error_unauthorized: "may not be accessed."error_readonly: "was attempted to be written but is not writable."diff --git a/lib/api/v3/utilities/date_time_formatter.rb b/lib/api/v3/utilities/date_time_formatter.rbindex 1c32a2302ab..b9442a180ac 100644--- a/lib/api/v3/utilities/date_time_formatter.rb+++ b/lib/api/v3/utilities/date_time_formatter.rb@@ -98,6 +98,12 @@ module APIduration)endend++ def self.parse_duration_to_days(duration, property_name, allow_nil: false)+ return nil if duration.nil? && allow_nil++ parse_duration_to_hours(duration, property_name).to_i / 24+ endendendenddiff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rbindex d73da740f72..9276117bf98 100644--- a/lib/api/v3/work_packages/work_package_representer.rb+++ b/lib/api/v3/work_packages/work_package_representer.rb@@ -593,6 +593,16 @@ module API# noopend+ def duration=(value)+ if represented.milestone?+ raise NoMethodError, I18n.t('activerecord.errors.messages.duration_for_milestones_not_writable')+ end++ represented.duration = datetime_formatter.parse_duration_to_days(value,+ 'duration',+ allow_nil: true)+ end+def ordered_custom_actions# As the custom actions are sometimes set as an arrayrepresented.custom_actions(current_user).to_a.sort_by(&:position)diff --git a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb b/spec/lib/api/v3/utilities/date_time_formatter_spec.rbindex ce2a18b7111..2280fa979a7 100644--- a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb+++ b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb@@ -56,6 +56,26 @@ describe ::API::V3::Utilities::DateTimeFormatter doendend+ shared_examples_for 'rejects durations with parsing errors' do+ it 'rejects parsing non sense' do+ expect do+ subject.send(method, 'foo', 'prop')+ end.to raise_error(API::Errors::PropertyFormatError)+ end++ it 'rejects parsing pure number strings' do+ expect do+ subject.send(method, '5', 'prop')+ end.to raise_error(API::Errors::PropertyFormatError)+ end++ it 'rejects parsing pure numbers' do+ expect do+ subject.send(method, 5, 'prop')+ end.to raise_error(API::Errors::PropertyFormatError)+ end+ end+describe 'format_date' doit 'formats dates' doexpect(subject.format_date(date)).to eq(date.iso8601)@@ -185,26 +205,35 @@ describe ::API::V3::Utilities::DateTimeFormatter doexpect(subject.parse_duration_to_hours('P1D', 'prop')).to eq(24.0)end- it 'rejects parsing non sense' do- expect do- subject.parse_duration_to_hours('foo', 'prop')- end.to raise_error(API::Errors::PropertyFormatError)+ it_behaves_like 'rejects durations with parsing errors' do+ let(:method) { :parse_duration_to_hours }end- it 'rejects parsing pure number strings' do- expect do- subject.parse_duration_to_hours('5', 'prop')- end.to raise_error(API::Errors::PropertyFormatError)+ it_behaves_like 'can parse nil' do+ let(:method) { :parse_duration_to_hours }+ let(:input) { 'PT5H' }end+ end- it 'rejects parsing pure numbers' do- expect do- subject.parse_duration_to_hours(5, 'prop')- end.to raise_error(API::Errors::PropertyFormatError)+ describe 'parse_duration_to_days' do+ it 'parses ISO 8601 durations of full days' do+ expect(subject.parse_duration_to_days('P5D', 'prop')).to eq(5)+ end++ it 'parses ISO 8601 durations of fractional days as whole' do+ expect(subject.parse_duration_to_days('P5DT18H30M', 'prop')).to eq(5)+ end++ it 'parses ISO 8601 durations of hours as 0 days' do+ expect(subject.parse_duration_to_days('PT1H30M', 'prop')).to eq(0)+ end++ it_behaves_like 'rejects durations with parsing errors' do+ let(:method) { :parse_duration_to_days }endit_behaves_like 'can parse nil' do- let(:method) { :parse_duration_to_hours }+ let(:method) { :parse_duration_to_days }let(:input) { 'PT5H' }endenddiff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rbindex 02deeccd36b..a5632a3ddc0 100644--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb@@ -1377,5 +1377,25 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter doendendend++ describe 'parsing' do+ describe 'duration', with_flag: { work_packages_duration_field_active: true } do+ subject { representer }++ it 'parses form iso8601 format' do+ subject.duration = 'P6D'+ expect(subject.represented.duration).to eq(6)+ end++ context 'when the work_package is a milestone' do+ let(:type_milestone) { true }++ it 'raises an error' do+ expect { subject.duration = 'P6D' }+ .to raise_error NoMethodError, /Milestone typed work packages don't have a duration/+ end+ end+ end+ endend# rubocop:enable RSpec:MultipleMemoizedHelpers