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

move name validation from query model to contract

ulferts84278a7fc248
diff --git a/app/contracts/queries/base_contract.rb b/app/contracts/queries/base_contract.rb
index 18bdf8dc577..22efd429d2a 100644
--- a/app/contracts/queries/base_contract.rb
+++ b/app/contracts/queries/base_contract.rb
@@ -62,6 +62,10 @@ module Queries
validate :user_allowed_to_make_public
validate :timestamps_are_parsable
+ validates :name,
+ presence: true,
+ length: { maximum: 255 }
+
def validate_project
errors.add :project, :error_not_found if project_id.present? && !project_visible?
end
diff --git a/app/models/query.rb b/app/models/query.rb
index e7ca3951f5d..7562e43ba02 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -42,10 +42,6 @@ class Query < ApplicationRecord
serialize :column_names, Array
serialize :sort_criteria, Array
- validates :name,
- presence: true,
- length: { maximum: 255 }
-
validates :include_subprojects,
inclusion: [true, false]
diff --git a/db/migrate/20230531093004_remove_default_from_query_name.rb b/db/migrate/20230531093004_remove_default_from_query_name.rb
new file mode 100644
index 00000000000..8f7f0c63466
--- /dev/null
+++ b/db/migrate/20230531093004_remove_default_from_query_name.rb
@@ -0,0 +1,5 @@
+class RemoveDefaultFromQueryName < ActiveRecord::Migration[7.0]
+ def change
+ change_column_default :queries, :name, to: nil, from: ''
+ end
+end
diff --git a/spec/contracts/queries/shared_contract_examples.rb b/spec/contracts/queries/shared_contract_examples.rb
index f94dabeea3b..3c2febe20ec 100644
--- a/spec/contracts/queries/shared_contract_examples.rb
+++ b/spec/contracts/queries/shared_contract_examples.rb
@@ -31,8 +31,12 @@ require 'contracts/shared/model_contract_shared_context'
shared_context 'with queries contract' do
let(:project) { build_stubbed(:project) }
+ let(:name) { 'Some query name' }
+ let(:public) { false }
+ let(:user) { current_user }
+ let(:permissions) { %i[view_queries save_queries] }
let(:query) do
- build_stubbed(:query, project:, public:, user:)
+ build_stubbed(:query, project:, public:, user:, name:)
end
let(:current_user) do
@@ -49,4 +53,20 @@ shared_context 'with queries contract' do
# Assume project is always visible
allow(contract).to receive(:project_visible?).and_return true
end
+
+ describe 'validation' do
+ it_behaves_like 'contract is valid'
+
+ context 'if the name is nil' do
+ let(:name) { nil }
+
+ it_behaves_like 'contract is invalid', name: :blank
+ end
+
+ context 'if the name is empty' do
+ let(:name) { '' }
+
+ it_behaves_like 'contract is invalid', name: :blank
+ end
+ end
end
diff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rb
index 6309d1ad05c..2735d5e17fa 100644
--- a/spec/models/query_spec.rb
+++ b/spec/models/query_spec.rb
@@ -521,12 +521,6 @@ describe Query,
end
describe '#valid?' do
- it 'is not valid without a name' do
- query.name = ''
- expect(query.save).to be_falsey
- expect(query.errors[:name].first).to include(I18n.t('activerecord.errors.messages.blank'))
- end
-
context 'with a missing value and an operator that requires values' do
before do
query.add_filter('due_date', 't-', [''])