|
28 | 28 | # See COPYRIGHT and LICENSE files for more details.
|
29 | 29 | #++
|
30 | 30 |
|
31 |
| -class WorkPackageChildrenController < ApplicationController |
| 31 | +class WorkPackageChildrenRelationsController < ApplicationController |
32 | 32 | include OpTurbo::ComponentStream
|
33 | 33 | include OpTurbo::DialogStreamHelper
|
34 | 34 |
|
35 | 35 | before_action :set_work_package
|
36 | 36 |
|
37 | 37 | before_action :authorize # Short-circuit early if not authorized
|
38 | 38 |
|
39 |
| - before_action :set_child, except: %i[new create] |
40 |
| - before_action :set_relations, except: %i[new create] |
41 |
| - |
42 | 39 | def new
|
43 | 40 | component = WorkPackageRelationsTab::AddWorkPackageChildDialogComponent
|
44 | 41 | .new(work_package: @work_package)
|
45 | 42 | respond_with_dialog(component)
|
46 | 43 | end
|
47 | 44 |
|
48 | 45 | def create
|
49 |
| - target_work_package_id = params[:work_package][:id] |
50 |
| - target_child_work_package = WorkPackage.find(target_work_package_id) |
| 46 | + child = WorkPackage.find(params[:work_package][:id]) |
| 47 | + service_result = set_relation(child:, parent: @work_package) |
51 | 48 |
|
52 |
| - target_child_work_package.parent = @work_package |
| 49 | + respond_with_relations_tab_update(service_result) |
| 50 | + end |
53 | 51 |
|
54 |
| - if target_child_work_package.save |
55 |
| - @children = @work_package.children.visible |
56 |
| - @relations = @work_package.relations.visible |
| 52 | + def destroy |
| 53 | + child = WorkPackage.find(params[:id]) |
| 54 | + service_result = set_relation(child:, parent: nil) |
57 | 55 |
|
58 |
| - component = WorkPackageRelationsTab::IndexComponent.new( |
59 |
| - work_package: @work_package, |
60 |
| - relations: @relations, |
61 |
| - children: @children |
62 |
| - ) |
63 |
| - replace_via_turbo_stream(component:) |
64 |
| - update_flash_message_via_turbo_stream( |
65 |
| - message: I18n.t(:notice_successful_update), scheme: :success |
66 |
| - ) |
67 |
| - respond_with_turbo_streams |
68 |
| - end |
| 56 | + respond_with_relations_tab_update(service_result) |
69 | 57 | end
|
70 | 58 |
|
71 |
| - def destroy |
72 |
| - @child.parent = nil |
| 59 | + private |
73 | 60 |
|
74 |
| - if @child.save |
| 61 | + def set_relation(child:, parent:) |
| 62 | + WorkPackages::UpdateService.new(user: current_user, model: child) |
| 63 | + .call(parent:) |
| 64 | + end |
| 65 | + |
| 66 | + def respond_with_relations_tab_update(service_result) |
| 67 | + if service_result.success? |
75 | 68 | @work_package.reload
|
76 |
| - @children = @work_package.children.visible |
77 | 69 | component = WorkPackageRelationsTab::IndexComponent.new(
|
78 | 70 | work_package: @work_package,
|
79 |
| - relations: @relations, |
80 |
| - children: @children |
| 71 | + relations: @work_package.relations.visible, |
| 72 | + children: @work_package.children.visible |
81 | 73 | )
|
82 | 74 | replace_via_turbo_stream(component:)
|
83 | 75 | update_flash_message_via_turbo_stream(
|
84 | 76 | message: I18n.t(:notice_successful_update), scheme: :success
|
85 | 77 | )
|
86 | 78 |
|
87 | 79 | respond_with_turbo_streams
|
| 80 | + else |
| 81 | + respond_with_turbo_streams(status: :unprocessable_entity) |
88 | 82 | end
|
89 | 83 | end
|
90 | 84 |
|
91 |
| - private |
92 |
| - |
93 | 85 | def set_work_package
|
94 | 86 | @work_package = WorkPackage.find(params[:work_package_id])
|
95 | 87 | @project = @work_package.project
|
96 | 88 | end
|
97 |
| - |
98 |
| - def set_child |
99 |
| - @child = WorkPackage.find(params[:id]) |
100 |
| - end |
101 |
| - |
102 |
| - def set_relations |
103 |
| - @relations = @work_package.relations.visible |
104 |
| - end |
105 | 89 | end
|
0 commit comments