Skip to content

Commit 1562c70

Browse files
committed
Display multiple branches on add/edit page
This is based on the Group's multiple_branches_enabled feature flag - so if this is toggled off, then it will use the original add and edit page view. When the flag is turned on for the group, the display logic assumes that the form has been created using the new Routes process, and will not be particularly compatible with forms that have been created using the old routing pages (although it might work if the routing is relatively simple). There is no validation displayed for these changes, as we're not ready to start deciding how to validate the new multiple-branch routes.
1 parent f8380af commit 1562c70

7 files changed

Lines changed: 142 additions & 34 deletions

File tree

app/components/page_list_component/view.html.erb

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,57 @@
3232
</dd>
3333
</div>
3434

35-
<% page.routing_conditions.each do |condition| %>
36-
<% condition_page = self.condition_page(condition) %>
37-
<% condition_page_position = self.condition_page_position(condition) %>
38-
<div id="<%= PageListComponent::ErrorSummary::View.error_id(condition.id) %>" class="govuk-summary-list__row app-page-list__row <%= class_names( "govuk-form-group--error": condition.has_routing_errors?) %>">
35+
<% if @form.group.multiple_branches_enabled? && page.routing_conditions.present? %>
36+
<div class="govuk-summary-list__row">
3937
<dt class="govuk-summary-list__key govuk-summary-list__key app-page-list__key">
40-
<%= t("page_conditions.condition_name", question_number: condition_page_position) %>
38+
<%= t("page_conditions.condition_name", question_number: page.position) %>
4139
</dt>
42-
4340
<dd class="govuk-summary-list__value">
44-
<ul class="govuk-list govuk-!-margin-0">
45-
<% condition.validation_errors.each do |error| %>
46-
<li class="app-page_list__route-text--error">
47-
<%= PageListComponent::ErrorSummary::View.generate_error_message(error.name, condition:, page: condition_page) %>
48-
</li>
49-
<% end %>
50-
</ul>
51-
52-
<p>
53-
<%= condition_description(condition) %>
54-
</p>
55-
56-
<dd class="govuk-summary-list__actions govuk-!-padding-bottom-6">
57-
<%= govuk_link_to show_routes_path(form_id: @form.id, page_id: condition.check_page_id) do %>
58-
<%= t("forms.form_overview.edit_with_visually_hidden_text_html", visually_hidden_text: t("page_conditions.condition_name", question_number: condition_page_position)) %>
41+
<% if page.routing_conditions.first.answer_value.present? %>
42+
<p><%= I18n.t("page_conditions.if_answer_is") %></p>
43+
<ul class="govuk-list govuk-list--bullet">
44+
<% page.routing_conditions.each do |condition| %>
45+
<li>
46+
<%= branch_condition_description(condition) %>
47+
</li>
48+
<% end %>
49+
</ul>
50+
<% else %>
51+
<p>
52+
<%= branch_condition_description(page.routing_conditions.first) %>
53+
</p>
5954
<% end %>
6055
</dd>
6156
</div>
57+
<% else %>
58+
<% page.routing_conditions.each do |condition| %>
59+
<% condition_page = self.condition_page(condition) %>
60+
<% condition_page_position = self.condition_page_position(condition) %>
61+
<div id="<%= PageListComponent::ErrorSummary::View.error_id(condition.id) %>" class="govuk-summary-list__row app-page-list__row <%= class_names( "govuk-form-group--error": condition.has_routing_errors?) %>">
62+
<dt class="govuk-summary-list__key govuk-summary-list__key app-page-list__key">
63+
<%= t("page_conditions.condition_name", question_number: condition_page_position) %>
64+
</dt>
65+
66+
<dd class="govuk-summary-list__value">
67+
<ul class="govuk-list govuk-!-margin-0">
68+
<% condition.validation_errors.each do |error| %>
69+
<li class="app-page_list__route-text--error">
70+
<%= PageListComponent::ErrorSummary::View.generate_error_message(error.name, condition:, page: condition_page) %>
71+
</li>
72+
<% end %>
73+
</ul>
74+
75+
<p>
76+
<%= condition_description(condition) %>
77+
</p>
78+
79+
<dd class="govuk-summary-list__actions govuk-!-padding-bottom-6">
80+
<%= govuk_link_to show_routes_path(form_id: @form.id, page_id: condition.check_page_id) do %>
81+
<%= t("forms.form_overview.edit_with_visually_hidden_text_html", visually_hidden_text: t("page_conditions.condition_name", question_number: condition_page_position)) %>
82+
<% end %>
83+
</dd>
84+
</div>
85+
<% end %>
6286
<% end %>
6387
<% end %>
6488
</dl>

app/components/page_list_component/view.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def condition_description(condition)
2828
end
2929
end
3030

