Browse Source

format postcode

pull/781/head
Ted-U 3 years ago
parent
commit
307049e956
  1. 3
      app/helpers/tab_nav_helper.rb
  2. 6
      app/models/case_log.rb
  3. 2
      app/models/form/setup/questions/location_id.rb
  4. 2
      config/application.rb
  5. 15
      lib/ext/string.rb
  6. 16
      spec/features/form/postcode_format_spec.rb

3
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), "<span class=\"govuk-visually-hidden\">Location </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{location.name}</span>"].join("\n")
end

6
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")

2
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

2
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)

15
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

16
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")

Loading…
Cancel
Save