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. 14
      spec/features/form/postcode_format_spec.rb

3
app/helpers/tab_nav_helper.rb

@ -1,5 +1,6 @@
module TabNavHelper module TabNavHelper
include GovukLinkHelper include GovukLinkHelper
include Helpers
def user_cell(user) def user_cell(user)
link_text = user.name.presence || user.email link_text = user.name.presence || user.email
@ -7,7 +8,7 @@ module TabNavHelper
end end
def location_cell(location, link) 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") [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 end

6
app/models/case_log.rb

@ -566,12 +566,6 @@ private
public_send("age#{person_num}") && public_send("age#{person_num}") < 16 public_send("age#{person_num}") && public_send("age#{person_num}") < 16
end 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! def process_postcode_changes!
self.postcode_full = format_postcode(postcode_full) self.postcode_full = format_postcode(postcode_full)
process_postcode(postcode_full, "postcode_known", "is_la_inferred", "la") 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? 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| 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 hsh
end end
end end

2
config/application.rb

@ -15,6 +15,8 @@ require "action_view/railtie"
# require "sprockets/railtie" # require "sprockets/railtie"
# require "rails/test_unit/railtie" # require "rails/test_unit/railtie"
require_relative '../lib/ext/string'
# Require the gems listed in Gemfile, including any gems # Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production. # you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups) 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

14
spec/features/form/postcode_format_spec.rb

@ -19,13 +19,13 @@ RSpec.describe "Postcode formatting" do
end end
context "when a postcode is input" do context "when a postcode is input" do
# it "for a 6 character postcode a space will be after the third character" do it "for a 6 character postcode a space will be after the third character" do
# visit("/logs/#{id}/property-postcode") visit("/logs/#{id}/property-postcode")
# fill_in("case-log-postcode-full-field", with: "aa11aa") fill_in("case-log-postcode-full-field", with: "aa11aa")
# click_button("Save and continue") click_button("Save and continue")
# visit("/logs/#{id}/property-information/check-answers") visit("/logs/#{id}/property-information/check-answers")
# expect(page).to have_text("AA1 1AA") expect(page).to have_text("AA1 1AA")
# end end
it "for a 5 character postcode a space will be after the second character" do it "for a 5 character postcode a space will be after the second character" do
visit("/logs/#{id}/property-postcode") visit("/logs/#{id}/property-postcode")

Loading…
Cancel
Save