31+
def branch_condition_description(condition)
32+
if condition.answer_value.present?
33+
I18n.t("page_conditions.branch_condition_description", goto_page_question_text: goto_page_text_for_condition(condition), answer_value: answer_value_text_for_condition(condition))
34+
else
35+
I18n.t("page_conditions.unconditional_description", goto_page_question_text: goto_page_text_for_condition(condition))
36+
end
37+
end
38+
3139
def condition_check_page_text(condition)
3240
check_page = @pages.find { |page| page.id == condition.check_page_id }
3341
I18n.t("page_conditions.condition_check_page_text", check_page_question_text: check_page.question_text)

app/views/pages/index.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
<div class="govuk-grid-row">
66
<div class="govuk-grid-column-two-thirds">
7-
<%= render PageListComponent::ErrorSummary::View.new(current_form) %>
7+
<% unless current_form.group.multiple_branches_enabled? %>
8+
<%= render PageListComponent::ErrorSummary::View.new(current_form) %>
9+
<% end %>
810

911
<h1 class="govuk-heading-l govuk-!-margin-bottom-2">
1012
<span class="govuk-caption-l"><%= current_form.name %></span><span class="govuk-visually-hidden"> - </span>

config/locales/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ en:
13581358
If you pasted the web address, check you copied the entire address.
13591359
title: Page not found
13601360
page_conditions:
1361+
branch_condition_description: "%{answer_value} go to %{goto_page_question_text}"
13611362
condition_answer_value_text: "“%{answer_value}”"
13621363
condition_answer_value_text_with_errors: "[Answer not selected]"
13631364
condition_check_page_text: "“%{check_page_question_text}”"
@@ -1376,10 +1377,12 @@ en:
13761377
end_of_form: End of form
13771378
exit_page: Add an exit page
13781379
exit_page_label: An ‘exit page’ to leave the form
1380+
if_answer_is: 'If the answer is:'
13791381
none_of_the_above: None of the above
13801382
route: Route
13811383
secondary_skip_description: After %{check_page_question_text} go to %{goto_page_question_text}
13821384
skip_condition_route_page_text: "%{route_page_question_number}, “%{route_page_question_text}”"
1385+
unconditional_description: Go to %{goto_page_question_text}
13831386
page_route_card:
13841387
any_other_answer: Route for any other answer
13851388
conditional_answer_value: "%{answer_value}"

spec/components/page_list_component/page_list_component_preview.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ class PageListComponent::PageListComponentPreview < ViewComponent::Preview
33

44
def default
55
pages = []
6-
form = build(:form, id: 0, pages:)
6+
form = build(:form, :with_group, id: 0, pages:)
77
render(PageListComponent::View.new(pages:, form:))
88
end
99

1010
def with_pages_and_no_conditions
1111
pages = [build(:page, id: 1, position: 1, question_text: "Enter your name", routing_conditions: []),
1212
build(:page, id: 2, position: 2, question_text: "What is your pet's phone number?", routing_conditions: []),
1313
build(:page, id: 3, position: 3, question_text: "How many pets do you own?", routing_conditions: [])]
14-
form = build(:form, id: 0, pages:)
14+
form = build(:form, :with_group, id: 0, pages:)
1515
render(PageListComponent::View.new(pages:, form:))
1616
end
1717

@@ -20,7 +20,7 @@ def with_pages_and_one_condition
2020
pages = [build(:page, id: 1, position: 1, question_text: "Enter your name", routing_conditions: [condition]),
2121
build(:page, id: 2, position: 2, question_text: "What is your pet's phone number?", routing_conditions: []),
2222
build(:page, id: 3, position: 3, question_text: "How many pets do you own?", routing_conditions: [])]
23-
form = build(:form, id: 0, pages:)
23+
form = build(:form, :with_group, id: 0, pages:)
2424

2525
# We need to build the records rather than create them so that we don't save them to the database when we view the
2626
# preview. However, this means that the associations aren't available so we need to manually set the associations
@@ -41,7 +41,7 @@ def with_pages_and_multiple_conditions
4141
pages = [(build :page, id: 1, position: 1, question_text: "Enter your name", routing_conditions: routing_conditions_1),
4242
(build :page, id: 2, position: 2, question_text: "What is your pet's phone number?", routing_conditions: routing_conditions_2),
4343
(build :page, id: 3, position: 3, question_text: "How many pets do you own?", routing_conditions: [])]
44-
form = build(:form, id: 0, pages:)
44+
form = build(:form, :with_group, id: 0, pages:)
4545

4646
# We need to build the records rather than create them so that we don't save them to the database when we view the
4747
# preview. However, this means that the associations aren't available so we need to manually set the associations
@@ -65,7 +65,7 @@ def with_pages_and_conditions_with_errors
6565
pages = [(build :page, id: 1, position: 1, question_text: "Enter your name", routing_conditions: routing_conditions_1),
6666
(build :page, id: 2, position: 2, question_text: "What is your pet's phone number?", routing_conditions: routing_conditions_2),
6767
(build :page, id: 3, position: 3, question_text: "How many pets do you own?", routing_conditions: [])]
68-
form = build(:form, id: 1, pages:)
68+
form = build(:form, :with_group, id: 1, pages:)
6969

7070
# We need to build the records rather than create them so that we don't save them to the database when we view the
7171
# preview. However, this means that the associations aren't available so we need to manually set the associations

