From c92771a8e37eec5ba5e277e0341e61acddac047a Mon Sep 17 00:00:00 2001 From: Ted-U Date: Mon, 25 Jul 2022 16:06:39 +0100 Subject: [PATCH] format postcode - add space --- app/models/case_log.rb | 15 ++++---- app/models/form.rb | 1 + spec/features/form/postcode_format_spec.rb | 42 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 spec/features/form/postcode_format_spec.rb diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 83e7b6550..382ed583c 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -8,7 +8,6 @@ class CaseLogValidator < ActiveModel::Validator include Validations::TenancyValidations include Validations::DateValidations include Validations::LocalAuthorityValidations - def validate(record) validation_methods = public_methods.select { |method| method.starts_with?("validate_") } validation_methods.each { |meth| public_send(meth, record) } @@ -567,13 +566,19 @@ private public_send("age#{person_num}") && public_send("age#{person_num}") < 16 end + def format_postcode(value) + value = value.upcase.gsub(/\s+/, "") + value.length == 5 ? value= value.insert(2, ' ') : value= value.insert(3, ' ') + value.strip + end + def process_postcode_changes! - self.postcode_full = upcase_and_remove_whitespace(postcode_full) + self.postcode_full = format_postcode(postcode_full) process_postcode(postcode_full, "postcode_known", "is_la_inferred", "la") end def process_previous_postcode_changes! - self.ppostcode_full = upcase_and_remove_whitespace(ppostcode_full) + self.ppostcode_full = format_postcode(ppostcode_full) process_postcode(ppostcode_full, "ppcodenk", "is_previous_la_inferred", "prevloc") end @@ -692,8 +697,4 @@ private (value * 52 / num_of_weeks).round(2) end - - def upcase_and_remove_whitespace(string) - string.present? ? string.upcase.gsub(/\s+/, "") : string - end end diff --git a/app/models/form.rb b/app/models/form.rb index 57b529f10..7ea8ff47e 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -178,5 +178,6 @@ class Form end end end + end end diff --git a/spec/features/form/postcode_format_spec.rb b/spec/features/form/postcode_format_spec.rb new file mode 100644 index 000000000..b9ff03f8a --- /dev/null +++ b/spec/features/form/postcode_format_spec.rb @@ -0,0 +1,42 @@ +require "rails_helper" +require_relative "helpers" + +RSpec.describe "Postcode formatting" do + include Helpers + include Form::Setup + let(:user) { FactoryBot.create(:user) } + let(:case_log) do + FactoryBot.create( + :case_log, + :in_progress, + owning_organisation: user.organisation, + managing_organisation: user.organisation, + ) + end + let(:id) { case_log.id } + let(:validator) { case_log._validators[nil].first } + + before do + sign_in user + end + + +context "when a postcode is input" do + # it "for a 6 character postcode a space will be after the third character" do + # visit("/logs/#{id}/property-postcode") + # fill_in("case-log-postcode-full-field", with: "aa11aa") + # click_button("Save and continue") + # visit("/logs/#{id}/property-information/check-answers") + # expect(page).to have_text("AA1 1AA") + # end + + + it "for a 5 character postcode a space will be after the second character" do + visit("/logs/#{id}/property-postcode") + fill_in("case-log-postcode-full-field", with: "a99aa") + click_button("Save and continue") + visit("/logs/#{id}/property-information/check-answers") + expect(page).to have_text("A9 9AA") + end + end +end \ No newline at end of file