Browse Source

format postcode - add space

pull/781/head
Ted-U 3 years ago
parent
commit
c92771a8e3
  1. 15
      app/models/case_log.rb
  2. 1
      app/models/form.rb
  3. 42
      spec/features/form/postcode_format_spec.rb

15
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

1
app/models/form.rb

@ -178,5 +178,6 @@ class Form
end
end
end
end
end

42
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
Loading…
Cancel
Save