-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrb_sprints_controller.rb
More file actions
199 lines (164 loc) · 6.24 KB
/
rb_sprints_controller.rb
File metadata and controls
199 lines (164 loc) · 6.24 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# 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.
#++
class RbSprintsController < RbApplicationController
include OpTurbo::ComponentStream
NEW_SPRINT_ACTIONS = %i[new_dialog
edit_dialog
create
refresh_form
update_agile_sprint].freeze
skip_before_action :load_sprint_and_project, only: NEW_SPRINT_ACTIONS
before_action :not_authorized_on_feature_flag_inactive,
:load_project,
only: NEW_SPRINT_ACTIONS
def new_dialog
call = Sprints::SetAttributesService.new(
user: current_user,
model: Agile::Sprint.new,
contract_class: EmptyContract
).call(attributes: converted_agile_sprint_params)
respond_with_dialog Backlogs::NewSprintDialogComponent.new(sprint: call.result)
end
def edit_dialog
@sprint = Agile::Sprint.for_project(@project).visible.find(params[:id])
respond_with_dialog Backlogs::NewSprintDialogComponent.new(sprint: @sprint, state: :edit)
end
def refresh_form
id = edit_agile_sprint_params.dig(:sprint, :id)
sprint = id.present? ? Agile::Sprint.for_project(@project).visible.find(id) : Agile::Sprint.new
call = Sprints::SetAttributesService.new(
user: current_user,
model: sprint,
contract_class: EmptyContract
).call(attributes: converted_agile_sprint_params)
update_via_turbo_stream(component: Backlogs::NewSprintFormComponent.new(sprint: call.result))
respond_with_turbo_streams
end
def create # rubocop:disable Metrics/AbcSize
call = Sprints::CreateService
.new(user: current_user)
.call(attributes: converted_agile_sprint_params)
if call.success?
flash[:notice] = I18n.t(:notice_successful_create)
render turbo_stream: turbo_stream.redirect_to(sprint_planning_backlogs_project_backlogs_path(@project))
else
update_new_sprint_form_component_via_turbo_stream(sprint: call.result, base_errors: call.errors[:base])
respond_with_turbo_streams
end
end
# Called like this due to `update` being taken by legacy sprints.
def update_agile_sprint # rubocop:disable Metrics/AbcSize
@sprint = Agile::Sprint.for_project(@project).visible.find(params[:id])
call = Sprints::UpdateService
.new(user: current_user, model: @sprint)
.call(attributes: agile_sprint_params[:sprint])
if call.success?
render_success_flash_message_via_turbo_stream(message: I18n.t(:notice_successful_update))
update_sprint_header_component_via_turbo_stream(sprint: call.result)
else
update_new_sprint_form_component_via_turbo_stream(sprint: call.result, base_errors: call.errors[:base])
end
respond_with_turbo_streams
end
def edit_name
update_header_component_via_turbo_stream(state: :edit)
respond_with_turbo_streams
end
def show_name
update_header_component_via_turbo_stream(state: :show)
respond_with_turbo_streams
end
def update
call = Versions::UpdateService
.new(user: current_user, model: @sprint)
.call(attributes: sprint_params)
if call.success?
status = 200
state = :show
@sprint = call.result
render_success_flash_message_via_turbo_stream(message: I18n.t(:notice_successful_update))
else
status = 422
state = :edit
render_error_flash_message_via_turbo_stream(
message: I18n.t(:notice_unsuccessful_update_with_reason, reason: call.message)
)
end
update_header_component_via_turbo_stream(state:)
respond_with_turbo_streams(status:)
end
private
def update_header_component_via_turbo_stream(state: :show)
@backlog = Backlog.for(sprint: @sprint, project: @project)
update_via_turbo_stream(
component: Backlogs::BacklogHeaderComponent.new(
backlog: @backlog,
project: @project,
state:
),
method: :morph
)
end
def update_sprint_header_component_via_turbo_stream(sprint:)
update_via_turbo_stream(
component: Backlogs::SprintHeaderComponent.new(sprint:,
project: @project),
method: :morph
)
end
def update_new_sprint_form_component_via_turbo_stream(sprint:, base_errors: nil)
update_via_turbo_stream(
component: Backlogs::NewSprintFormComponent.new(
sprint:,
base_errors:
),
status: :bad_request
)
end
# Overrides load_sprint_and_project to load the sprint from :id instead of :sprint_id
def load_sprint_and_project
@sprint = Sprint.visible.find(params[:id])
load_project
end
def sprint_params
params.expect(sprint: %i[name start_date effective_date])
end
def agile_sprint_params
params.permit(sprint: %i[name start_date finish_date])
end
def edit_agile_sprint_params
params.permit(sprint: %i[id name start_date finish_date])
end
def converted_agile_sprint_params
# Do some preprocessing to make the params easier to use
converted_sprint_params = agile_sprint_params[:sprint].to_h
converted_sprint_params[:project] = @project
converted_sprint_params
end
end