Skip to content

Commit

Permalink
AC/ACB/DT - add test and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
acavalla committed Nov 30, 2023
1 parent 1b42ccc commit dcdd54f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
49 changes: 20 additions & 29 deletions app/models/simple_smart_answer_edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,13 @@ def destroy_in_attrs?(attrs)
attrs.delete("_destroy") == "1"
end

def generate_mermaid()
parts = ["%%{
init: {
'theme': 'base',
'themeVariables': {
'background': '#FFFFFF',
'primaryTextColor': '#0B0C0C',
'lineColor': '#0b0c0c',
'fontSize': '23.75px'
}
}
}%%\nflowchart TD"]
parts<<"accTitle: #{title}"
parts<<"accDescr: A flowchart for the #{title} smart answer"
parts<<"AA[Start]:::start"
def generate_mermaid
parts = ["%%{ init: {\n'theme': 'base',\n'themeVariables': {\n " \
"'background': '#FFFFFF',\n 'primaryTextColor': '#0B0C0C',\n " \
"'lineColor': '#0b0c0c',\n 'fontSize': '23.75px' } } }%%\nflowchart TD"]
parts << "accTitle: #{title}\naccDescr: A flowchart for the #{title} smart answer\nAA[Start]:::start"
unless nodes.nil?
parts<<"AA---Q#{nodes.first.slug.split('-')[1]}"
parts << "AA---Q#{nodes.first.slug.split('-')[1]}"
nodes.each do |node|
parts << if node.kind == "question"
mermaid_question(node)
Expand All @@ -105,7 +95,7 @@ def generate_mermaid()
end
end
end
parts<<"classDef answer fill: #F3F2F1, stroke:#505A5F;\nclassDef outcome fill: #6FA4D2" +
parts << "classDef answer fill: #F3F2F1, stroke:#505A5F;\nclassDef outcome fill: #6FA4D2" \
"\nclassDef question fill: #B1B4B6, stroke:#505A5F;\nclassDef start fill:#00703c,color: #ffffff"
parts.join("\n")
end
Expand All @@ -125,25 +115,26 @@ def question(node)
part.join("\n")
end

def outcome(node)
body = node.body == "" ? "" : "\n#{node.body}"
"#{node.slug.titleize}\n#{node.title}#{body}"
end

def mermaid_question(node)
question_id=node.slug.split('-')[1]
part = ["Q#{question_id}[\"`#{node.slug.titleize} #{node.title}`\"]:::question"]
question_node_id = node.slug.split("-")[1]
part = ["Q#{question_node_id}[\"`Q#{question_node_id}. #{node.title}`\"]:::question"]
node.options.each.with_index(1) do |option, index|
part<< "Q#{question_id}---Q#{question_id}A#{index}"
part << "Q#{question_id}A#{index}([\"`Answer #{index} #{option.label}`\"]):::answer"
answer_node_id = "Q#{question_node_id}A#{index}"
next_node_title, next_node_number = option.next_node.split("-")
part << "Q#{question_id}A#{index}-->#{next_node_title[0].upcase}#{next_node_number}\n"
part << "Q#{question_node_id}---#{answer_node_id}\n" \
"#{answer_node_id}([\"`A#{index}. #{option.label}`\"]):::answer\n" \
"#{answer_node_id}-->#{next_node_title[0].upcase}#{next_node_number}\n"
end
part.join("\n")
end

def mermaid_outcome(node)
node_id = node.slug.split("-")[1]
"O#{node_id}{{\"`O#{node_id} #{node.title}`\"}}:::outcome"
end
def outcome(node)
body = node.body == "" ? "" : "\n#{node.body}"
"#{node.slug.titleize}\n#{node.title}#{body}"
outcome_node_id = node.slug.split("-")[1]
"O#{outcome_node_id}{{\"`O#{outcome_node_id}. #{node.title}`\"}}:::outcome"
end
end
24 changes: 13 additions & 11 deletions test/models/simple_smart_answer_edition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ class SimpleSmartAnswerEditionTest < ActiveSupport::TestCase
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
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
Expand Down Expand Up @@ -242,19 +245,18 @@ class SimpleSmartAnswerEditionTest < ActiveSupport::TestCase
edition = FactoryBot.build(:simple_smart_answer_edition, panopticon_id: @artefact.id)
edition.body = "This is a simple smart answer."

edition.nodes.build(slug: "question-1", title: "You approach two locked doors. Which do you choose?", kind: "question", options: [{label: "A tiger fights you", next_node: "outcome-1" }])
edition.nodes.build(slug: "question-1", title: "You approach two locked doors. Which do you choose?", kind: "question", options: [{ label: "A tiger fights you", next_node: "outcome-1" }])
edition.nodes.build(slug: "outcome-1", title: "Tiger wins", kind: "outcome")
edition.save!

assert_equal "%%{\n init: {\n 'theme': 'base',\n 'themeVariables': {\n 'background': '#FFFFFF',\n" +
" 'primaryTextColor': '#0B0C0C',\n 'lineColor': '#0b0c0c',\n 'fontSize': '23.75px'\n" +
" }\n }\n}%%\nflowchart TD\naccTitle: Simple smart answer\naccDescr: A flowchart for the Simple " +
"smart answer smart answer\nAA[Start]:::start\nAA---Q1\nQ1[\"`Question 1 You approach two locked doors. " +
"Which do you choose?`\"]:::question\nQ1---Q1A1\nQ1A1([\"`Answer 1 A tiger fights you`\"]):::answer\n" +
"Q1A1-->O1\n\nO1{{\"`O1 Tiger wins`\"}}:::outcome\nclassDef answer fill: #F3F2F1, stroke:#505A5F;\nclassDef " +
"outcome fill: #6FA4D2\nclassDef question fill: #B1B4B6, stroke:#505A5F;\nclassDef start fill:#00703c,color: " +
"#ffffff", edition.generate_mermaid

assert_equal "%%{ init: {\n'theme': 'base',\n'themeVariables': {\n 'background': '#FFFFFF',\n" \
" 'primaryTextColor': '#0B0C0C',\n 'lineColor': '#0b0c0c',\n 'fontSize': '23.75px'" \
" } } }%%\nflowchart TD\naccTitle: Simple smart answer\naccDescr: A flowchart for the Simple " \
"smart answer smart answer\nAA[Start]:::start\nAA---Q1\nQ1[\"`Q1. You approach two locked doors. " \
"Which do you choose?`\"]:::question\nQ1---Q1A1\nQ1A1([\"`A1. A tiger fights you`\"]):::answer\n" \
"Q1A1-->O1\n\nO1{{\"`O1. Tiger wins`\"}}:::outcome\nclassDef answer fill: #F3F2F1, stroke:#505A5F;\nclassDef " \
"outcome fill: #6FA4D2\nclassDef question fill: #B1B4B6, stroke:#505A5F;\nclassDef start fill:#00703c,color: " \
"#ffffff", edition.generate_mermaid
end
# rubocop:enable Rails/SaveBang
end

0 comments on commit dcdd54f

Please sign in to comment.