Browse Source

Refactor how we add stimulus controller html attributes so that we can combine them

pull/31/head
baarkerlounger 4 years ago
parent
commit
1f525c6413
  1. 10
      app/helpers/conditional_questions_helper.rb
  2. 12
      app/helpers/numeric_questions_helper.rb
  3. 39
      app/helpers/stimulus_controller_attribute_helper.rb
  4. 2
      app/views/form/_checkbox_question.html.erb
  5. 2
      app/views/form/_date_question.html.erb
  6. 2
      app/views/form/_numeric_question.html.erb
  7. 2
      app/views/form/_radio_question.html.erb
  8. 2
      app/views/form/_text_question.html.erb
  9. 6
      spec/helpers/stimulus_controller_attribute_helper_spec.rb

10
app/helpers/conditional_questions_helper.rb

@ -8,14 +8,4 @@ module ConditionalQuestionsHelper
def display_question_key_div(page_info, question_key) def display_question_key_div(page_info, question_key)
"style='display:none;'".html_safe if conditional_questions_for_page(page_info).include?(question_key) "style='display:none;'".html_safe if conditional_questions_for_page(page_info).include?(question_key)
end end
def conditional_html_attributes(question)
return {} if question["conditional_for"].blank?
{
"data-controller": "conditional-question",
"data-action": "conditional-question#displayConditional",
"data-info": question["conditional_for"].to_json,
}
end
end end

12
app/helpers/numeric_questions_helper.rb

@ -1,12 +0,0 @@
module NumericQuestionsHelper
def numeric_question_html_attributes(question)
return {} if question["fields-to-add"].blank? || question["result-field"].blank?
{
"data-controller": "numeric-question",
"data-action": "numeric-question#calculateFields",
"data-target": "case-log-#{question['result-field'].to_s.dasherize}-field",
"data-calculated": question["fields-to-add"].to_json,
}
end
end

39
app/helpers/stimulus_controller_attribute_helper.rb

@ -0,0 +1,39 @@
module StimulusControllerAttributeHelper
def stimulus_html_attributes(question)
attribs = [
numeric_question_html_attributes(question),
conditional_html_attributes(question)
]
merge_controller_attributes(*attribs)
end
private
def numeric_question_html_attributes(question)
return {} if question["fields-to-add"].blank? || question["result-field"].blank?
{
"data-controller": "numeric-question",
"data-action": "numeric-question#calculateFields",
"data-target": "case-log-#{question['result-field'].to_s.dasherize}-field",
"data-calculated": question["fields-to-add"].to_json,
}
end
def conditional_html_attributes(question)
return {} if question["conditional_for"].blank?
{
"data-controller": "conditional-question",
"data-action": "conditional-question#displayConditional",
"data-info": question["conditional_for"].to_json,
}
end
end
def merge_controller_attributes(*args)
args.flat_map(&:keys).uniq.each_with_object({}) do |key, hsh|
hsh[key] = args.map { |a| a.fetch(key, "") }.join(" ").strip
hsh
end
end

2
app/views/form/_checkbox_question.html.erb

@ -6,7 +6,7 @@
<% if key.starts_with?("divider") %> <% if key.starts_with?("divider") %>
<%= f.govuk_check_box_divider %> <%= f.govuk_check_box_divider %>
<% else %> <% else %>
<%= f.govuk_check_box question_key, val, label: { text: val }, **conditional_html_attributes(question) %> <%= f.govuk_check_box question_key, val, label: { text: val }, **stimulus_html_attributes(question) %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>

2
app/views/form/_date_question.html.erb

@ -2,5 +2,5 @@
hint: { text: question["hint_text"] }, hint: { text: question["hint_text"] },
legend: { text: question["header"].html_safe, size: "l"}, legend: { text: question["header"].html_safe, size: "l"},
width: 20, width: 20,
**conditional_html_attributes(question) **stimulus_html_attributes(question)
%> %>

2
app/views/form/_numeric_question.html.erb

@ -3,5 +3,5 @@
label: { text: question["header"].html_safe, size: "l"}, label: { text: question["header"].html_safe, size: "l"},
min: question["min"], max: question["max"], step: question["step"], min: question["min"], max: question["max"], step: question["step"],
width: 20, :readonly => question["readonly"], width: 20, :readonly => question["readonly"],
**numeric_question_html_attributes(question) **stimulus_html_attributes(question)
%> %>

2
app/views/form/_radio_question.html.erb

@ -7,7 +7,7 @@
<% if key.starts_with?("divider") %> <% if key.starts_with?("divider") %>
<%= f.govuk_radio_divider %> <%= f.govuk_radio_divider %>
<% else %> <% else %>
<%= f.govuk_radio_button question_key, val, label: { text: val }, **conditional_html_attributes(question) %> <%= f.govuk_radio_button question_key, val, label: { text: val }, **stimulus_html_attributes(question) %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>

2
app/views/form/_text_question.html.erb

@ -2,5 +2,5 @@
hint: { text: question["hint_text"] }, hint: { text: question["hint_text"] },
label: { text: question["header"].html_safe, size: "l"}, label: { text: question["header"].html_safe, size: "l"},
width: 20, width: 20,
**conditional_html_attributes(question) **stimulus_html_attributes(question)
%> %>

6
spec/helpers/numeric_questions_helper_spec.rb → spec/helpers/stimulus_controller_attribute_helper_spec.rb

@ -1,16 +1,16 @@
require "rails_helper" require "rails_helper"
RSpec.describe NumericQuestionsHelper do RSpec.describe StimulusControllerAttributeHelper do
let(:form) { Form.new(2021, 2022) } let(:form) { Form.new(2021, 2022) }
let(:questions) { form.questions_for_page("rent") } let(:questions) { form.questions_for_page("rent") }
describe "html attributes" do describe "html attributes" do
it "returns empty hash if fields-to-add or result-field are empty " do it "returns empty hash if fields-to-add or result-field are empty " do
expect(numeric_question_html_attributes(questions["total_charge"])).to eq({}) expect(stimulus_html_attributes(questions["total_charge"])).to eq({})
end end
it "returns html attributes if fields-to-add or result-field are not empty " do it "returns html attributes if fields-to-add or result-field are not empty " do
expect(numeric_question_html_attributes(questions["basic_rent"])).to eq({ expect(stimulus_html_attributes(questions["basic_rent"])).to eq({
"data-controller": "numeric-question", "data-controller": "numeric-question",
"data-action": "numeric-question#calculateFields", "data-action": "numeric-question#calculateFields",
"data-target": "case-log-#{questions['basic_rent']['result-field'].to_s.dasherize}-field", "data-target": "case-log-#{questions['basic_rent']['result-field'].to_s.dasherize}-field",
Loading…
Cancel
Save