9 changed files with 47 additions and 30 deletions
@ -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 |
@ -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 |
@ -1,16 +1,16 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe NumericQuestionsHelper do |
||||
RSpec.describe StimulusControllerAttributeHelper do |
||||
let(:form) { Form.new(2021, 2022) } |
||||
let(:questions) { form.questions_for_page("rent") } |
||||
|
||||
describe "html attributes" 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 |
||||
|
||||
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-action": "numeric-question#calculateFields", |
||||
"data-target": "case-log-#{questions['basic_rent']['result-field'].to_s.dasherize}-field", |
Loading…
Reference in new issue