diff --git a/app/models/location.rb b/app/models/location.rb index 954c034f9..1a4d29451 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -16,7 +16,7 @@ class Location < ApplicationRecord before_validation :lookup_postcode!, if: :postcode_changed? - auto_strip_attributes :name + auto_strip_attributes :name, squish: true scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 77b5af50c..c7447cfe6 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -36,7 +36,7 @@ class Organisation < ApplicationRecord has_paper_trail - auto_strip_attributes :name + auto_strip_attributes :name, squish: true PROVIDER_TYPE = { LA: 1, diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 702c8a79d..08b3db265 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -75,7 +75,7 @@ class Scheme < ApplicationRecord validate :validate_confirmed validate :validate_owning_organisation - auto_strip_attributes :service_name + auto_strip_attributes :service_name, squish: true SENSITIVE = { No: 0, diff --git a/app/models/user.rb b/app/models/user.rb index fc26dc686..7042034b4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -38,7 +38,7 @@ class User < ApplicationRecord has_one_time_password(encrypted: true) - auto_strip_attributes :name + auto_strip_attributes :name, squish: true ROLES = { data_provider: 1, diff --git a/lib/tasks/squish_names.rake b/lib/tasks/squish_names.rake new file mode 100644 index 000000000..3fe3da2e9 --- /dev/null +++ b/lib/tasks/squish_names.rake @@ -0,0 +1,19 @@ +desc "Squish names of locations, schemes, users, and organisations" +task squish_names: :environment do + Location.find_each do |location| + location.name.squish! + location.save! + end + Scheme.find_each do |scheme| + scheme.service_name.squish! + scheme.save! + end + User.find_each do |user| + user.name.squish! + user.save! + end + Organisation.find_each do |organisation| + organisation.name.squish! + organisation.save! + end +end diff --git a/spec/lib/tasks/squish_names_spec.rb b/spec/lib/tasks/squish_names_spec.rb new file mode 100644 index 000000000..5b1fcb028 --- /dev/null +++ b/spec/lib/tasks/squish_names_spec.rb @@ -0,0 +1,65 @@ +require "rails_helper" +require "rake" + +RSpec.describe "squish_names" do + describe ":squish_names", type: :task do + subject(:task) { Rake::Task["squish_names"] } + + before do + Rake.application.rake_require("tasks/squish_names") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let!(:scheme) { create(:scheme) } + let!(:location) { create(:location) } + let!(:user) { create(:user) } + let!(:organisation) { create(:organisation) } + + it "updates names with multiple spaces to only have one" do + scheme.service_name = "test test" + location.name = "test test test" + user.name = "test test" + organisation.name = "test test test" + + scheme.save!(validate: false) + location.save!(validate: false) + user.save!(validate: false) + organisation.save!(validate: false) + + task.invoke + scheme.reload + location.reload + user.reload + organisation.reload + expect(scheme.service_name).to eq("test test") + expect(location.name).to eq("test test test") + expect(user.name).to eq("test test") + expect(organisation.name).to eq("test test test") + end + + it "does not update names without multiple spaces" do + scheme.service_name = "test test" + location.name = "test test test" + user.name = "test test" + organisation.name = "test test test" + + scheme.save!(validate: false) + location.save!(validate: false) + user.save!(validate: false) + organisation.save!(validate: false) + + task.invoke + scheme.reload + location.reload + user.reload + organisation.reload + expect(scheme.service_name).to eq("test test") + expect(location.name).to eq("test test test") + expect(user.name).to eq("test test") + expect(organisation.name).to eq("test test test") + end + end + end +end