Browse Source

Extract logic to helper

pull/29/head
baarkerlounger 4 years ago
parent
commit
40e55f86be
  1. 8
      app/helpers/conditional_questions_helper.rb
  2. 6
      app/views/form/page.html.erb
  3. 13
      spec/helpers/conditional_questions_helper_spec.rb

8
app/helpers/conditional_questions_helper.rb

@ -4,4 +4,12 @@ module ConditionalQuestionsHelper
question["conditional_for"] question["conditional_for"]
}.compact.map(&:keys).flatten }.compact.map(&:keys).flatten
end end
def display_question_key_div(page_info, question_key)
if conditional_questions_for_page(page_info).include?(question_key)
"<div id=#{question_key}_div style='display:none;'>".html_safe
else
"<div id=#{question_key}_div>".html_safe
end
end
end end

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

@ -14,11 +14,7 @@
<%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<% page_info["questions"].map do |question_key, question| %> <% page_info["questions"].map do |question_key, question| %>
<% if conditional_questions_for_page(page_info).include?(question_key) %> <%= display_question_key_div(page_info, question_key)%>
<div id=<%= "#{question_key}_div" %> style="display:none;">
<% else %>
<div id=<%= "#{question_key}_div" %> >
<% end %>
<%= render partial: "form/#{question["type"]}_question", locals: { question_key: question_key, question: question, f: f } %> <%= render partial: "form/#{question["type"]}_question", locals: { question_key: question_key, question: question, f: f } %>
</div> </div>
<% end %> <% end %>

13
spec/helpers/conditional_questions_helper_spec.rb

@ -12,4 +12,17 @@ RSpec.describe ConditionalQuestionsHelper do
expect(conditional_questions_for_page(page)).to eq(conditional_pages) expect(conditional_questions_for_page(page)).to eq(conditional_pages)
end end
end end
describe "display question key div" do
let(:question_key) { "armed_forces" }
let(:conditional_question_key) { "armed_forces_injured" }
it "returns a non visible div for conditional questions" do
expect(display_question_key_div(page, conditional_question_key)).to match("style='display:none;'")
end
it "returns a visible div for conditional questions" do
expect(display_question_key_div(page, question_key)).not_to match("style='display:none;'")
end
end
end end

Loading…
Cancel
Save