-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
bulk_controller.rb
129 lines (113 loc) · 3.95 KB
/
bulk_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2022 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::BulkController < ApplicationController
before_action :find_work_packages
before_action :authorize
include ProjectsHelper
include CustomFieldsHelper
include RelationsHelper
include QueriesHelper
include WorkPackages::FlashBulkError
def edit
setup_edit
end
def update
@call = ::WorkPackages::Bulk::UpdateService
.new(user: current_user, work_packages: @work_packages)
.call(attributes_for_update)
if @call.success?
flash[:notice] = t(:notice_successful_update)
redirect_back_or_default(controller: '/work_packages', action: :index, project_id: @project)
else
error_flash(@work_packages, @call)
setup_edit
render action: :edit
end
end
def destroy
if WorkPackage.cleanup_associated_before_destructing_if_required(@work_packages, current_user, params[:to_do])
destroy_work_packages(@work_packages)
respond_to do |format|
format.html do
redirect_back_or_default(project_work_packages_path(@work_packages.first.project))
end
format.json do
head :ok
end
end
else
respond_to do |format|
format.html do
render locals: { work_packages: @work_packages,
associated: WorkPackage.associated_classes_to_address_before_destruction_of(@work_packages) }
end
format.json do
render json: { error_message: 'Clean up of associated objects required' }, status: 420
end
end
end
end
private
def setup_edit
@available_statuses = @projects.map { |p| Workflow.available_statuses(p) }.inject(&:&)
@custom_fields = @projects.map(&:all_work_package_custom_fields).inject(&:&)
@assignables = @responsibles = possible_assignees
@types = @projects.map(&:types).inject(&:&)
end
def destroy_work_packages(work_packages)
work_packages.each do |work_package|
WorkPackages::DeleteService
.new(user: current_user,
model: work_package.reload)
.call
rescue ::ActiveRecord::RecordNotFound
# raised by #reload if work package no longer exists
# nothing to do, work package was already deleted (eg. by a parent)
end
end
def possible_assignees
@projects.inject(Principal.all) do |scope, project|
scope.where(id: Principal.possible_assignee(project))
end
end
def attributes_for_update
return {} unless params.has_key? :work_package
permitted_params
.update_work_package
.tap { |attributes| attributes[:custom_field_values]&.reject! { |_k, v| v.blank? } }
.compact_blank
.transform_values { |v| v == 'none' ? '' : v }
.to_h
end
def user
current_user
end
def default_breadcrumb
I18n.t(:label_work_package_plural)
end
end