Browse Source

Keep spaces in postcode

Co-authored-by: Stéphane <smeny@users.noreply.github.com>
pull/781/head
Ted-U 3 years ago
parent
commit
8c49e5e693
  1. 3
      app/helpers/tab_nav_helper.rb
  2. 18
      app/models/case_log.rb
  3. 4
      app/models/form/setup/questions/location_id.rb
  4. 10
      app/models/location.rb
  5. 2
      app/models/scheme.rb
  6. 17
      app/services/refinement_test.rb
  7. 2
      spec/features/schemes_helpers.rb
  8. 14
      spec/features/schemes_spec.rb
  9. 4
      spec/helpers/tab_nav_helper_spec.rb
  10. 2
      spec/requests/case_logs_controller_spec.rb
  11. 6
      spec/requests/locations_controller_spec.rb

3
app/helpers/tab_nav_helper.rb

@ -1,5 +1,4 @@
module TabNavHelper
using RefinementTest
include GovukLinkHelper
def user_cell(user)
@ -8,7 +7,7 @@ module TabNavHelper
end
def location_cell(location, link)
link_text = location.postcode.formatted_postcode
link_text = location.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

18
app/models/case_log.rb

@ -56,7 +56,7 @@ class CaseLog < ApplicationRecord
scope :filter_by_id, ->(id) { where(id:) }
scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") }
scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") }
scope :filter_by_postcode, ->(postcode_full) { where("postcode_full ILIKE ?", "%#{postcode_full.gsub(/\s+/, '')}%") }
scope :filter_by_postcode, ->(postcode_full) { where("postcode_full ILIKE ?", "%#{UKPostcode.parse(postcode_full)}%") }
scope :search_by, lambda { |param|
filter_by_id(param)
.or(filter_by_tenant_code(param))
@ -117,6 +117,22 @@ class CaseLog < ApplicationRecord
end
end
def postcode_full=(postcode)
if postcode
super UKPostcode.parse(postcode).to_s
else
super nil
end
end
def ppostcode_full=(postcode)
if postcode
super UKPostcode.parse(postcode).to_s
else
super nil
end
end
def completed?
status == "completed"
end

4
app/models/form/setup/questions/location_id.rb

@ -1,6 +1,4 @@
class Form::Setup::Questions::LocationId < ::Form::Question
using RefinementTest
def initialize(_id, hsh, page)
super("location_id", hsh, page)
@check_answer_label = "Location"
@ -20,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.formatted_postcode, "hint" => location.name }
hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name }
hsh
end
end

10
app/models/location.rb

@ -7,7 +7,7 @@ class Location < ApplicationRecord
attr_accessor :add_another_location
scope :search_by_postcode, ->(postcode) { where("postcode ILIKE ?", "%#{postcode.gsub(/\s+/, '')}%") }
scope :search_by_postcode, ->(postcode) { where("postcode ILIKE ?", "%#{UKPostcode.parse(postcode)}%") }
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) }
@ -41,6 +41,14 @@ class Location < ApplicationRecord
]
end
def postcode=(postcode)
if postcode
super UKPostcode.parse(postcode).to_s
else
super nil
end
end
private
PIO = PostcodeService.new

2
app/models/scheme.rb

