diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 10a4cba84..e4808e047 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,3 +1,8 @@ class ApplicationRecord < ActiveRecord::Base self.abstract_class = true + + def strip_whitespaces + self.service_name = service_name.strip unless !respond_to?("service_name") || service_name.nil? + self.name = name.strip unless !respond_to?("name") || name.nil? + end end diff --git a/app/models/location.rb b/app/models/location.rb index c6f727f5c..2958f616e 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -6,6 +6,7 @@ class Location < ApplicationRecord has_paper_trail before_save :lookup_postcode!, if: :postcode_changed? + before_validation :strip_whitespaces attr_accessor :add_another_location diff --git a/app/models/organisation.rb b/app/models/organisation.rb index a119b52f7..477ae940b 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -12,8 +12,7 @@ class Organisation < ApplicationRecord has_paper_trail - before_validation :strip_whitespace - + before_validation :strip_whitespaces PROVIDER_TYPE = { LA: 1, PRP: 2, @@ -76,8 +75,4 @@ class Organisation < ApplicationRecord { name: "data_protection_agreement", value: data_protection_agreement_string, editable: false }, ] end - - def strip_whitespace - self.name = name.strip unless name.nil? - end end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 0c05fa8a3..73b3b5106 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -19,7 +19,7 @@ class Scheme < ApplicationRecord validate :validate_confirmed - before_validation :strip_whitespace + before_validation :strip_whitespaces SENSITIVE = { No: 0, @@ -230,8 +230,4 @@ class Scheme < ApplicationRecord end end end - - def strip_whitespace - self.service_name = service_name.strip unless service_name.nil? - end end diff --git a/app/models/user.rb b/app/models/user.rb index c9404d5e2..0d08dc533 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,7 +27,7 @@ class User < ApplicationRecord has_one_time_password(encrypted: true) - before_validation :strip_whitespace + before_validation :strip_whitespaces ROLES = { data_provider: 1, @@ -167,8 +167,4 @@ private end end end - - def strip_whitespace - self.name = name.strip unless name.nil? - end end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index ed2132809..080260128 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -269,7 +269,7 @@ RSpec.describe LocationsController, type: :request do context "when signed in as a support user" do let(:user) { FactoryBot.create(:user, :support) } let!(:scheme) { FactoryBot.create(:scheme) } - let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", add_another_location: "No", postcode: "ZZ1 1ZZ", mobility_type: "N" } } } + let(:params) { { location: { name: " Test", units: "5", type_of_unit: "Bungalow", add_another_location: "No", postcode: "ZZ1 1ZZ", mobility_type: "N" } } } before do allow(user).to receive(:need_two_factor_authentication?).and_return(false)