Skip to content

Commit 0cd7b57

Browse files
committed
Add handling when group is deleted during move
1 parent a8685c3 commit 0cd7b57

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

app/controllers/group_forms_controller.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,20 @@ def update
4444

4545
if @group_select.valid?
4646
receiving_group = Group.find_by(external_id: @group_select.group)
47-
@group_form.update!(group: receiving_group)
48-
success_message = t(".success", form_name: @form.name, receiving_group_name: receiving_group.name)
4947

50-
redirect_to @group, success: success_message, status: :see_other
48+
# In case the receiving group is deleted since the form was loaded
49+
if receiving_group
50+
@group_form.update!(group: receiving_group)
51+
success_message = t(".success", form_name: @form.name, receiving_group_name: receiving_group.name)
52+
53+
redirect_to @group, success: success_message, status: :see_other
54+
else
55+
@group_select = Forms::GroupSelect.new(group: @group, form: @form)
56+
@group_select_presenter = Forms::GroupSelectPresenter.call(group: @group, groups: @group_select.groups, form: @form)
57+
@group_select.errors.add(:group, :gone)
58+
59+
render :edit, status: :unprocessable_content
60+
end
5161
else
5262
@group_select = Forms::GroupSelect.new(group: @group, form: @form)
5363
@group_select_presenter = Forms::GroupSelectPresenter.call(group: @group, groups: @group_select.groups, form: @form)

config/locales/input_objects/group_select.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ en:
77
attributes:
88
group:
99
blank: Select the group you want to move this form to
10+
gone: The group you selected no longer exists
1011
helpers:
1112
hint:
1213
forms_group_select:

spec/features/group_form/move_form_spec.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@
4141
given_i_am_logged_in_as_an_organisation_admin
4242
and_i_see_the_move_form_page
4343
when_i_do_not_choose_a_group
44-
then_i_see_an_error_message
44+
then_i_see_an_error_message_to_select_a_group
45+
end
46+
47+
scenario "receiving group is deleted before moving form" do
48+
given_i_am_logged_in_as_an_organisation_admin
49+
and_i_see_the_move_form_page
50+
and_the_receiving_group_is_deleted
51+
when_i_change_the_group
52+
then_i_see_an_error_message_the_group_is_gone
4553
end
4654
end
4755

@@ -87,6 +95,10 @@ def and_i_see_the_move_form_page
8795
expect(page.find("h1")).to have_content("#{form.name}\n-\nMove this form to a different group")
8896
end
8997

98+
def and_the_receiving_group_is_deleted
99+
another_group.destroy!
100+
end
101+
90102
def when_i_change_the_group
91103
choose(another_group.name)
92104
click_button "Continue"
@@ -109,8 +121,13 @@ def when_i_do_not_choose_a_group
109121
click_button "Continue"
110122
end
111123

112-
def then_i_see_an_error_message
124+
def then_i_see_an_error_message_to_select_a_group
113125
expect(page).to have_content("There is a problem")
114126
expect(page).to have_content("Select the group you want to move this form to")
115127
end
128+
129+
def then_i_see_an_error_message_the_group_is_gone
130+
expect(page).to have_content("There is a problem")
131+
expect(page).to have_content("The group you selected no longer exists")
132+
end
116133
end

0 commit comments

Comments
 (0)