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

Simplify RescheduleService and specs

Dombi Attila79a822618712
diff --git a/app/services/project_phases/reschedule_service.rb b/app/services/project_phases/reschedule_service.rb
index 6d1fb4668a2..1d2ddf1bb00 100644
--- a/app/services/project_phases/reschedule_service.rb
+++ b/app/services/project_phases/reschedule_service.rb
@@ -72,33 +72,31 @@ module ProjectPhases
def calculate_date_range(from, duration:)
days = working_days_from(from, count: duration)
- [days.first.date, days.last.date] if days.length == duration
+ [days.first, days.last] if days.length == duration
end
def reschedule_partial_phase_and_retrieve_next_start(phase, from)
- reschedule_phase_start(phase, from)
- return unless phase.finish_date?
+ phase.start_date = calculate_start_date(from)
+ phase.finish_date = calculate_finish_date(phase, from) if phase.finish_date?
+ phase.save
- reschedule_phase_finish_and_retrieve_next_start(phase, from)
+ phase.finish_date + 1 if phase.finish_date?
end
- def reschedule_phase_start(phase, from)
- start_date = working_days_from(from, count: 1).first.date
- phase.update(start_date:)
+ def calculate_start_date(from)
+ working_days_from(from, count: 1).first
end
- def reschedule_phase_finish_and_retrieve_next_start(phase, from)
- finish_date = [from, phase.finish_date].max
- working_finish_date = working_days_from(finish_date, count: 1).first.date
- phase.update(finish_date: working_finish_date)
- working_finish_date + 1
+ def calculate_finish_date(phase, from)
+ max_finish_date = [from, phase.finish_date].max
+ working_days_from(max_finish_date, count: 1).first
end
def working_days_from(from, count:)
- days = Day.working.from_range(from:, to: from.next_year).first(count)
+ days = Day.working.from_range(from:, to: from.next_year).limit(count).pluck(days: :date)
if days.length < count && !days.empty?
years = count.ceildiv(days.length) + 1
- days = Day.working.from_range(from:, to: from.next_year(years)).first(count)
+ days = Day.working.from_range(from:, to: from.next_year(years)).limit(count).pluck(days: :date)
end
days
end
diff --git a/spec/services/project_phases/reschedule_service_spec.rb b/spec/services/project_phases/reschedule_service_spec.rb
index 4ee4cd23bd1..fb304815085 100644
--- a/spec/services/project_phases/reschedule_service_spec.rb
+++ b/spec/services/project_phases/reschedule_service_spec.rb
@@ -153,20 +153,16 @@ RSpec.describe ProjectPhases::RescheduleService, type: :model do
]
end
- subject do
- service_response = service.call(phases:, from:)
- phases.each { |p| p.duration = p.calculate_duration }
- service_response
- end
+ subject { service.call(phases:, from:) }
it "reschedules only the ones with a start_date or finish_date present" do
expect(subject).to be_success
- expect(phases[0]).to have_attributes(start_date: from, finish_date: from, duration: 1)
- expect(phases[1]).to have_attributes(start_date: from + 1, finish_date: nil, duration: nil)
- expect(phases[2]).to have_attributes(start_date: from + 1, finish_date: nil, duration: nil)
- expect(phases[3]).to have_attributes(start_date: from + 1, finish_date: nil, duration: nil)
- expect(phases[4]).to have_attributes(start_date: from + 1, finish_date: from + 19, duration: 13)
- expect(phases[5]).to have_attributes(start_date: from + 20, finish_date: nil, duration: nil)
+ expect(phases[0]).to have_attributes(start_date: from, finish_date: from, calculate_duration: 1)
+ expect(phases[1]).to have_attributes(start_date: from + 1, finish_date: nil, calculate_duration: nil)
+ expect(phases[2]).to have_attributes(start_date: from + 1, finish_date: nil, calculate_duration: nil)
+ expect(phases[3]).to have_attributes(start_date: from + 1, finish_date: nil, calculate_duration: nil)
+ expect(phases[4]).to have_attributes(start_date: from + 1, finish_date: from + 19, calculate_duration: 13)
+ expect(phases[5]).to have_attributes(start_date: from + 20, finish_date: nil, calculate_duration: nil)
end
context "when the from date is earlier than the phases dates" do
@@ -174,12 +170,12 @@ RSpec.describe ProjectPhases::RescheduleService, type: :model do
it "reschedules only the ones with a start_date or finish_date present" do
expect(subject).to be_success
- expect(phases[0]).to have_attributes(start_date: from, finish_date: date, duration: 2)
- expect(phases[1]).to have_attributes(start_date: date + 1, finish_date: nil, duration: nil)
- expect(phases[2]).to have_attributes(start_date: date + 1, finish_date: nil, duration: nil)
- expect(phases[3]).to have_attributes(start_date: date + 1, finish_date: nil, duration: nil)
- expect(phases[4]).to have_attributes(start_date: date + 1, finish_date: date + 17, duration: 13)
- expect(phases[5]).to have_attributes(start_date: date + 20, finish_date: nil, duration: nil)
+ expect(phases[0]).to have_attributes(start_date: from, finish_date: date, calculate_duration: 2)
+ expect(phases[1]).to have_attributes(start_date: date + 1, finish_date: nil, calculate_duration: nil)
+ expect(phases[2]).to have_attributes(start_date: date + 1, finish_date: nil, calculate_duration: nil)
+ expect(phases[3]).to have_attributes(start_date: date + 1, finish_date: nil, calculate_duration: nil)
+ expect(phases[4]).to have_attributes(start_date: date + 1, finish_date: date + 17, calculate_duration: 13)
+ expect(phases[5]).to have_attributes(start_date: date + 20, finish_date: nil, calculate_duration: nil)
end
end
end