Skip to content

Commit 17cdb7f

Browse files
committed
Order Condition groups by selection option
We want to display the conditions for a page on the page list in the same order as the selection options on the page. To do this, we get a list of selection options from the page's answer settings, and then use this list to sort the conditions. We turn off the filter for the call to `in_order_of` so that any extra conditions are included, even if they aren't included in the order. group_by preservers order when given an array so we are safe to use it like this. We still sort by the `goto_page_id` so that the conditions are grouped by the page they go to and nil comes last.
1 parent 8fafef8 commit 17cdb7f

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

app/components/page_list_component/view.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ def skip_condition_route_page_text(condition)
101101
end
102102

103103
def answer_value_groups(page)
104-
page.routing_conditions.group_by(&:goto_page_id).sort_by { |goto_page_id, _| goto_page_id || Float::INFINITY }
104+
answer_order = page.answer_settings&.selection_options&.map(&:value) || []
105+
106+
page.routing_conditions.to_a
107+
.in_order_of(:answer_value, answer_order, filter: false)
108+
.group_by(&:goto_page_id)
109+
.sort_by { |goto_page_id, _| goto_page_id || Float::INFINITY }
105110
end
106111
end
107112
end

spec/components/page_list_component/view_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,5 +563,26 @@
563563
result = page_list_component.answer_value_groups(pages.first)
564564
expect(result).to eq expected
565565
end
566+
567+
context "when given a different order of selection options" do
568+
let(:selection_options) do
569+
[
570+
{ value: "Option 3" },
571+
{ value: "Option 2" },
572+
{ value: "Option 1" },
573+
]
574+
end
575+
576+
it "returns the conditions in the same order" do
577+
expected = [
578+
[103, [conditions.second, conditions.first]],
579+
[104, [conditions.third]],
580+
[nil, [conditions.fourth]],
581+
]
582+
583+
result = page_list_component.answer_value_groups(pages.first)
584+
expect(result).to eq expected
585+
end
586+
end
566587
end
567588
end

0 commit comments

Comments
 (0)