Skip to content

Commit

Permalink
Merge pull request #2351 from alphagov/fix-whitespace-in-organisation…
Browse files Browse the repository at this point in the history
…-names

Fix and prevent unnecessary whitespace in organisation names
  • Loading branch information
chrislo committed Sep 11, 2023
2 parents 5685f76 + a9ba453 commit 6d625fc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Organisation < ApplicationRecord
validates :name, presence: true
validates :organisation_type, presence: true

before_save :strip_whitespace_from_name

def name_with_abbreviation
return_value = if abbreviation.present? && abbreviation != name
"#{name}#{abbreviation}"
Expand All @@ -21,4 +23,10 @@ def name_with_abbreviation

return_value
end

private

def strip_whitespace_from_name
name.strip!
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class StripWhitespaceFromOrganisationNames < ActiveRecord::Migration[7.0]
NAME_HAS_LEADING_OR_TRAILING_SPACE = "name REGEXP('^\s+') OR name REGEXP('\s+$')".freeze

def up
Organisation.where(NAME_HAS_LEADING_OR_TRAILING_SPACE).each(&:save!)
end

def down; end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_08_16_164936) do
ActiveRecord::Schema[7.0].define(version: 2023_09_11_092404) do
create_table "batch_invitation_application_permissions", id: :integer, charset: "utf8mb3", force: :cascade do |t|
t.integer "batch_invitation_id", null: false
t.integer "supported_permission_id", null: false
Expand Down
6 changes: 6 additions & 0 deletions test/models/organisation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def setup
end
end

test "strips unwanted whitespace from name" do
organisation = create(:organisation, name: " An organisation ")

assert_equal "An organisation", organisation.name
end

context "displaying name with abbreviation" do
should "use abbreviation when it is not the same as name" do
organisation = build(:organisation, name: "An Organisation", abbreviation: "ABBR")
Expand Down

0 comments on commit 6d625fc

Please sign in to comment.