Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Use normalization to squish Project#name whitespace

Alexander Brandon Colesf1d5ab49dda6
diff --git a/app/models/project.rb b/app/models/project.rb
index e47928838a9..2909625418d 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -156,7 +156,7 @@ class Project < ApplicationRecord
presence: true,
length: { maximum: 255 }
- before_validation :remove_white_spaces_from_project_name
+ normalizes :name, with: ->(name) { name.squish }
# TODO: we temporarily disable this validation because it leads to failed tests
# it implicitly assumes a db:seed-created standard type to be present and currently
@@ -305,8 +305,4 @@ class Project < ApplicationRecord
OpenProject::AccessControl.allowed_actions(permission)
end
end
-
- def remove_white_spaces_from_project_name
- self.name = name.squish unless name.nil?
- end
end
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 930baefc149..22eb1e0f514 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -156,10 +156,9 @@ RSpec.describe Project do
let(:name) { " Hello World " }
let(:project) { described_class.new attributes_for(:project, name:) }
- context "with white spaces in the name" do
- it "trims the name" do
- project.save
- expect(project.name).to eql("Hello World")
+ context "with whitespace in the name" do
+ it "normalizes excess whitespace" do
+ expect(subject).to normalize(:name).from(name).to("Hello World")
end
end