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

replace UpdateContract and UpdateService

Ivan Kuchinb902e5e399df
diff --git a/app/contracts/project_life_cycle_steps/update_contract.rb b/app/contracts/project_life_cycle_steps/update_contract.rb
index 5fca96d1088..83634dd420a 100644
--- a/app/contracts/project_life_cycle_steps/update_contract.rb
+++ b/app/contracts/project_life_cycle_steps/update_contract.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -28,6 +30,34 @@
module ProjectLifeCycleSteps
class UpdateContract < BaseContract
- alias_method :project, :model
+ validate :validate_start_after_preceeding_phases
+
+ delegate :project, to: :model
+
+ def writable_attributes = %w[start_date finish_date active]
+
+ def validate_start_after_preceeding_phases
+ return unless model.active?
+ return unless model.range_set?
+ return if start_after_preceding_phases?
+
+ model.errors.add(:date_range, :non_continuous_dates)
+ end
+
+ private
+
+ def start_after_preceding_phases?
+ preceding_phases
+ .select(&:range_set?)
+ .all? { valid_dates?(current: model, previous: it) }
+ end
+
+ def valid_dates?(current:, previous:)
+ current.start_date > previous.finish_date
+ end
+
+ def preceding_phases
+ project.available_phases.select { it.position < model.position }
+ end
end
end
diff --git a/app/contracts/project_life_cycle_steps/update_single_contract.rb b/app/contracts/project_life_cycle_steps/update_single_contract.rb
deleted file mode 100644
index 58240d62ad4..00000000000
--- a/app/contracts/project_life_cycle_steps/update_single_contract.rb
+++ /dev/null
@@ -1,63 +0,0 @@
-# frozen_string_literal: true
-
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-module ProjectLifeCycleSteps
- class UpdateSingleContract < BaseContract
- validate :validate_start_after_preceeding_phases
-
- delegate :project, to: :model
-
- def writable_attributes = %w[start_date finish_date active]
-
- def validate_start_after_preceeding_phases
- return unless model.active?
- return unless model.range_set?
- return if start_after_preceding_phases?
-
- model.errors.add(:date_range, :non_continuous_dates)
- end
-
- private
-
- def start_after_preceding_phases?
- preceding_phases
- .select(&:range_set?)
- .all? { valid_dates?(current: model, previous: it) }
- end
-
- def valid_dates?(current:, previous:)
- current.start_date > previous.finish_date
- end
-
- def preceding_phases
- project.available_phases.select { it.position < model.position }
- end
- end
-end
diff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rb
index 74ee619063b..2d053787fb1 100644
--- a/app/models/permitted_params.rb
+++ b/app/models/permitted_params.rb
@@ -291,7 +291,7 @@ class PermittedParams
end
def project_phase
- params.require(:project_phase).permit(%i[id date_range])
+ params.require(:project_phase).permit(%i[date_range])
end
def project_custom_field_project_mapping
diff --git a/app/services/project_life_cycle_steps/update_service.rb b/app/services/project_life_cycle_steps/update_service.rb
index 369a19136ef..e44b07c08b1 100644
--- a/app/services/project_life_cycle_steps/update_service.rb
+++ b/app/services/project_life_cycle_steps/update_service.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -28,11 +30,71 @@
module ProjectLifeCycleSteps
class UpdateService < ::BaseServices::Update
- def after_perform(call)
- project = call.result.project
+ delegate :project, to: :model
+
+ def after_validate(*)
+ set_duration
+
+ super
+ end
+
+ def after_perform(*)
+ reschedule_following_phases if model.range_set?
+
project.touch_and_save_journals
- call
+ super
+ end
+
+ private
+
+ def set_duration
+ model.duration = calculate_duration
+ end
+
+ def calculate_duration
+ return nil unless model.range_set?
+
+ Day.working.from_range(from: model.start_date, to: model.finish_date).count
+ end
+
+ def reschedule_following_phases
+ from = initial_reschedule_date
+
+ following_phases.each do |phase|
+ next unless phase.range_set?
+ next unless phase.duration # not updated using service that sets duration
+
+ date_range = calculate_date_range(from, duration: phase.duration)
+ next unless date_range
+
+ next unless phase.update(date_range:)
+
+ from = date_range.end + 1
+ end
+ end
+
+ def initial_reschedule_date
+ model.active? ? model.finish_date + 1 : model.start_date
+ end
+
+ def following_phases
+ project.available_phases.select { it.position > model.position }
+ end
+
+ def calculate_date_range(from, duration:)
+ days = working_days_from(from, count: duration)
+
+ days.first.date..days.last.date if days.length == duration
+ end
+
+ def working_days_from(from, count:)
+ days = Day.working.from_range(from:, to: from.next_year).first(count)
+ 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)
+ end
+ days
end
end
end
diff --git a/app/services/project_life_cycle_steps/update_single_service.rb b/app/services/project_life_cycle_steps/update_single_service.rb
deleted file mode 100644
index 9fb44dfa212..00000000000
--- a/app/services/project_life_cycle_steps/update_single_service.rb
+++ /dev/null
@@ -1,98 +0,0 @@
-# frozen_string_literal: true
-
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-module ProjectLifeCycleSteps
- class UpdateSingleService < ::BaseServices::Update
- def after_validate(*)
- set_duration
-
- super
- end
-
- def after_perform(*)
- reschedule_following_phases if model.range_set?
-
- super
- end
-
- private
-
- def default_contract_class = ProjectLifeCycleSteps::UpdateSingleContract
-
- def set_duration
- model.duration = calculate_duration
- end
-
- def calculate_duration
- return nil unless model.range_set?
-
- Day.working.from_range(from: model.start_date, to: model.finish_date).count
- end
-
- def reschedule_following_phases
- from = initial_reschedule_date
-
- following_phases.each do |phase|
- next unless phase.range_set?
- next unless phase.duration # not updated using service that sets duration
-
- date_range = calculate_date_range(from, duration: phase.duration)
- next unless date_range
-
- next unless phase.update(date_range:)
-
- from = date_range.end + 1
- end
- end
-
- def initial_reschedule_date
- model.active? ? model.finish_date + 1 : model.start_date
- end
-
- def following_phases
- model.project.available_phases.select { it.position > model.position }
- end
-
- def calculate_date_range(from, duration:)
- days = working_days_from(from, count: duration)
-
- days.first.date..days.last.date if days.length == duration
- end
-
- def working_days_from(from, count:)
- days = Day.working.from_range(from:, to: from.next_year).first(count)
- 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)
- end
- days
- end
- end
-end
diff --git a/spec/contracts/project_life_cycle_steps/update_contract_spec.rb b/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
index 3fddc7d5c9a..1b17ab94a21 100644
--- a/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
+++ b/spec/contracts/project_life_cycle_steps/update_contract_spec.rb
@@ -34,12 +34,15 @@ require "contracts/shared/model_contract_shared_context"
RSpec.describe ProjectLifeCycleSteps::UpdateContract do
include_context "ModelContract shared context"
- let(:contract) { described_class.new(phase, user) }
let(:user) { build_stubbed(:user) }
- let(:project) { build_stubbed(:project) }
- let(:phase) { build_stubbed(:project_phase, project:) }
+
+ subject(:contract) { described_class.new(phase, user) }
context "with authorized user" do
+ let(:phase) { build_stubbed(:project_phase) }
+ let(:project) { phase.project }
+ let(:date) { Date.current }
+
before do
mock_permissions_for(user) do |mock|
mock.allow_in_project(:edit_project_phases, project:)
@@ -47,10 +50,98 @@ RSpec.describe ProjectLifeCycleSteps::UpdateContract do
end
it_behaves_like "contract is valid"
- include_examples "contract reuses the model errors"
+ it_behaves_like "contract reuses the model errors"
+
+ context "when phase is invalid" do
+ let(:phase) do
+ build_stubbed(:project_phase, start_date: date + 1, finish_date: date - 1)
+ end
+
+ it_behaves_like "contract is invalid", date_range: :start_date_must_be_before_finish_date
+ end
+
+ context "when trying to change extra attributes" do
+ before do
+ phase.duration = 42
+ end
+
+ it_behaves_like "contract is invalid", duration: :error_readonly
+ end
+
+ describe "#validate_start_after_preceeding_phases" do
+ def build_phase(**) = build_stubbed(:project_phase, project:, **)
+
+ let(:project) { build_stubbed(:project) }
+ let(:phases) { [preceding, phase, following] }
+ let(:phase) { build_phase(date_range:, active:) }
+ let(:preceding) { build_phase(date_range: preceding_date_range) }
+ let(:following) { build_phase(date_range: following_date_range) }
+ let(:active) { true }
+ let(:date_range) { date - 1..date + 1 }
+ let(:preceding_date_range) { date - 6..date - 5 }
+ let(:following_date_range) { date + 5..date + 6 }
+
+ before do
+ allow(project).to receive(:available_phases).and_return(phases)
+ end
+
+ context "with successive non overlapping dates" do
+ it_behaves_like "contract is valid"
+ end
+
+ context "without dates" do
+ let(:date_range) { nil }
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "with preceding phase overlapping with start" do
+ let(:preceding_date_range) { date - 6..date - 1 }
+
+ it_behaves_like "contract is invalid", date_range: :non_continuous_dates
+
+ context "when inactive" do
+ let(:active) { false }
+
+ it_behaves_like "contract is valid"
+ end
+ end
+
+ context "with preceding phase following this" do
+ let(:preceding_date_range) { date + 2..date + 4 }
+
+ it_behaves_like "contract is invalid", date_range: :non_continuous_dates
+
+ context "when inactive" do
+ let(:active) { false }
+
+ it_behaves_like "contract is valid"
+ end
+ end
+
+ context "with preceding phase without dates" do
+ let(:preceding_date_range) { nil }
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "with following phase overlapping with start" do
+ let(:following_date_range) { date - 1..date + 6 }
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "with following phase preceding this" do
+ let(:following_date_range) { date - 4..date - 2 }
+
+ it_behaves_like "contract is valid"
+ end
+ end
end
context "with unauthorized user" do
+ let(:phase) { build_stubbed(:project_phase) }
+
it_behaves_like "contract user is unauthorized"
end
end
diff --git a/spec/contracts/project_life_cycle_steps/update_single_contract_spec.rb b/spec/contracts/project_life_cycle_steps/update_single_contract_spec.rb
deleted file mode 100644
index 8a740a8b2f8..00000000000
--- a/spec/contracts/project_life_cycle_steps/update_single_contract_spec.rb
+++ /dev/null
@@ -1,147 +0,0 @@
-# frozen_string_literal: true
-
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-require "spec_helper"
-require "contracts/shared/model_contract_shared_context"
-
-RSpec.describe ProjectLifeCycleSteps::UpdateSingleContract do
- include_context "ModelContract shared context"
-
- let(:user) { build_stubbed(:user) }
-
- subject(:contract) { described_class.new(phase, user) }
-
- context "with authorized user" do
- let(:phase) { build_stubbed(:project_phase) }
- let(:project) { phase.project }
- let(:date) { Date.current }
-
- before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project(:edit_project_phases, project:)
- end
- end
-
- it_behaves_like "contract is valid"
- include_examples "contract reuses the model errors"
-
- context "when phase is invalid" do
- let(:phase) do
- build_stubbed(:project_phase, start_date: date + 1, finish_date: date - 1)
- end
-
- it_behaves_like "contract is invalid", date_range: :start_date_must_be_before_finish_date
- end
-
- context "when trying to change extra attributes" do
- before do
- phase.duration = 42
- end
-
- it_behaves_like "contract is invalid", duration: :error_readonly
- end
-
- describe "#validate_start_after_preceeding_phases" do
- def build_phase(**) = build_stubbed(:project_phase, project:, **)
-
- let(:project) { build_stubbed(:project) }
- let(:phases) { [preceding, phase, following] }
- let(:phase) { build_phase(date_range:, active:) }
- let(:preceding) { build_phase(date_range: preceding_date_range) }
- let(:following) { build_phase(date_range: following_date_range) }
- let(:active) { true }
- let(:date_range) { date - 1..date + 1 }
- let(:preceding_date_range) { date - 6..date - 5 }
- let(:following_date_range) { date + 5..date + 6 }
-
- before do
- allow(project).to receive(:available_phases).and_return(phases)
- end
-
- context "with successive non overlapping dates" do
- it_behaves_like "contract is valid"
- end
-
- context "without dates" do
- let(:date_range) { nil }
-
- it_behaves_like "contract is valid"
- end
-
- context "with preceding phase overlapping with start" do
- let(:preceding_date_range) { date - 6..date - 1 }
-
- it_behaves_like "contract is invalid", date_range: :non_continuous_dates
-
- context "when inactive" do
- let(:active) { false }
-
- it_behaves_like "contract is valid"
- end
- end
-
- context "with preceding phase following this" do
- let(:preceding_date_range) { date + 2..date + 4 }
-
- it_behaves_like "contract is invalid", date_range: :non_continuous_dates
-
- context "when inactive" do
- let(:active) { false }
-
- it_behaves_like "contract is valid"
- end
- end
-
- context "with preceding phase without dates" do
- let(:preceding_date_range) { nil }
-
- it_behaves_like "contract is valid"
- end
-
- context "with following phase overlapping with start" do
- let(:following_date_range) { date - 1..date + 6 }
-
- it_behaves_like "contract is valid"
- end
-
- context "with following phase preceding this" do
- let(:following_date_range) { date - 4..date - 2 }
-
- it_behaves_like "contract is valid"
- end
- end
- end
-
- context "with unauthorized user" do
- let(:phase) { build_stubbed(:project_phase) }
-
- it_behaves_like "contract user is unauthorized"
- end
-end
diff --git a/spec/services/project_life_cycle_steps/update_single_service_spec.rb b/spec/services/project_life_cycle_steps/update_service_spec.rb
similarity index 91%
rename from spec/services/project_life_cycle_steps/update_single_service_spec.rb
rename to spec/services/project_life_cycle_steps/update_service_spec.rb
index 145069d38b1..15b50aaf194 100644
--- a/spec/services/project_life_cycle_steps/update_single_service_spec.rb
+++ b/spec/services/project_life_cycle_steps/update_service_spec.rb
@@ -31,12 +31,37 @@
require "spec_helper"
require "services/base_services/behaves_like_update_service"
-RSpec.describe ProjectLifeCycleSteps::UpdateSingleService, type: :model do
+RSpec.describe ProjectLifeCycleSteps::UpdateService, type: :model do
shared_let(:week_days) { week_with_saturday_and_sunday_as_weekend }
+ before do
+ allow(project).to receive(:touch_and_save_journals)
+ end
+
it_behaves_like "BaseServices update service" do
let(:factory) { :project_phase }
- let(:contract_class) { ProjectLifeCycleSteps::UpdateSingleContract }
+ let(:contract_class) { ProjectLifeCycleSteps::UpdateContract }
+ let(:project) { model_instance.project }
+ end
+
+ describe "journalizing" do
+ shared_let(:phase) { create(:project_phase) }
+
+ let(:user) { build_stubbed(:user) }
+ let(:project) { phase.project }
+ let(:service) { described_class.new(user:, model: phase) }
+
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project(:edit_project_phases, project:)
+ end
+ end
+
+ it "calls touch_and_save_journals on project" do
+ expect(service.call).to be_success
+
+ expect(project).to have_received(:touch_and_save_journals)
+ end
end
describe "#set_duration" do