Skip to content

Commit

Permalink
Add answers and routing to simple smart answers history
Browse files Browse the repository at this point in the history
  • Loading branch information
acavalla committed Nov 6, 2023
1 parent b93309f commit c57e0cb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
33 changes: 31 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,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
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")
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,
Expand Down

0 comments on commit c57e0cb

Please sign in to comment.