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

place project archiving into separate controller

ulfertsdba8567f20da
diff --git a/app/controllers/projects/archive_controller.rb b/app/controllers/projects/archive_controller.rb
new file mode 100644
index 00000000000..f61c80d2938
--- /dev/null
+++ b/app/controllers/projects/archive_controller.rb
@@ -0,0 +1,69 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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.
+#++
+
+class Projects::ArchiveController < ApplicationController
+ before_action :find_project_by_project_id
+ before_action :require_admin
+
+ def create
+ change_status_action(:archive)
+ end
+
+ def destroy
+ change_status_action(:unarchive)
+ end
+
+ private
+
+ def change_status_action(status)
+ service_call = change_status(status)
+
+ if service_call.success?
+ redirect_to(project_path_with_status)
+ else
+ flash[:error] = t(:"error_can_not_#{status}_project",
+ errors: service_call.errors.full_messages.join(', '))
+ redirect_back fallback_location: project_path_with_status
+ end
+ end
+
+ def change_status(status)
+ "Projects::#{status.to_s.camelcase}Service"
+ .constantize
+ .new(user: current_user, model: @project)
+ .call
+ end
+
+ def project_path_with_status
+ acceptable_params = params.permit(:status).to_h.compact.select { |_, v| v.present? }
+
+ projects_path(acceptable_params)
+ end
+end
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index 5dff8b311d8..6846fc4297b 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -35,7 +35,7 @@ class ProjectsController < ApplicationController
before_action :find_project, except: %i[index level_list new]
before_action :authorize, only: %i[copy]
before_action :authorize_global, only: %i[new]
- before_action :require_admin, only: %i[archive unarchive destroy destroy_info]
+ before_action :require_admin, only: %i[destroy destroy_info]
include SortHelper
include PaginationHelper
@@ -83,14 +83,6 @@ class ProjectsController < ApplicationController
end
end
- def archive
- change_status_action(:archive)
- end
-
- def unarchive
- change_status_action(:unarchive)
- end
-
# Delete @project
def destroy
service_call = ::Projects::ScheduleDeletionService
@@ -104,7 +96,6 @@ class ProjectsController < ApplicationController
end
redirect_to project_path_with_status
- update_demo_project_settings @project, false
end
def destroy_info
@@ -132,26 +123,6 @@ class ProjectsController < ApplicationController
render_404
end
- def change_status_action(status)
- service_call = change_status(status)
-
- if service_call.success?
- update_demo_project_settings @project, status == :archive
- redirect_to(project_path_with_status)
- else
- flash[:error] = t(:"error_can_not_#{status}_project",
- errors: service_call.errors.full_messages.join(', '))
- redirect_back fallback_location: project_path_with_status
- end
- end
-
- def change_status(status)
- "Projects::#{status.to_s.camelcase}Service"
- .constantize
- .new(user: current_user, model: @project)
- .call
- end
-
def redirect_work_packages_or_overview
return if redirect_to_project_menu_item(@project, :work_packages)
@@ -194,11 +165,4 @@ class ProjectsController < ApplicationController
def set_sorting(query)
query.orders.select(&:valid?).map { |o| [o.attribute.to_s, o.direction.to_s] }
end
-
- def update_demo_project_settings(project, value)
- # e.g. when one of the demo projects gets deleted or a archived
- if project.identifier == 'your-scrum-project' || project.identifier == 'demo-project'
- Setting.demo_projects_available = value
- end
- end
end
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index f5edca5ffe7..7dc5d701ee9 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -96,9 +96,9 @@ module ProjectsHelper
def project_more_menu_archive_item(project)
if User.current.admin? && project.active?
[t(:button_archive),
- archive_project_path(project, status: params[:status]),
+ project_archive_path(project, status: params[:status]),
{ data: { confirm: t('project.archive.are_you_sure', name: project.name) },
- method: :put,
+ method: :post,
class: 'icon-context icon-locked',
title: t(:button_archive) }]
end
@@ -107,8 +107,8 @@ module ProjectsHelper
def project_more_menu_unarchive_item(project)
if User.current.admin? && !project.active? && (project.parent.nil? || project.parent.active?)
[t(:button_unarchive),
- unarchive_project_path(project, status: params[:status]),
- { method: :put,
+ project_archive_path(project, status: params[:status]),
+ { method: :delete,
class: 'icon-context icon-unlocked',
title: t(:button_unarchive) }]
end
diff --git a/app/services/projects/archive_service.rb b/app/services/projects/archive_service.rb
index 9f5ccd453ac..78e52e9223c 100644
--- a/app/services/projects/archive_service.rb
+++ b/app/services/projects/archive_service.rb
@@ -31,6 +31,7 @@
module Projects
class ArchiveService < ::BaseServices::BaseContracted
include Contracted
+ include Projects::Concerns::UpdateDemoData
def initialize(user:, model:, contract_class: Projects::ArchiveContract)
super(user: user, contract_class: contract_class)
diff --git a/app/services/projects/concerns/update_demo_data.rb b/app/services/projects/concerns/update_demo_data.rb
new file mode 100644
index 00000000000..2cf0e928f3c
--- /dev/null
+++ b/app/services/projects/concerns/update_demo_data.rb
@@ -0,0 +1,46 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 Projects::Concerns
+ module UpdateDemoData
+ private
+
+ def after_perform(call)
+ project = call.result
+
+ # e.g. when one of the demo projects gets deleted or archived
+ if %w[your-scrum-project demo-project].include?(project.identifier)
+ Setting.demo_projects_available = !project.destroyed? && !project.archived?
+ end
+
+ super
+ end
+ end
+end
diff --git a/app/services/projects/delete_service.rb b/app/services/projects/delete_service.rb
index b481bfd6f84..96c9ee16faf 100644
--- a/app/services/projects/delete_service.rb
+++ b/app/services/projects/delete_service.rb
@@ -30,6 +30,8 @@
module Projects
class DeleteService < ::BaseServices::Delete
+ include Projects::Concerns::UpdateDemoData
+
def call(*)
super.tap do |service_call|
notify(service_call.success?)
diff --git a/app/services/projects/unarchive_service.rb b/app/services/projects/unarchive_service.rb
index 97820796fb0..452af683156 100644
--- a/app/services/projects/unarchive_service.rb
+++ b/app/services/projects/unarchive_service.rb
@@ -31,6 +31,7 @@
module Projects
class UnarchiveService < ::BaseServices::BaseContracted
include Contracted
+ include Projects::Concerns::UpdateDemoData
def initialize(user:, model:, contract_class: Projects::UnarchiveContract)
super(user: user, contract_class: contract_class)
diff --git a/app/views/projects/_toolbar.html.erb b/app/views/projects/_toolbar.html.erb
index d5224b1ff0a..98c904544d6 100644
--- a/app/views/projects/_toolbar.html.erb
+++ b/app/views/projects/_toolbar.html.erb
@@ -55,9 +55,9 @@ See COPYRIGHT and LICENSE files for more details.
<% end %>
<% if User.current.admin? %>
<li class="toolbar-item hidden-for-mobile">
- <%= link_to(archive_project_path(@project, status: '', name: @project.name),
+ <%= link_to(project_archive_path(@project, status: '', name: @project.name),
data: { confirm: t('project.archive.are_you_sure', name: @project.name) },
- method: :put,
+ method: :post,
title: t(:label_archive_project),
class: 'button') do %>
<%= op_icon('button--icon icon-locked') %>
diff --git a/config/routes.rb b/config/routes.rb
index f45553716f6..a8f193f6632 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -189,6 +189,7 @@ OpenProject::Application.routes.draw do
end
resource :templated, only: %i[create destroy], controller: 'templated'
+ resource :archive, only: %i[create destroy], controller: 'archive'
end
member do
@@ -199,8 +200,6 @@ OpenProject::Application.routes.draw do
get :copy
- put :archive
- put :unarchive
patch :types
# Destroy uses a get request to prompt the user before the actual DELETE request
diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb
index 119fa5c9186..12cb45db343 100644
--- a/spec/controllers/projects_controller_spec.rb
+++ b/spec/controllers/projects_controller_spec.rb
@@ -174,20 +174,6 @@ describe ProjectsController, type: :controller do
expect { project.reload }.not_to raise_error
end
-
- it 'should archive' do
- put :archive, params: { id: project.id }
-
- expect(project.reload).to be_archived
- end
-
- it 'should unarchive' do
- project.update(active: false)
- put :unarchive, params: { id: project.id }
-
- expect(project.reload).to be_active
- expect(project).not_to be_archived
- end
end
describe '#copy' do