11module Forms
22 class BaseController < ApplicationController
3- prepend_before_action :set_form
3+ prepend_before_action :prepare_context
44 around_action :set_locale
55
66 def redirect_to_friendly_url_start
@@ -15,7 +15,6 @@ def redirect_to_friendly_url_start
1515 end
1616
1717 def error_repeat_submission
18- @current_context = Flow ::Context . new ( form : @form , store : session )
1918 render template : "errors/repeat_submission" , locals : { form : @form }
2019 end
2120
@@ -27,9 +26,7 @@ def set_request_logging_attributes
2726
2827 private
2928
30- def current_context
31- @current_context ||= Flow ::Context . new ( form : @form , store : session )
32- end
29+ attr_reader :current_context
3330
3431 def mode
3532 @mode ||= Mode . new ( params [ :mode ] )
@@ -43,29 +40,39 @@ def set_available_languages
4340 @available_languages = current_context . form . available_languages if current_context . form . multilingual?
4441 end
4542
46- def set_form
47- form_id = params . require ( :form_id )
48- @form = Api ::V2 ::FormDocumentRepository . find_with_mode ( form_id :, mode :, language : locale )
49-
50- if @form . blank?
51- I18n . with_locale ( locale ) do
52- if locale == "cy"
53- archived_welsh_version = Api ::V2 ::FormDocumentRepository . find ( form_id :, tag : :archived , language : :cy )
54- live_english_version = Api ::V2 ::FormDocumentRepository . find ( form_id :, tag : :live , language : :en )
55-
56- if archived_welsh_version . present? && live_english_version . present?
57- return render template : "forms/archived_welsh/show" ,
58- locals : { form : live_english_version } ,
59- status : :not_found
60- end
61- end
43+ def prepare_context
44+ form_document = Api ::V2 ::FormDocumentRepository . find_with_mode ( form_id :, mode :, language : locale )
45+
46+ return handle_form_not_found if form_document . blank?
47+
48+ @form = Form . new ( form_document )
49+ raise ActiveResource ::ResourceNotFound , "Form has no steps" unless @form . start_page
50+
51+ @current_context = Flow ::Context . new ( form : @form , form_document :, store : session )
52+ end
6253
63- archived_form = Api ::V2 ::FormDocumentRepository . find ( form_id :, tag : :archived )
64- return render template : "forms/archived/show" , locals : { form_name : archived_form . name } , status : :not_found if archived_form . present?
54+ def handle_form_not_found
55+ I18n . with_locale ( locale ) do
56+ if locale == "cy"
57+ archived_welsh_form_document = Api ::V2 ::FormDocumentRepository . find ( form_id :, tag : :archived , language : :cy )
58+ live_english_form_document = Api ::V2 ::FormDocumentRepository . find ( form_id :, tag : :live , language : :en )
59+
60+ if archived_welsh_form_document . present? && live_english_form_document . present?
61+ return render template : "forms/archived_welsh/show" ,
62+ locals : { form : Form . new ( live_english_form_document ) } ,
63+ status : :not_found
64+ end
6565 end
66+
67+ archived_form_document = Api ::V2 ::FormDocumentRepository . find ( form_id :, tag : :archived )
68+ return render template : "forms/archived/show" , locals : { form_name : archived_form_document . name } , status : :not_found if archived_form_document . present?
6669 end
6770
68- raise ActiveResource ::ResourceNotFound , "Not Found" unless @form . present? && @form . start_page
71+ raise ActiveResource ::ResourceNotFound , "Not Found"
72+ end
73+
74+ def form_id
75+ params . require ( :form_id )
6976 end
7077 end
7178end
0 commit comments