From c57e0cb36921eeb940e1feaf36494f842996ba95 Mon Sep 17 00:00:00 2001 From: Anna Cavalla Date: Mon, 6 Nov 2023 10:53:47 +0000 Subject: [PATCH] Add answers and routing to simple smart answers history --- app/models/simple_smart_answer_edition.rb | 33 +++++++++++++++++-- .../simple_smart_answer_edition_test.rb | 25 ++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app/models/simple_smart_answer_edition.rb b/app/models/simple_smart_answer_edition.rb index adb9352e6..37e6107c7 100644 --- a/app/models/simple_smart_answer_edition.rb +++ b/app/models/simple_smart_answer_edition.rb @@ -17,9 +17,15 @@ class SimpleSmartAnswerEdition < Edition GOVSPEAK_FIELDS = [:body].freeze def whole_body - parts = [body] + parts = body == "" ? [] : [body] unless nodes.nil? - parts += nodes.map { |node| "#{node.kind.capitalize} #{node.slug.split('-')[1]}\n#{node.title} \n\n #{node.body}" } + nodes.each do |node| + parts << if node.kind == "question" + question(node) + elsif node.kind == "outcome" + outcome(node) + end + end end parts.join("\n\n\n") end @@ -73,4 +79,27 @@ def initial_node def destroy_in_attrs?(attrs) attrs.delete("_destroy") == "1" end + +private + + def question(node) + part = "#{label_title(node)}\n\n" + part += node.body == "" ? "" : "#{node.body}\n\n" + node.options.each.with_index(1) do |option, index| + part += "Answer #{index}\n#{option.label}\n" + title = (nodes.select { |single_node| single_node["slug"] == option.next_node })[0].title.to_s + next_node_title, next_node_number = option.next_node.split("-") + part += "Next question for user: #{next_node_title.capitalize} #{next_node_number} (#{title})\n" + end + part + end + + def outcome(node) + body = node.body == "" ? "" : "#{node.body}\n\n" + label_title(node) + body + end + + def label_title(node) + "#{node.kind.capitalize} #{node.slug.split('-')[1]}\n#{node.title}" + end end diff --git a/test/models/simple_smart_answer_edition_test.rb b/test/models/simple_smart_answer_edition_test.rb index f8b726936..e4f44dae0 100644 --- a/test/models/simple_smart_answer_edition_test.rb +++ b/test/models/simple_smart_answer_edition_test.rb @@ -53,12 +53,12 @@ class SimpleSmartAnswerEditionTest < ActiveSupport::TestCase body: "This smart answer is somewhat unique and calls for a different kind of introduction", state: "published", ) - edition.nodes.build(slug: "question-1", title: "You approach two open doors. Which do you choose?", kind: "question", order: 1) + edition.nodes.build(slug: "question-1", title: "You approach two open doors. Which do you choose?", kind: "question", order: 1, body: "") edition.save! new_edition = edition.build_clone(AnswerEdition) - assert_equal "This smart answer is somewhat unique and calls for a different kind of introduction\n\n\nQuestion 1\nYou approach two open doors. Which do you choose? \n\n ", new_edition.body + assert_equal "This smart answer is somewhat unique and calls for a different kind of introduction\n\n\nQuestion 1\nYou approach two open doors. Which do you choose?\n\n", new_edition.body assert new_edition.is_a?(AnswerEdition) assert_not new_edition.respond_to?(:nodes) @@ -74,6 +74,27 @@ class SimpleSmartAnswerEditionTest < ActiveSupport::TestCase assert_equal "question1", edition.initial_node.slug end + should "format the questions and outcomes correctly for the history" do + edition = FactoryBot.create(:simple_smart_answer_edition) + edition.nodes.build(slug: "question-1", + title: "The first question", + kind: "question", + body: "Body", + order: 1, + options: [ + { + label: "option one", + next_node: "outcome-1", + }, + { label: "option two", + next_node: "outcome-2" }, + ]) + edition.nodes.build(slug: "outcome-1", title: "The first outcome", order: 3, kind: "outcome") + edition.nodes.build(slug: "outcome-2", title: "The second outcome", order: 4, kind: "outcome") + + assert_equal "Introduction to the smart answer\n\n\nQuestion 1\nThe first question\n\nBody\n\nAnswer 1\noption one\nNext question for user: Outcome 1 (The first outcome)\nAnswer 2\noption two\nNext question for user: Outcome 2 (The second outcome)\n\n\n\nOutcome 1\nThe first outcome\n\n\n\n\nOutcome 2\nThe second outcome\n\n", edition.whole_body + end + should "create nodes with nested attributes" do edition = FactoryBot.create( :simple_smart_answer_edition,