spec/components/page_list_component/view_spec.rb

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "rails_helper"
22

33
RSpec.describe PageListComponent::View, type: :component do
4-
let(:form) { create :form }
4+
let(:form) { create :form, :with_group }
55
let(:pages) { form.reload.pages }
66
let(:page_list_component) { described_class.new(pages:, form:) }
77

@@ -46,7 +46,7 @@
4646
end
4747

4848
context "when the form has multiple pages" do
49-
let(:form) { create :form, :with_pages }
49+
let(:form) { create :form, :with_pages, :with_group }
5050
let(:first_page) { form.pages.first }
5151
let(:second_page) { form.pages.second }
5252

@@ -79,7 +79,7 @@
7979
end
8080

8181
context "when the form has conditions" do
82-
let(:form) { create :form, :ready_for_routing }
82+
let(:form) { create :form, :ready_for_routing, :with_group }
8383
let(:edit_condition_path) { "/forms/0/pages/1/conditions/1" }
8484

8585
context "when the page has a single condition" do
@@ -144,7 +144,7 @@
144144
end
145145

146146
context "when the form has a valid branch route" do
147-
let(:form) { create :form, :ready_for_routing }
147+
let(:form) { create :form, :ready_for_routing, :with_group }
148148
let!(:secondary_skip_condition) { create :condition, routing_page_id: pages.second.id, check_page_id: pages.first.id, answer_value: nil, goto_page_id: pages.fourth.id }
149149

150150
before do
@@ -164,7 +164,7 @@
164164
end
165165

166166
context "when the form has a branch route with an error" do
167-
let(:form) { create :form, :ready_for_routing }
167+
let(:form) { create :form, :ready_for_routing, :with_group }
168168

169169
before do
170170
create :condition, routing_page_id: pages.first.id, check_page_id: pages.first.id, answer_value: "Option 1", goto_page_id: pages.third.id
@@ -183,7 +183,7 @@
183183
end
184184

185185
describe "class methods" do
186-
let(:form) { create :form, :with_pages }
186+
let(:form) { create :form, :with_pages, :with_group }
187187

188188
describe "show_up_button" do
189189
it "returns false when index is 0" do
@@ -322,5 +322,76 @@
322322
end
323323
end
324324
end
325+
326+
describe "#branch_condition_description" do
327+
context "when condition has all values set" do
328+
let(:condition) do
329+
create(
330+
:condition,
331+
routing_page_id: pages.first.id,
332+
check_page_id: pages.first.id,
333+
answer_value: "Option 1",
334+
goto_page_id: pages.third.id,
335+
)
336+
end
337+
338+
it "returns complete condition description" do
339+
expected_text = "“#{condition.answer_value}” go to #{pages.third.position}, “#{pages.third.question_text}”"
340+
expect(page_list_component.branch_condition_description(condition)).to eq(expected_text)
341+
end
342+
end
343+
344+
context "when answer value is 'none_of_the_above'" do
345+
let(:condition) do
346+
create(
347+
:condition,
348+
routing_page_id: pages.first.id,
349+
check_page_id: pages.first.id,
350+
answer_value: "none_of_the_above",
351+
goto_page_id: pages.third.id,
352+
)
353+
end
354+
355+
it "returns description with 'None of the above' text" do
356+
expected_text = "“None of the above” go to #{pages.third.position}, “#{pages.third.question_text}”"
357+
expect(page_list_component.branch_condition_description(condition)).to eq(expected_text)
358+
end
359+
end
360+
361+
context "when skip_to_end is true" do
362+
let(:condition) do
363+
create(
364+
:condition,
365+
routing_page_id: pages.first.id,
366+
check_page_id: pages.first.id,
367+
answer_value: "Option 1",
368+
goto_page_id: nil,
369+
skip_to_end: true,
370+
)
371+
end
372+
373+
it "returns description with 'Check your answers' text" do
374+
expected_text = "“#{condition.answer_value}” go to end of form."
375+
expect(page_list_component.branch_condition_description(condition)).to eq(expected_text)
376+
end
377+
end
378+
379+
context "when showing an unconditional route" do
380+
let(:condition) do
381+
create(
382+
:condition,
383+
routing_page_id: pages.second.id,
384+
check_page_id: pages.first.id,
385+
answer_value: nil,
386+
goto_page_id: pages.fourth.id,
387+
)
388+
end
389+
390+
it "returns correct description" do
391+
expected_text = "Go to #{pages.fourth.position}, “#{pages.fourth.question_text}”"
392+
expect(page_list_component.branch_condition_description(condition)).to eq(expected_text)
393+
end
394+
end
395+
end
325396
end
326397
end

spec/views/pages/index.html.erb_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "rails_helper"
22

33
describe "pages/index.html.erb" do
4-
let(:form) { create :form, pages: }
4+
let(:form) { create :form, :with_group, pages: }
55
let(:pages) { [] }
66
let(:mark_complete_input) { Forms::MarkPagesSectionCompleteInput.new(form:).assign_form_values }
77

0 commit comments

Comments
 (0)