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
Validate remaining_hours doesn't exceed estimated_hours
Aaron Contrerasbee33d4629ed
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rbindex ee86d5abfa3..c3d7e94eb6d 100644--- a/app/contracts/work_packages/base_contract.rb+++ b/app/contracts/work_packages/base_contract.rb@@ -61,7 +61,9 @@ module WorkPackagesattribute :derived_estimated_hours,writable: false- attribute :remaining_hours+ attribute :remaining_hours do+ validate_remaining_hours_doesnt_exceed_estimated_hours+ endattribute :derived_remaining_hours,writable: false@@ -318,6 +320,26 @@ module WorkPackagesendend+ def validate_remaining_hours_doesnt_exceed_estimated_hours+ if both_remaining_and_estimated_hours_present? && remaining_work_exceeds_work?+ if model.changed.include?("estimated_hours")+ errors.add(:estimated_hours, :cant_be_inferior_to_remaining_work)+ end++ if model.changed.include?("remaining_hours")+ errors.add(:remaining_hours, :cant_exceed_work)+ end+ end+ end++ def both_remaining_and_estimated_hours_present?+ model.remaining_hours.present? && model.estimated_hours.present?+ end++ def remaining_work_exceeds_work?+ model.remaining_hours > model.estimated_hours+ end+def validate_no_reopen_on_closed_versionif model.version_id && model.reopened? && model.version.closed?errors.add :base, I18n.t(:error_can_not_reopen_work_package_on_closed_version)diff --git a/config/locales/en.yml b/config/locales/en.ymlindex f4441c6e569..5015c613241 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1009,6 +1009,9 @@ en:does_not_exist: "The specified category does not exist."estimated_hours:only_values_greater_or_equal_zeroes_allowed: "must be >= 0."+ cant_be_inferior_to_remaining_work: "can't be inferior to 'Remaining work'"+ remaining_hours:+ cant_exceed_work: "can't exceed 'Work'"readonly_status: "The work package is in a readonly status so its attributes cannot be changed."type:attributes:diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rbindex 2d71f18350b..2e589a117f3 100644--- a/spec/contracts/work_packages/base_contract_spec.rb+++ b/spec/contracts/work_packages/base_contract_spec.rb@@ -339,6 +339,22 @@ RSpec.describe WorkPackages::BaseContract do.to contain_exactly(:only_values_greater_or_equal_zeroes_allowed)endend++ context 'when inferior to remaining_hours' do+ let(:estimated_hours) { 5.0 }++ before do+ work_package.remaining_hours = 6.0+ allow(work_package).to receive(:changed).and_return(%w[estimated_hours])+ end++ it 'is invalid' do+ contract.validate++ expect(subject.errors.symbols_for(:estimated_hours))+ .to contain_exactly(:cant_be_inferior_to_remaining_work)+ end+ endenddescribe 'derived estimated hours' do@@ -366,6 +382,66 @@ RSpec.describe WorkPackages::BaseContract doendend+ describe 'remaining hours' do+ before do+ work_package.estimated_hours = 5.0+ work_package.remaining_hours = remaining_hours++ allow(work_package).to receive(:changed).and_return(%w[remaining_hours])+ end++ context "when it's the same as estimated_hours" do+ let(:remaining_hours) { 5.0 }++ it 'is valid' do+ contract.validate++ expect(subject.errors.symbols_for(:remaining_hours))+ .to be_empty+ end+ end++ context "when it's less than estimated_hours" do+ let(:remaining_hours) { 4.0 }++ it 'is valid' do+ contract.validate++ expect(subject.errors.symbols_for(:remaining_hours))+ .to be_empty+ end+ end++ context "when it's greater than estimated_hours" do+ let(:remaining_hours) { 6.0 }++ it 'is invalid' do+ contract.validate++ expect(subject.errors.symbols_for(:remaining_hours))+ .to contain_exactly(:cant_exceed_work)+ end+ end+ end++ describe 'estimated_hours and remaining_hours' do+ context 'when changing both and remaining_hours exceeds estimated_hours' do+ before do+ work_package.estimated_hours = 5.0+ work_package.remaining_hours = 6.0+ end++ it 'is invalid' do+ contract.validate++ expect(subject.errors.symbols_for(:estimated_hours))+ .to contain_exactly(:cant_be_inferior_to_remaining_work)+ expect(subject.errors.symbols_for(:remaining_hours))+ .to contain_exactly(:cant_exceed_work)+ end+ end+ end+shared_examples_for 'a date attribute' do |attribute|context 'for a date' dobefore do