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
move name validation from query model to contract
ulferts84278a7fc248
diff --git a/app/contracts/queries/base_contract.rb b/app/contracts/queries/base_contract.rbindex 18bdf8dc577..22efd429d2a 100644--- a/app/contracts/queries/base_contract.rb+++ b/app/contracts/queries/base_contract.rb@@ -62,6 +62,10 @@ module Queriesvalidate :user_allowed_to_make_publicvalidate :timestamps_are_parsable+ validates :name,+ presence: true,+ length: { maximum: 255 }+def validate_projecterrors.add :project, :error_not_found if project_id.present? && !project_visible?enddiff --git a/app/models/query.rb b/app/models/query.rbindex e7ca3951f5d..7562e43ba02 100644--- a/app/models/query.rb+++ b/app/models/query.rb@@ -42,10 +42,6 @@ class Query < ApplicationRecordserialize :column_names, Arrayserialize :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.rbnew file mode 100644index 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+enddiff --git a/spec/contracts/queries/shared_contract_examples.rb b/spec/contracts/queries/shared_contract_examples.rbindex 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' dolet(: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:)endlet(:current_user) do@@ -49,4 +53,20 @@ shared_context 'with queries contract' do# Assume project is always visibleallow(contract).to receive(:project_visible?).and_return trueend++ 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+ endenddiff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rbindex 6309d1ad05c..2735d5e17fa 100644--- a/spec/models/query_spec.rb+++ b/spec/models/query_spec.rb@@ -521,12 +521,6 @@ describe Query,enddescribe '#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' dobefore doquery.add_filter('due_date', 't-', [''])