Skip to content

Commit e580eb0

Browse files
committed
Save parent relationship when editing consent
This follows on from 46c2fa5 which didn't solve the original problem. In that commit only the check and confirm page was updated to show the correct parent relationship, but when we came to actually confirming the consent, the parent relationship itself still wasn't changing. This fixes that by replacing the use of `find_or_initialize_by` with a simple `find` which ensures that the parent relationships remain attached to the parent and therefore can be saved. I've also updated the test to ensure this is working as expected and won't regress in the future. Jira-Issue: MAV-790
1 parent b4cba32 commit e580eb0

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

app/models/draft_consent.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,23 @@ def parent
174174
parent.phone = parent_phone
175175
parent.phone_receive_updates = parent_phone_receive_updates
176176

177-
parent
178-
.parent_relationships
179-
.find_or_initialize_by(patient:)
180-
.assign_attributes(
181-
type: parent_relationship_type,
182-
other_name: parent_relationship_other_name
183-
)
177+
# We can't use find_or_initialize_by here because we need the object to
178+
# remain attached to the parent so we can save the parent with its
179+
# relationships.
180+
181+
parent_relationship =
182+
parent.parent_relationships.find { it.patient_id == patient_id } ||
183+
parent.parent_relationships.build(patient_id:)
184+
185+
parent_relationship.assign_attributes(
186+
patient:, # acts as preload
187+
type: parent_relationship_type,
188+
other_name: parent_relationship_other_name
189+
)
190+
191+
if parent_relationship.new_record?
192+
parent.parent_relationships << parent_relationship
193+
end
184194

185195
parent
186196
end

spec/features/verbal_consent_change_answers_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
relationship: "Dad"
1919
)
2020
then_i_see_the_confirmation_page
21+
22+
when_i_confirm_the_consent
23+
and_i_click_on_the_patient
24+
then_i_see_the_new_parent_details
2125
end
2226

2327
def given_a_patient_is_in_an_hpv_programme
@@ -84,4 +88,17 @@ def then_i_see_the_confirmation_page
8488
def when_i_click_on_change_name
8589
click_link "Change name"
8690
end
91+
92+
def when_i_confirm_the_consent
93+
click_button "Confirm"
94+
end
95+
96+
def and_i_click_on_the_patient
97+
click_link @patient.full_name, match: :first
98+
end
99+
100+
def then_i_see_the_new_parent_details
101+
expect(page).to have_content("New parent name")
102+
expect(page).to have_content("Dad")
103+
end
87104
end

0 commit comments

Comments
 (0)