Skip to content

Commit 433fe6a

Browse files
authored
Merge pull request #2635 from alphagov/fix-no-method-error-when-draft-question-is-wrong-type
Check the draft question's answer type is correct for answer type specific routes
2 parents 1ceb312 + 6401fe9 commit 433fe6a

23 files changed

Lines changed: 344 additions & 113 deletions

app/controllers/pages/address_settings_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Pages::AddressSettingsController < PagesController
2+
before_action { redirect_if_unexpected_answer_type("address") }
3+
24
def new
35
settings = draft_question.answer_settings
46
@address_settings_input = Pages::AddressSettingsInput.new(uk_address: settings.dig(:input_type, :uk_address),

app/controllers/pages/date_settings_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Pages::DateSettingsController < PagesController
2+
before_action { redirect_if_unexpected_answer_type("date") }
3+
24
def new
35
@date_settings_input = Pages::DateSettingsInput.new(input_type: draft_question.answer_settings[:input_type])
46
@date_settings_path = date_settings_create_path(current_form.id)

app/controllers/pages/name_settings_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Pages::NameSettingsController < PagesController
2+
before_action { redirect_if_unexpected_answer_type("name") }
3+
24
def new
35
@name_settings_input = Pages::NameSettingsInput.new(input_type: draft_question.answer_settings[:input_type],
46
title_needed: draft_question.answer_settings[:title_needed])

app/controllers/pages/question_text_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Pages::QuestionTextController < PagesController
2+
before_action { redirect_if_unexpected_answer_type("selection") }
3+
24
def new
35
@question_text_input = Pages::QuestionTextInput.new(question_text: draft_question.question_text)
46
@question_text_path = question_text_create_path(current_form.id)

app/controllers/pages/selection/bulk_options_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Pages::Selection::BulkOptionsController < PagesController
2+
before_action { redirect_if_unexpected_answer_type("selection") }
3+
24
def new
35
@bulk_options_input = Pages::Selection::BulkOptionsInput.new(draft_question:)
46
@bulk_options_input.assign_form_values

app/controllers/pages/selection/none_of_the_above_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Pages::Selection::NoneOfTheAboveController < PagesController
22
include PagesHelper
33

4+
before_action { redirect_if_unexpected_answer_type("selection") }
5+
46
def new
57
@none_of_the_above_input = input_object_from_draft_question
68
@none_of_the_above_path = selection_none_of_the_above_create_path(current_form.id)

app/controllers/pages/selection/options_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Pages::Selection::OptionsController < PagesController
2+
before_action { redirect_if_unexpected_answer_type("selection") }
3+
24
def new
35
@selection_options_input = Pages::Selection::OptionsInput.new(draft_question:)
46
@selection_options_input.assign_form_values

app/controllers/pages/selection/type_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
class Pages::Selection::TypeController < PagesController
22
include PagesHelper
3+
4+
before_action { redirect_if_unexpected_answer_type("selection") }
5+
36
def new
47
@selection_type_input = Pages::Selection::TypeInput.new(only_one_option:, draft_question:)
58
@selection_type_path = selection_type_create_path(current_form.id)

app/controllers/pages/text_settings_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Pages::TextSettingsController < PagesController
2+
before_action { redirect_if_unexpected_answer_type("text") }
3+
24
def new
35
@text_settings_input = Pages::TextSettingsInput.new(input_type: draft_question.answer_settings[:input_type])
46
@text_settings_path = text_settings_create_path(current_form.id)

app/controllers/pages/type_of_answer_controller.rb

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def create
99
@type_of_answer_input = Pages::TypeOfAnswerInput.new(answer_type_form_params)
1010

1111
if @type_of_answer_input.submit
12-
redirect_to next_page_path(current_form.id, @type_of_answer_input.answer_type, :create)
12+
redirect_to next_page_path(@type_of_answer_input.answer_type)
1313
else
1414
@type_of_answer_path = type_of_answer_create_path(current_form.id)
1515
render :type_of_answer, locals: { current_form: }
@@ -26,7 +26,7 @@ def update
2626
@type_of_answer_input = Pages::TypeOfAnswerInput.new(answer_type_form_params)
2727

2828
if @type_of_answer_input.submit
29-
redirect_to next_page_path(current_form.id, @type_of_answer_input.answer_type, :update)
29+
redirect_to next_page_path(@type_of_answer_input.answer_type)
3030
else
3131
@type_of_answer_path = type_of_answer_update_path(current_form.id)
3232
render :type_of_answer
@@ -35,54 +35,15 @@ def update
3535

3636
private
3737

38-
def selection_path(form_id, action)
39-
return question_text_new_path(form_id) if action == :create
40-
41-
selection_type_edit_path(form_id, page.id)
42-
end
43-
44-
def text_path(form_id, action)
45-
action == :create ? text_settings_new_path(form_id) : text_settings_edit_path(form_id)
46-
end
47-
48-
def date_path(form_id, action)
49-
action == :create ? date_settings_new_path(form_id) : date_settings_edit_path(form_id)
50-
end
51-
52-
def address_path(form_id, action)
53-
action == :create ? address_settings_new_path(form_id) : address_settings_edit_path(form_id)
54-
end
55-
56-
def name_path(form_id, action)
57-
action == :create ? name_settings_new_path(form_id) : name_settings_edit_path(form_id)
58-
end
59-
60-
def default_path(form_id, action)
61-
action == :create ? new_question_path(form_id) : edit_question_path(form_id)
62-
end
63-
64-
def next_page_path(form_id, answer_type, action)
65-
case answer_type
66-
when "selection"
67-
selection_path(form_id, action)
68-
when "text"
69-
text_path(form_id, action)
70-
when "date"
71-
date_path(form_id, action)
72-
when "address"
73-
address_path(form_id, action)
74-
when "name"
75-
name_path(form_id, action)
76-
else
77-
default_path(form_id, action)
78-
end
79-
end
80-
8138
def answer_type_form_params
8239
params.require(:pages_type_of_answer_input).permit(:answer_type).merge(draft_question:, current_form:)
8340
end
8441

8542
def answer_type_changed?
8643
@type_of_answer_input.answer_type != @type_of_answer_input.draft_question.answer_type
8744
end
45+
46+
def next_page_path(answer_type)
47+
Pages::AddOrEditQuestionService.new(form_id: current_form.id, existing_page_id: page_id).new_or_edit_path_for_answer_type(answer_type)
48+
end
8849
end

0 commit comments

Comments
 (0)