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

include duration in contract

ulferts89eae6714cb0
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 993ef02ea3b..4d2212faa73 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -98,6 +98,8 @@ module WorkPackages
model.leaf? || model.schedule_manually?
}
+ attribute :duration
+
attribute :budget
validates :due_date,
@@ -128,6 +130,11 @@ module WorkPackages
validate :validate_assigned_to_exists
+ validates :duration,
+ comparison: { greater_than: 0 }
+
+ validate :validate_duration_matches_dates
+
def initialize(work_package, user, options: {})
super
@@ -282,7 +289,7 @@ module WorkPackages
end
def validate_version_is_assignable
- if model.version_id && !model.assignable_versions.map(&:id).include?(model.version_id)
+ if model.version_id && model.assignable_versions.map(&:id).exclude?(model.version_id)
errors.add :version_id, :inclusion
end
end
@@ -296,7 +303,7 @@ module WorkPackages
def validate_people_visible(attribute, id_attribute, list)
id = model[id_attribute]
- return if id.nil? || !model.changed.include?(id_attribute)
+ return if id.nil? || model.changed.exclude?(id_attribute)
unless principal_visible?(id, list)
errors.add attribute,
@@ -305,6 +312,18 @@ module WorkPackages
end
end
+ def validate_duration_matches_dates
+ return unless model.start_date && model.due_date && model.duration
+
+ calculated_duration = model.due_date - model.start_date + 1
+
+ if calculated_duration > model.duration
+ errors.add :duration, :smaller_than_dates
+ elsif calculated_duration < model.duration
+ errors.add :duration, :larger_than_dates
+ end
+ end
+
def readonly_attributes_unchanged
super.tap do
if already_in_readonly_status? && unauthenticated_changed.any?
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 49590d8f4f1..ff46a4759b4 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -793,6 +793,9 @@ en:
attributes:
due_date:
not_start_date: "is not on start date, although this is required for milestones."
+ duration:
+ larger_than_dates: "is larger than the interval between the start and the finish date."
+ smaller_than_dates: "is smaller than the interval between the start and the finish date."
parent:
cannot_be_milestone: "cannot be a milestone."
cannot_be_self_assigned: "cannot be assigned to itself."
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index be1853fdd9c..aba45789a5d 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -27,6 +27,7 @@
#++
require 'spec_helper'
+require 'contracts/shared/model_contract_shared_context'
describe WorkPackages::BaseContract do
let(:work_package) do
@@ -80,6 +81,8 @@ describe WorkPackages::BaseContract do
subject(:contract) { described_class.new(work_package, current_user) }
+ include_context 'ModelContract shared context'
+
shared_examples_for 'invalid if changed' do |attribute|
before do
allow(work_package).to receive(:changed).and_return(changed_values.map(&:to_s))
@@ -426,7 +429,9 @@ describe WorkPackages::BaseContract do
before do
work_package.schedule_manually = schedule_manually
- work_package.parent = build_stubbed(:work_package)
+ allow(work_package)
+ .to receive(:parent)
+ .and_return(build_stubbed(:work_package))
allow(work_package)
.to receive(:soonest_start)
.and_return(Date.today + 4.days)
@@ -434,7 +439,7 @@ describe WorkPackages::BaseContract do
work_package.start_date = Date.today + 2.days
end
- context 'scheduled automatically' do
+ context 'when scheduled automatically' do
it 'notes the error' do
contract.validate
@@ -446,20 +451,15 @@ describe WorkPackages::BaseContract do
end
end
- context 'scheduled manually' do
+ context 'when scheduled manually' do
let(:schedule_manually) { true }
- it 'is valid' do
- contract.validate
-
- expect(contract.errors[:start_date])
- .to be_empty
- end
+ it_behaves_like 'contract is valid'
end
end
end
- describe 'finish date' do
+ describe 'due date' do
it_behaves_like 'a parent unwritable property', :due_date, schedule_sensitive: true
it_behaves_like 'a date attribute', :due_date
@@ -476,6 +476,72 @@ describe WorkPackages::BaseContract do
end
end
+ describe 'duration' do
+ context 'when setting the duration' do
+ before do
+ work_package.duration = 5
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context 'when setting the duration to 0' do
+ before do
+ work_package.duration = 0
+ end
+
+ it_behaves_like 'contract is invalid', duration: :greater_than
+ end
+
+ context 'when setting the duration to a negative value' do
+ before do
+ work_package.duration = -5
+ end
+
+ it_behaves_like 'contract is invalid', duration: :greater_than
+ end
+
+ context 'when setting duration as well as the dates' do
+ before do
+ work_package.duration = 6
+ work_package.start_date = Time.zone.today - 4.days
+ work_package.due_date = Time.zone.today + 1.day
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context 'when setting duration as well as the dates and the duration is too small' do
+ before do
+ work_package.duration = 5
+ work_package.start_date = Time.zone.today - 4.days
+ work_package.due_date = Time.zone.today + 1.day
+ end
+
+ it_behaves_like 'contract is invalid', duration: :smaller_than_dates
+ end
+
+ context 'when setting duration as well as the dates and the duration is too big' do
+ before do
+ work_package.duration = 7
+ work_package.start_date = Time.zone.today - 4.days
+ work_package.due_date = Time.zone.today + 1.day
+ end
+
+ it_behaves_like 'contract is invalid', duration: :larger_than_dates
+ end
+
+ context 'when setting duration as well as the dates to the same date' do
+ before do
+ work_package.duration = 1
+ work_package.start_date = Time.zone.today
+ work_package.due_date = Time.zone.today
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+ end
+
describe 'percentage done' do
it_behaves_like 'a parent unwritable property', :done_ratio