Browse Source

CLDC-2895 Add squish to the names of locations, schemes, users, and organisations (#2004)

* CLDC-2895 Add squish to the names of locations, schemes, users, and institutions

* CLDC-2895 Add rake task to squish the names of all locations, schemes, users, and organisations

* CLDC-2895 Add tests to squish_names rake tasks
demo-branch
David May-Miller 1 year ago committed by GitHub
parent
commit
4ec9e95abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/location.rb
  2. 2
      app/models/organisation.rb
  3. 2
      app/models/scheme.rb
  4. 2
      app/models/user.rb
  5. 19
      lib/tasks/squish_names.rake
  6. 65
      spec/lib/tasks/squish_names_spec.rb

2
app/models/location.rb

@ -16,7 +16,7 @@ class Location < ApplicationRecord
before_validation :lookup_postcode!, if: :postcode_changed? 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_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") }
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }

2
app/models/organisation.rb

@ -36,7 +36,7 @@ class Organisation < ApplicationRecord
has_paper_trail has_paper_trail
auto_strip_attributes :name auto_strip_attributes :name, squish: true
PROVIDER_TYPE = { PROVIDER_TYPE = {
LA: 1, LA: 1,

2
app/models/scheme.rb

@ -75,7 +75,7 @@ class Scheme < ApplicationRecord
validate :validate_confirmed validate :validate_confirmed
validate :validate_owning_organisation validate :validate_owning_organisation
auto_strip_attributes :service_name auto_strip_attributes :service_name, squish: true
SENSITIVE = { SENSITIVE = {
No: 0, No: 0,

2
app/models/user.rb

@ -38,7 +38,7 @@ class User < ApplicationRecord
has_one_time_password(encrypted: true) has_one_time_password(encrypted: true)
auto_strip_attributes :name auto_strip_attributes :name, squish: true
ROLES = { ROLES = {
data_provider: 1, data_provider: 1,

19
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

65
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
Loading…
Cancel
Save