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

[#48373] adressed code review issues

Eric Schubertc46cbf754eb5
diff --git a/modules/storages/app/contracts/storages/last_project_folders/base_contract.rb b/modules/storages/app/contracts/storages/last_project_folders/base_contract.rb
index 330fcbf20be..a3a2ce79c6f 100644
--- a/modules/storages/app/contracts/storages/last_project_folders/base_contract.rb
+++ b/modules/storages/app/contracts/storages/last_project_folders/base_contract.rb
@@ -33,9 +33,7 @@ module Storages::LastProjectFolders
attribute :projects_storage
validates_presence_of :projects_storage
attribute :mode
- validates :mode, presence: true, inclusion: {
- in: Storages::ProjectStorage.project_folder_modes.keys.reject { |v| v == :inactive }
- }
+ validates :mode, presence: true, inclusion: { in: %w[manual automatic] }
attribute :origin_folder_id
end
end
diff --git a/modules/storages/app/contracts/storages/project_storages/base_contract.rb b/modules/storages/app/contracts/storages/project_storages/base_contract.rb
index 9d37ed564a9..0cde972fc93 100644
--- a/modules/storages/app/contracts/storages/project_storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/project_storages/base_contract.rb
@@ -46,10 +46,8 @@ module Storages::ProjectStorages
attribute :project_folder_id
validates :project_folder_id, presence: true, if: :project_folder_mode_manual?
- attribute :project_folder_id
-
attribute :project_folder_mode do
- unless @model.project_folder_inactive? || @model.project_folder_manual? || @model.project_folder_automatic?
+ if Storages::ProjectStorage.project_folder_modes.keys.exclude?(@model.project_folder_mode)
errors.add :project_folder_mode, :invalid
end
end
diff --git a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
index 6fa2786243d..fc5903683d2 100644
--- a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
@@ -111,8 +111,8 @@ class Storages::Admin::ProjectsStoragesController < Projects::SettingsController
@last_project_folders = Storages::LastProjectFolder
.where(projects_storage: @project_storage)
- .index_by(&:mode)
- .transform_values(&:origin_folder_id)
+ .pluck(:mode, :origin_folder_id)
+ .to_h
render '/storages/project_settings/edit'
end
diff --git a/modules/storages/app/services/storages/project_storages/create_service.rb b/modules/storages/app/services/storages/project_storages/create_service.rb
index eb3106eec6c..fb4a762dfa0 100644
--- a/modules/storages/app/services/storages/project_storages/create_service.rb
+++ b/modules/storages/app/services/storages/project_storages/create_service.rb
@@ -35,9 +35,15 @@ module Storages::ProjectStorages
super(service_call)
project_storage = service_call.result
+ add_historical_data(service_call) if project_storage.project_folder_mode.to_sym != :inactive
- return service_call if project_storage.project_folder_mode.to_sym == :inactive
+ service_call
+ end
+ private
+
+ def add_historical_data(service_call)
+ project_storage = service_call.result
last_project_folder_result =
LastProjectFolderPersistenceHelper.create_last_project_folder(
user:,
diff --git a/modules/storages/app/services/storages/project_storages/last_project_folder_persistence_helper.rb b/modules/storages/app/services/storages/project_storages/last_project_folder_persistence_helper.rb
index b53f7fa55c6..a2b69e6dc8a 100644
--- a/modules/storages/app/services/storages/project_storages/last_project_folder_persistence_helper.rb
+++ b/modules/storages/app/services/storages/project_storages/last_project_folder_persistence_helper.rb
@@ -32,12 +32,12 @@ module Storages::ProjectStorages::LastProjectFolderPersistenceHelper
def create_last_project_folder(user:, projects_storage_id:, origin_folder_id:, mode:)
::Storages::LastProjectFolders::CreateService
.new(user:)
- .call({ projects_storage_id:, origin_folder_id:, mode: mode.to_sym })
+ .call(projects_storage_id:, origin_folder_id:, mode: mode.to_sym)
end
def update_last_project_folder(user:, project_folder:, origin_folder_id:)
::Storages::LastProjectFolders::UpdateService
.new(model: project_folder, user:)
- .call({ origin_folder_id: })
+ .call(origin_folder_id:)
end
end
diff --git a/modules/storages/app/services/storages/project_storages/update_service.rb b/modules/storages/app/services/storages/project_storages/update_service.rb
index 2b29b076159..91023a3d108 100644
--- a/modules/storages/app/services/storages/project_storages/update_service.rb
+++ b/modules/storages/app/services/storages/project_storages/update_service.rb
@@ -36,8 +36,15 @@ module Storages::ProjectStorages
super(service_call)
project_storage = service_call.result
- return service_call if project_storage.project_folder_mode.to_sym == :inactive
+ add_historical_data(service_call) if project_storage.project_folder_mode.to_sym != :inactive
+ service_call
+ end
+
+ private
+
+ def add_historical_data(service_call)
+ project_storage = service_call.result
project_folder = ::Storages::LastProjectFolder
.find_by(
projects_storage_id: project_storage.id,
diff --git a/modules/storages/spec/contracts/storages/last_project_folders/base_contract_spec.rb b/modules/storages/spec/contracts/storages/last_project_folders/base_contract_spec.rb
new file mode 100644
index 00000000000..adcd8f07f1d
--- /dev/null
+++ b/modules/storages/spec/contracts/storages/last_project_folders/base_contract_spec.rb
@@ -0,0 +1,69 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 Storages::LastProjectFolders::BaseContract do
+ include_context 'ModelContract shared context'
+
+ let(:last_project_folder) { build(:last_project_folder) }
+ let(:contract) { described_class.new(last_project_folder, build_stubbed(:admin)) }
+
+ context 'if no project storage is given' do
+ before do
+ last_project_folder.projects_storage = nil
+ end
+
+ it_behaves_like 'contract is invalid'
+ end
+
+ context 'if the project folder mode is `inactive`' do
+ before do
+ last_project_folder.mode = 'inactive'
+ end
+
+ it_behaves_like 'contract is invalid'
+ end
+
+ context 'if the project folder mode is `manual`' do
+ before do
+ last_project_folder.mode = 'manual'
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context 'if the project folder mode is `automatic`' do
+ before do
+ last_project_folder.mode = 'automatic'
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/project_storages/base_contract_spec.rb b/modules/storages/spec/contracts/storages/project_storages/base_contract_spec.rb
index 26b7c240b88..74e98789f6b 100644
--- a/modules/storages/spec/contracts/storages/project_storages/base_contract_spec.rb
+++ b/modules/storages/spec/contracts/storages/project_storages/base_contract_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe Storages::ProjectStorages::BaseContract do
# Creator is not writable in BaseContract; just test base contract writable attributes
let(:project_storage) { build(:project_storage) }
- context 'when the project folder mode is `inactive`' do
+ context 'if the project folder mode is `inactive`' do
before do
project_storage.project_folder_mode = 'inactive'
end
@@ -44,7 +44,15 @@ RSpec.describe Storages::ProjectStorages::BaseContract do
it_behaves_like 'contract is valid'
end
- context 'when the project folder mode is `manual`' do
+ context 'if the project folder mode is `automatic`' do
+ before do
+ project_storage.project_folder_mode = 'automatic'
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context 'if the project folder mode is `manual`' do
before do
project_storage.project_folder_mode = 'manual'
end
diff --git a/modules/storages/spec/factories/last_project_folder_factory.rb b/modules/storages/spec/factories/last_project_folder_factory.rb
new file mode 100644
index 00000000000..e229c4603da
--- /dev/null
+++ b/modules/storages/spec/factories/last_project_folder_factory.rb
@@ -0,0 +1,33 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+FactoryBot.define do
+ factory :last_project_folder, class: '::Storages::LastProjectFolder' do
+ projects_storage factory: :project_storage
+ end
+end