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
ensure duration to be integer
ulfertsf5fc2b9c5962
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rbindex db319180f8a..f3742638226 100644--- a/app/contracts/work_packages/base_contract.rb+++ b/app/contracts/work_packages/base_contract.rb@@ -131,9 +131,13 @@ module WorkPackagesvalidate :validate_assigned_to_existsvalidates :duration,- comparison: { greater_than: 0 },+ # only_integer: true, cannot be used as that will not compare with the value+ # before the type cast. So even a float value will pass the validation as it is silently+ # floored.+ numericality: { greater_than: 0 },allow_nil: true+ validate :validate_duration_integervalidate :validate_duration_matches_datesdef initialize(work_package, user, options: {})@@ -313,6 +317,10 @@ module WorkPackagesendend+ def validate_duration_integer+ errors.add :duration, :only_integer if model.duration_before_type_cast != model.duration+ end+def validate_duration_matches_datesreturn unless calculated_duration && model.durationdiff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rbindex aba45789a5d..58bd48025d6 100644--- a/spec/contracts/work_packages/base_contract_spec.rb+++ b/spec/contracts/work_packages/base_contract_spec.rb@@ -493,6 +493,14 @@ describe WorkPackages::BaseContract doit_behaves_like 'contract is invalid', duration: :greater_thanend+ context 'when setting the duration to a floating point' do+ before do+ work_package.duration = 4.5+ end++ it_behaves_like 'contract is invalid', duration: :only_integer+ end+context 'when setting the duration to a negative value' dobefore dowork_package.duration = -5