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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#37583] Error "Identifier is invalid" (#9373)
ulfertsa8c075e258b7
diff --git a/app/models/project.rb b/app/models/project.rbindex 6410f7f84e4..53c4b999ca9 100644--- a/app/models/project.rb+++ b/app/models/project.rb@@ -118,6 +118,10 @@ class Project < ApplicationRecordonly_when_blank: true, # Only generate when identifier not setlimit: IDENTIFIER_MAX_LENGTH,blacklist: RESERVED_IDENTIFIERS,+ custom_rule: ->(base_url) {+ # remove leading numbers and hyphens as they would clash with the identifier's validations later on.+ base_url.sub(/^[-\d]*|-*$/, '')+ },adapter: OpenProject::ActsAsUrl::Adapter::OpActiveRecord # use a custom adapter able to handle edge casesvalidates :identifier,diff --git a/lib/open_project/acts_as_url/adapter/op_active_record.rb b/lib/open_project/acts_as_url/adapter/op_active_record.rbindex 812dccbb438..01992eca5f6 100644--- a/lib/open_project/acts_as_url/adapter/op_active_record.rb+++ b/lib/open_project/acts_as_url/adapter/op_active_record.rb@@ -31,7 +31,10 @@# Improves handling of some edge cases when to_url is called. The method is provided by# stringex but some edge cases have not been handled properly by that gem.#-# Currently, this is limited to the string '.' which would lead to an empty string otherwise.+# This includes+# * the strings '.' and '!' which would lead to an empty string otherwise+# * the ability to add a custom_rule lambda that is able to postprocess the identifier. It will run+# after the default transformation was executed.module OpenProjectmodule ActsAsUrl@@ -57,6 +60,10 @@ module OpenProjectdef modify_base_urlsuper+ if !base_url.empty? && settings.respond_to?(:custom_rule)+ self.base_url = settings.custom_rule.call(base_url)+ end+modify_base_url_custom_rules if base_url.empty?enddiff --git a/spec/services/projects/set_attributes_service_integration_spec.rb b/spec/services/projects/set_attributes_service_integration_spec.rbindex e3031b1a098..f15674f9d02 100644--- a/spec/services/projects/set_attributes_service_integration_spec.rb+++ b/spec/services/projects/set_attributes_service_integration_spec.rb@@ -35,12 +35,23 @@ describe Projects::SetAttributesService, 'integration', type: :model dolet(:contract) { Projects::CreateContract }let(:instance) { described_class.new(user: user, model: project, contract_class: contract) }let(:attributes) { {} }+ let(:project) { Project.new }let(:service_result) doinstance.call(attributes)end+ describe 'with a project name starting with numbers' do+ let(:attributes) { { name: '100 Project A' } }++ it 'will create an identifier with the numbers stripped' do+ expect(service_result).to be_success+ expect(service_result.result.identifier).to eq 'project-a'+ end+ end+describe 'with an existing project' do- let!(:existing) { FactoryBot.create :project, identifier: 'my-new-project' }+ let(:existing_identifier) { 'my-new-project' }+ let!(:existing) { FactoryBot.create :project, identifier: existing_identifier }context 'and a new project with no identifier set' dolet(:project) { Project.new name: 'My new project' }@@ -62,5 +73,14 @@ describe Projects::SetAttributesService, 'integration', type: :model doexpect(errors).to eq ['Identifier has already been taken.']endend++ context 'with an existing identifier and a project name starting with numbers' do+ let(:attributes) { { name: '100 My new project' } }++ it 'will auto correct the identifier with the numbers stripped' do+ expect(service_result).to be_success+ expect(service_result.result.identifier).to eq 'my-new-project-1'+ end+ endendend