Browse Source

feat: add question with dynamic header

pull/944/head
natdeanlewissoftwire 3 years ago
parent
commit
ac9fbda9cf
  1. 2
      app/helpers/question_view_helper.rb
  2. 3
      app/models/form/page.rb
  3. 3
      app/models/form/question.rb
  4. 21
      app/models/form/sales/pages/person2_known.rb
  5. 25
      app/models/form/sales/questions/person2_known.rb
  6. 1
      app/models/form/sales/subsections/household_characteristics.rb
  7. 1
      app/views/form/headers/_personx_known_page.erb
  8. 5
      app/views/form/page.html.erb
  9. 7
      db/migrate/20221010085259_add_details_known_to_sales_log.rb
  10. 5
      db/schema.rb
  11. 1
      spec/factories/sales_log.rb

2
app/helpers/question_view_helper.rb

@ -7,7 +7,7 @@ module QuestionViewHelper
def legend(question, page_header, conditional)
{
text: question.header.html_safe,
text: question.header_partial.present? ? question.header_partial.html_safe : question.header.html_safe,
size: label_size(page_header, conditional),
tag: label_tag(page_header, conditional),
}

3
app/models/form/page.rb

@ -1,5 +1,5 @@
class Form::Page
attr_accessor :id, :header, :description, :questions, :depends_on, :title_text,
attr_accessor :id, :header, :header_partial, :description, :questions, :depends_on, :title_text,
:informative_text, :subsection, :hide_subsection_label
def initialize(id, hsh, subsection)
@ -7,6 +7,7 @@ class Form::Page
@subsection = subsection
if hsh
@header = hsh["header"]
@header_partial = hsh["header_partial"]
@description = hsh["description"]
@questions = hsh["questions"].map { |q_id, q| Form::Question.new(q_id, q, self) }
@depends_on = hsh["depends_on"]

3
app/models/form/question.rb

@ -1,5 +1,5 @@
class Form::Question
attr_accessor :id, :header, :hint_text, :description, :questions,
attr_accessor :id, :header, :header_partial, :hint_text, :description, :questions,
:type, :min, :max, :step, :width, :fields_to_add, :result_field,
:conditional_for, :readonly, :answer_options, :page, :check_answer_label,
:inferred_answers, :hidden_in_check_answers, :inferred_check_answers_value,
@ -16,6 +16,7 @@ class Form::Question
if hsh
@check_answer_label = hsh["check_answer_label"]
@header = hsh["header"]
@header_partial = hsh["header_partial"]
@guidance_partial = hsh["guidance_partial"]
@guidance_position = GuidancePosition::TOP
@hint_text = hsh["hint_text"]

21
app/models/form/sales/pages/person2_known.rb

@ -0,0 +1,21 @@
class Form::Sales::Pages::Person2Known < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "person_2_known"
@header_partial = "personx_known_page"
@header = ""
@description = ""
@subsection = subsection
@depends_on = [
{ "hholdcount" => 2 },
{ "hholdcount" => 3 },
{ "hholdcount" => 4 },
]
end
def questions
@questions ||= [
Form::Sales::Questions::Person2Known.new(nil, nil, self),
]
end
end

25
app/models/form/sales/questions/person2_known.rb

@ -0,0 +1,25 @@
class Form::Sales::Questions::Person2Known < ::Form::Question
def initialize(id, hsh, page)
super
@id = "details_known_2"
@check_answer_label = "Details known for person 2"
@header = "Do you know the details for person 2?
"
@type = "radio"
@answer_options = ANSWER_OPTIONS
@page = page
@hint_text = ""
@hidden_in_check_answers = {
"depends_on" => [
{
"details_known_2" => 1,
},
],
}
end
ANSWER_OPTIONS = {
"1" => { "value" => "Yes" },
"2" => { "value" => "No" },
}.freeze
end

1
app/models/form/sales/subsections/household_characteristics.rb

@ -29,6 +29,7 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection
Form::Sales::Pages::Buyer2LiveInProperty.new(nil, nil, self),
Form::Sales::Pages::NumberOfOthersInProperty.new(nil, nil, self),
Form::Sales::Pages::Person1Age.new(nil, nil, self),
Form::Sales::Pages::Person2Known.new(nil, nil, self),
]
end
end

1
app/views/form/headers/_personx_known_page.erb

@ -0,0 +1 @@
You have given us the details for 1 of the <%= hholdcount %> other people in the household

5
app/views/form/page.html.erb

@ -11,11 +11,14 @@
<% remove_other_page_errors(@log, @page) %>
<%= f.govuk_error_summary %>
<% if @page.header.present? %>
<% if @page.header.present? || @page.header_partial.present? %>
<h1 class="govuk-heading-l">
<% if !@page.hide_subsection_label %>
<span class="govuk-caption-l"><%= @subsection.label %></span>
<% end %>
<%if @page.header_partial.present? %>
<% @page.header = render partial: "form/headers/#{@page.header_partial}", locals: {hholdcount: @log.hholdcount} %>
<% end %>
<%= @page.header %>
</h1>
<% end %>

7
db/migrate/20221010085259_add_details_known_to_sales_log.rb

@ -0,0 +1,7 @@
class AddDetailsKnownToSalesLog < ActiveRecord::Migration[7.0]
change_table :sales_logs, bulk: true do |t|
t.column :details_known_2, :integer
t.column :details_known_3, :integer
t.column :details_known_4, :integer
end
end

5
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_10_07_093606) do
ActiveRecord::Schema[7.0].define(version: 2022_10_10_085259) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -358,6 +358,9 @@ ActiveRecord::Schema[7.0].define(version: 2022_10_07_093606) do
t.integer "hholdcount"
t.integer "age3"
t.integer "age3_known"
t.integer "details_known_2"
t.integer "details_known_3"
t.integer "details_known_4"
t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id"
t.index ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id"

1
spec/factories/sales_log.rb

@ -41,6 +41,7 @@ FactoryBot.define do
wheel { 1 }
age3_known { 0 }
age3 { 40 }
person2_known { 1 }
end
end
end

Loading…
Cancel
Save