Browse Source
* Format errors on money amounts with 2 decimals * CLDC-1975 Format money input and error messages * format discounted_ownership_value * Format more locales * Update lettings_log tests * Fix import service specpull/1526/head v0.3.10
Jack
2 years ago
committed by
GitHub
15 changed files with 184 additions and 81 deletions
@ -0,0 +1,23 @@ |
|||||||
|
module MoneyFormattingHelper |
||||||
|
include ActionView::Helpers::NumberHelper |
||||||
|
|
||||||
|
def format_money_input(log:, question:) |
||||||
|
value = log[question.id] |
||||||
|
|
||||||
|
return unless value |
||||||
|
return value unless question.prefix == "£" |
||||||
|
|
||||||
|
number_with_precision( |
||||||
|
value, |
||||||
|
precision: 2, |
||||||
|
) |
||||||
|
end |
||||||
|
|
||||||
|
def format_as_currency(num_string) |
||||||
|
number_to_currency( |
||||||
|
num_string, |
||||||
|
unit: "£", |
||||||
|
precision: 2, |
||||||
|
) |
||||||
|
end |
||||||
|
end |
@ -1,14 +1,19 @@ |
|||||||
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.top_guidance? %> |
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.top_guidance? %> |
||||||
|
|
||||||
<%= f.govuk_number_field question.id.to_sym, |
<%= f.govuk_number_field( |
||||||
|
question.id.to_sym, |
||||||
caption: caption(caption_text, page_header, conditional), |
caption: caption(caption_text, page_header, conditional), |
||||||
label: legend(question, page_header, conditional), |
label: legend(question, page_header, conditional), |
||||||
hint: { text: question.hint_text&.html_safe }, |
hint: { text: question.hint_text&.html_safe }, |
||||||
min: question.min, max: question.max, step: question.step, |
min: question.min, |
||||||
|
max: question.max, |
||||||
|
step: question.step, |
||||||
width: question.width, |
width: question.width, |
||||||
readonly: question.read_only?, |
readonly: question.read_only?, |
||||||
prefix_text: question.prefix.to_s, |
prefix_text: question.prefix.to_s, |
||||||
suffix_text: question.suffix_label(@log), |
suffix_text: question.suffix_label(@log), |
||||||
**stimulus_html_attributes(question) %> |
value: format_money_input(log: @log, question:), |
||||||
|
**stimulus_html_attributes(question), |
||||||
|
) %> |
||||||
|
|
||||||
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.bottom_guidance? %> |
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.bottom_guidance? %> |
||||||
|
@ -0,0 +1,41 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe MoneyFormattingHelper do |
||||||
|
describe "#format_money_input" do |
||||||
|
let!(:log) { create(:lettings_log, :completed, brent: 1000) } |
||||||
|
let(:question) { instance_double(Form::Question, id: "brent", prefix:) } |
||||||
|
|
||||||
|
context "with £ prefix" do |
||||||
|
let(:prefix) { "£" } |
||||||
|
|
||||||
|
it "returns formatted input" do |
||||||
|
expect(format_money_input(log:, question:)).to eq("1000.00") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "with other prefix" do |
||||||
|
let(:prefix) { "other" } |
||||||
|
|
||||||
|
it "does not format the input" do |
||||||
|
expect(format_money_input(log:, question:)).to eq(BigDecimal(1000)) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "without prefix" do |
||||||
|
let(:prefix) { nil } |
||||||
|
|
||||||
|
it "does not format the input" do |
||||||
|
expect(format_money_input(log:, question:)).to eq(BigDecimal(1000)) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when value is nil" do |
||||||
|
let(:prefix) { "£" } |
||||||
|
let(:log) { create(:lettings_log, brent: nil) } |
||||||
|
|
||||||
|
it "does not format the input" do |
||||||
|
expect(format_money_input(log:, question:)).to be_nil |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue