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

Generate `BulkController`

Aaron Contreras7e0668dba302
diff --git a/app/controllers/work_packages/shares/bulk_controller.rb b/app/controllers/work_packages/shares/bulk_controller.rb
new file mode 100644
index 00000000000..f38e76e5ec0
--- /dev/null
+++ b/app/controllers/work_packages/shares/bulk_controller.rb
@@ -0,0 +1,98 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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.
+# ++
+
+class WorkPackages::Shares::BulkController < ApplicationController
+ include OpTurbo::ComponentStream
+
+ before_action :find_work_package
+ before_action :find_shares
+ before_action :find_project
+ before_action :authorize
+
+ def update; end
+
+ def destroy
+ @shares.each do |share|
+ WorkPackageMembers::DeleteService
+ .new(user: current_user, model: share)
+ .call
+ end
+
+ if current_visible_member_count.zero?
+ respond_with_replace_modal
+ else
+ respond_with_remove_shares
+ end
+ end
+
+ private
+
+ def respond_with_replace_modal
+ replace_via_turbo_stream(
+ component: WorkPackages::Share::ModalBodyComponent.new(work_package: @work_package)
+ )
+
+ respond_with_turbo_streams
+ end
+
+ def respond_with_remove_shares
+ @shares.each do |share|
+ remove_via_turbo_stream(
+ component: WorkPackages::Share::ShareRowComponent.new(share:)
+ )
+ end
+
+ update_via_turbo_stream(
+ component: WorkPackages::Share::CounterComponent.new(work_package: @work_package, count: current_visible_member_count)
+ )
+
+ respond_with_turbo_streams
+ end
+
+ def find_work_package
+ @work_package = WorkPackage.find(params[:work_package_id])
+ end
+
+ def find_project
+ @project = @work_package.project
+ end
+
+ def find_shares
+ @shares = Member.of_work_package(@work_package).where(id: params[:share_ids])
+ end
+
+ def current_visible_member_count
+ @current_visible_member_count ||= Member
+ .joins(:member_roles)
+ .of_work_package(@work_package)
+ .merge(MemberRole.only_non_inherited)
+ .size
+ end
+end
diff --git a/config/routes.rb b/config/routes.rb
index 072a4491568..fb2aba23b3a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -468,7 +468,11 @@ OpenProject::Application.routes.draw do
get 'details/*state' => 'work_packages#index', on: :collection, as: :details
# Rails managed sharing route
- resources :shares, controller: 'work_packages/shares', only: %i[index create]
+ resources :shares, controller: 'work_packages/shares', only: %i[index create] do
+ collection do
+ resource :bulk, controller: 'work_packages/shares/bulk', only: %i[update destroy], as: :shares_bulk
+ end
+ end
# states managed by client-side (angular) routing on work_package#show
get '/' => 'work_packages#index', on: :collection, as: 'index'
diff --git a/spec/routing/work_package/shares/bulk_spec.rb b/spec/routing/work_package/shares/bulk_spec.rb
new file mode 100644
index 00000000000..6db0452a393
--- /dev/null
+++ b/spec/routing/work_package/shares/bulk_spec.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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'
+
+RSpec.describe 'Work package bulk sharing routing' do
+ describe 'DELETE /work_packages/:work_package_id/shares' do
+ it 'routes to work_packages/shares/bulk#destroy' do
+ expect(delete('/work_packages/1/shares/bulk'))
+ .to route_to(controller: 'work_packages/shares/bulk',
+ action: 'destroy',
+ work_package_id: '1')
+ end
+ end
+
+ describe 'PATCH /work_packages/:work_package_id/shares' do
+ it 'routes to work_packages/shares/bulk#update' do
+ expect(patch('/work_packages/1/shares/bulk'))
+ .to route_to(controller: 'work_packages/shares/bulk',
+ action: 'update',
+ work_package_id: '1')
+ end
+ end
+end