diff --git a/app/helpers/tab_nav_helper.rb b/app/helpers/tab_nav_helper.rb index 1f6db638e..4dc015763 100644 --- a/app/helpers/tab_nav_helper.rb +++ b/app/helpers/tab_nav_helper.rb @@ -1,5 +1,6 @@ module TabNavHelper include GovukLinkHelper + include Helpers def user_cell(user) link_text = user.name.presence || user.email @@ -7,7 +8,7 @@ module TabNavHelper end def location_cell(location, link) - link_text = location.postcode + link_text = location.postcode.formatted_postcode [govuk_link_to(link_text, link, method: :patch), "Location #{location.name}"].join("\n") end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index c818b0c32..fdb4972c9 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -566,12 +566,6 @@ private public_send("age#{person_num}") && public_send("age#{person_num}") < 16 end - def format_postcode(value) - value = value.upcase.gsub(/\s+/, "") - value = value.length == 5 ? value.insert(2, " ") : value.insert(3, " ") - value.strip - end - def process_postcode_changes! self.postcode_full = format_postcode(postcode_full) process_postcode(postcode_full, "postcode_known", "is_la_inferred", "la") diff --git a/app/models/form/setup/questions/location_id.rb b/app/models/form/setup/questions/location_id.rb index d5a0976f7..d0650f9ea 100644 --- a/app/models/form/setup/questions/location_id.rb +++ b/app/models/form/setup/questions/location_id.rb @@ -18,7 +18,7 @@ class Form::Setup::Questions::LocationId < ::Form::Question return answer_opts unless ActiveRecord::Base.connected? Location.select(:id, :postcode, :name).where("startdate <= ? or startdate IS NULL", Time.zone.today).each_with_object(answer_opts) do |location, hsh| - hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name } + hsh[location.id.to_s] = { "value" => location.postcode.formatted_postcode, "hint" => location.name } hsh end end diff --git a/config/application.rb b/config/application.rb index 7bdbfb93e..74d1375b1 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,6 +15,8 @@ require "action_view/railtie" # require "sprockets/railtie" # require "rails/test_unit/railtie" +require_relative '../lib/ext/string' + # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) diff --git a/lib/ext/string.rb b/lib/ext/string.rb new file mode 100644 index 000000000..8fa3751cb --- /dev/null +++ b/lib/ext/string.rb @@ -0,0 +1,15 @@ +class String + def formatted_postcode + postcode = self.upcase.gsub(/\s+/, "") + case postcode.length + when 5 + postcode.insert(2, " ") + when 6 + postcode.insert(3, " ") + when 7 + postcode.insert(4, " ") + else + self + end + end + end \ No newline at end of file diff --git a/spec/features/form/postcode_format_spec.rb b/spec/features/form/postcode_format_spec.rb index eca14ae40..36c2460a0 100644 --- a/spec/features/form/postcode_format_spec.rb +++ b/spec/features/form/postcode_format_spec.rb @@ -19,17 +19,17 @@ RSpec.describe "Postcode formatting" do 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 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: "a9 9aa") + 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")