@ -6,7 +6,7 @@ class Scheme < ApplicationRecord
scope :filter_by_id, ->(id) { where(id: (id.start_with?("S") ? id[1..] : id)) }
scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") }
scope :search_by_postcode, ->(postcode) { joins("LEFT JOIN locations ON locations.scheme_id = schemes.id").where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") }
scope :search_by_postcode, ->(postcode) { joins("LEFT JOIN locations ON locations.scheme_id = schemes.id").where("locations.postcode ILIKE ?", "%#{UKPostcode.parse(postcode)}%") }
scope :search_by, ->(param) { search_by_postcode(param).or(search_by_service_name(param)).or(filter_by_id(param)).distinct }
validate :validate_confirmed

17
app/services/refinement_test.rb

@ -1,17 +0,0 @@
module RefinementTest
refine String do
def formatted_postcode
postcode = 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
end

2
spec/features/schemes_helpers.rb

@ -68,7 +68,7 @@ module SchemesHelpers
end
def fill_in_and_save_second_location
fill_in "Postcode", with: "XX1 1XX"
fill_in "Postcode", with: "AA1 2AA"
fill_in "Location name (optional)", with: "Other name"
fill_in "Total number of units at this location", with: 2
choose "Self-contained house"

14
spec/features/schemes_spec.rb

@ -1,8 +1,6 @@
require "rails_helper"
require_relative "schemes_helpers"
using RefinementTest
RSpec.describe "Schemes scheme Features" do
include SchemesHelpers
context "when viewing list of schemes" do
@ -205,7 +203,7 @@ RSpec.describe "Schemes scheme Features" do
it "shows details of those locations" do
locations.each do |location|
expect(page).to have_content(location.id)
expect(page).to have_content(location.postcode.formatted_postcode)
expect(page).to have_content(location.postcode)
expect(page).to have_content(location.units)
expect(page).to have_content(location.type_of_unit)
expect(page).to have_content(location.startdate&.to_formatted_s(:govuk_date))
@ -495,7 +493,7 @@ RSpec.describe "Schemes scheme Features" do
it "displays information about newly created location" do
click_link "Add a location"
fill_in_and_save_second_location
expect(page).to have_content "XX1 1XX"
expect(page).to have_content "AA1 2AA"
expect(page).to have_content "Other name"
expect(page).to have_content "Self-contained house"
end
@ -509,13 +507,13 @@ RSpec.describe "Schemes scheme Features" do
end
it "displays changed location" do
click_link "XX1 1XX"
fill_in "Postcode", with: "ZZ1 1ZZ"
click_link "AA1 2AA"
fill_in "Postcode", with: "AA1 3AA"
choose "location-mobility-type-wheelchair-user-standard-field"
click_button "Save and continue"
expect(page).to have_content "Locations"
expect(page).to have_content "#{scheme.locations.count} location"
expect(page).to have_content "ZZ1 1ZZ"
expect(page).to have_content "AA1 3AA"
end
end
@ -725,7 +723,7 @@ RSpec.describe "Schemes scheme Features" do
context "when I click to change location name" do
before do
click_link(location.postcode.formatted_postcode)
click_link(location.postcode)
end
it "shows available fields to edit" do

4
spec/helpers/tab_nav_helper_spec.rb

@ -1,7 +1,5 @@
require "rails_helper"
using RefinementTest
RSpec.describe TabNavHelper do
let(:organisation) { FactoryBot.create(:organisation) }
let(:user) { FactoryBot.build(:user, organisation:) }
@ -25,7 +23,7 @@ RSpec.describe TabNavHelper do
describe "#location_cell" do
it "returns the location link to the postcode with optional name" do
link = "/schemes/#{location.scheme.id}/locations/#{location.id}/edit"
expected_html = "<a class=\"govuk-link\" rel=\"nofollow\" data-method=\"patch\" href=\"/schemes/#{scheme.id}/locations/#{location.id}/edit\">#{location.postcode.formatted_postcode}</a>\n<span class=\"govuk-visually-hidden\">Location </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{location.name}</span>"
expected_html = "<a class=\"govuk-link\" rel=\"nofollow\" data-method=\"patch\" href=\"/schemes/#{scheme.id}/locations/#{location.id}/edit\">#{location.postcode}</a>\n<span class=\"govuk-visually-hidden\">Location </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{location.name}</span>"
expect(location_cell(location, link)).to match(expected_html)
end
end

2
spec/requests/case_logs_controller_spec.rb

@ -792,7 +792,7 @@ RSpec.describe CaseLogsController, type: :request do
it "downloads logs matching both csv and filter logs" do
get "/logs?status[]=completed&search=#{postcode}", headers:, params: {}
csv = CSV.parse(response.body)
expect(csv.count).to eq(2)
expect(csv.count).to eq(1)
end
end
end

6
spec/requests/locations_controller_spec.rb

@ -1,7 +1,5 @@
require "rails_helper"
using RefinementTest
RSpec.describe LocationsController, type: :request do
let(:page) { Capybara::Node::Simple.new(response.body) }
let(:user) { FactoryBot.create(:user, :support) }
@ -804,7 +802,7 @@ RSpec.describe LocationsController, type: :request do
it "shows scheme" do
locations.each do |location|
expect(page).to have_content(location.id)
expect(page).to have_content(location.postcode.formatted_postcode)
expect(page).to have_content(location.postcode)
expect(page).to have_content(location.type_of_unit)
expect(page).to have_content(location.startdate&.to_formatted_s(:govuk_date))
end
@ -906,7 +904,7 @@ RSpec.describe LocationsController, type: :request do
it "shows scheme" do
locations.each do |location|
expect(page).to have_content(location.id)
expect(page).to have_content(location.postcode.formatted_postcode)
expect(page).to have_content(location.postcode)
expect(page).to have_content(location.type_of_unit)
expect(page).to have_content(location.startdate&.to_formatted_s(:govuk_date))
end

Loading…
Cancel
Save