Skip to content

Commit 69eac27

Browse files
Allow GovukNotifyPersonalisation to handle session = nil
This fixes a bug for templates which don't need to reference a session.
1 parent 9cb7452 commit 69eac27

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

app/lib/govuk_notify_personalisation.rb

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def not_catch_up
107107
end
108108

109109
def consent_deadline
110+
return nil if session.nil?
111+
110112
next_date = session.future_dates.first
111113

112114
close_consent_at =
@@ -117,6 +119,7 @@ def consent_deadline
117119

118120
def consent_link
119121
return nil if session.nil? || programmes.empty?
122+
120123
host +
121124
start_parent_interface_consent_forms_path(
122125
session,
@@ -173,41 +176,41 @@ def location_name
173176
if vaccination_record
174177
vaccination_record_location(vaccination_record)
175178
else
176-
session.location.name
179+
session&.location&.name
177180
end
178181
end
179182

180183
def next_or_today_session_date
181-
session.next_date(include_today: true)&.to_fs(:short_day_of_week)
184+
session&.next_date(include_today: true)&.to_fs(:short_day_of_week)
182185
end
183186

184187
def next_or_today_session_dates
185188
session
186-
.today_or_future_dates
187-
.map { it.to_fs(:short_day_of_week) }
188-
.to_sentence
189+
&.today_or_future_dates
190+
&.map { it.to_fs(:short_day_of_week) }
191+
&.to_sentence
189192
end
190193

191194
def next_or_today_session_dates_or
192195
session
193-
.today_or_future_dates
194-
.map { it.to_fs(:short_day_of_week) }
195-
.to_sentence(last_word_connector: ", or ", two_words_connector: " or ")
196+
&.today_or_future_dates
197+
&.map { it.to_fs(:short_day_of_week) }
198+
&.to_sentence(last_word_connector: ", or ", two_words_connector: " or ")
196199
end
197200

198201
def next_session_date
199-
session.next_date(include_today: false)&.to_fs(:short_day_of_week)
202+
session&.next_date(include_today: false)&.to_fs(:short_day_of_week)
200203
end
201204

202205
def next_session_dates
203-
session.future_dates.map { it.to_fs(:short_day_of_week) }.to_sentence
206+
session&.future_dates&.map { it.to_fs(:short_day_of_week) }&.to_sentence
204207
end
205208

206209
def next_session_dates_or
207210
session
208-
.future_dates
209-
.map { it.to_fs(:short_day_of_week) }
210-
.to_sentence(last_word_connector: ", or ", two_words_connector: " or ")
211+
&.future_dates
212+
&.map { it.to_fs(:short_day_of_week) }
213+
&.to_sentence(last_word_connector: ", or ", two_words_connector: " or ")
211214
end
212215

213216
def outcome_administered
@@ -263,6 +266,8 @@ def show_additional_instructions
263266
end
264267

265268
def subsequent_session_dates_offered_message
269+
return nil if session.nil?
270+
266271
dates = session.future_dates.drop(1)
267272
return "" if dates.empty?
268273

@@ -304,7 +309,10 @@ def talk_to_your_child_message
304309
].join("\n\n")
305310
end
306311

307-
delegate :privacy_notice_url, :privacy_policy_url, to: :team, prefix: true
312+
delegate :privacy_notice_url,
313+
:privacy_policy_url,
314+
to: :team,
315+
prefix: true
308316

309317
def today_or_date_of_vaccination
310318
return if vaccination_record.nil?

spec/lib/govuk_notify_personalisation_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,13 @@
513513
end
514514
end
515515
end
516+
517+
context "with session nil" do
518+
let(:session) { nil }
519+
let(:consent) { create(:consent, patient:) }
520+
521+
it "doesn't throw an error" do
522+
expect { to_h }.not_to raise_error
523+
end
524+
end
516525
end

0 commit comments

Comments
 (0)