Skip to content

Commit

Permalink
Merge pull request #1957 from alphagov/show-answers-and-routing-in-hi…
Browse files Browse the repository at this point in the history
…story

Add answers and routing to simple smart answers history
  • Loading branch information
andy-collon authored Nov 10, 2023
2 parents d3ef387 + 3a2060c commit e8bf2a2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
30 changes: 28 additions & 2 deletions app/models/simple_smart_answer_edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,4 +79,24 @@ def initial_node
def destroy_in_attrs?(attrs)
attrs.delete("_destroy") == "1"
end

private

def question(node)
part = ["#{node.slug.titleize}\n#{node.title}\n"]
part << node.body.to_s unless node.body == ""
part << ""
node.options.each.with_index(1) do |option, index|
part << "Answer #{index}\n#{option.label}"
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.join("\n")
end

def outcome(node)
body = node.body == "" ? "" : "\n#{node.body}"
"#{node.slug.titleize}\n#{node.title}#{body}"
end
end
25 changes: 23 additions & 2 deletions test/models/simple_smart_answer_edition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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", body: "Outcome body")
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)\n\nAnswer 2\noption two\nNext question for user: Outcome 2 (The second outcome)\n\n\n\nOutcome 1\nThe first outcome\nOutcome body\n\n\nOutcome 2\nThe second outcome\n", edition.whole_body
end

should "create nodes with nested attributes" do
edition = FactoryBot.create(
:simple_smart_answer_edition,
Expand Down

0 comments on commit e8bf2a2

Please sign in to comment.