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

persisted project queries

ulferts83bd82d33d1b
diff --git a/app/models/queries/actions/action_query.rb b/app/models/queries/actions/action_query.rb
index 54096895df0..d00b821998d 100644
--- a/app/models/queries/actions/action_query.rb
+++ b/app/models/queries/actions/action_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Actions::ActionQuery < Queries::BaseQuery
+class Queries::Actions::ActionQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Action
end
diff --git a/app/models/queries/base_query.rb b/app/models/queries/base_query.rb
index 1f5c94f7183..1efd55914a1 100644
--- a/app/models/queries/base_query.rb
+++ b/app/models/queries/base_query.rb
@@ -26,8 +26,21 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::BaseQuery
- class << self
+module Queries::BaseQuery
+ extend ActiveSupport::Concern
+
+ included do
+ include Queries::Filters::AvailableFilters
+ include Queries::Orders::AvailableOrders
+ include Queries::GroupBys::AvailableGroupBys
+ include ActiveModel::Validations
+
+ validate :filters_valid,
+ :sortation_valid
+ validate :group_by_valid, if: -> { respond_to?(:group_by) }
+ end
+
+ class_methods do
def model
@model ||= name.demodulize.gsub('Query', '').constantize
end
@@ -37,25 +50,6 @@ class Queries::BaseQuery
end
end
- attr_accessor :filters, :orders
- attr_reader :group_by
-
- include Queries::Filters::AvailableFilters
- include Queries::Orders::AvailableOrders
- include Queries::GroupBys::AvailableGroupBys
- include ActiveModel::Validations
-
- validate :filters_valid,
- :sortation_valid,
- :group_by_valid
-
- def initialize(user: nil)
- @filters = []
- @orders = []
- @group_by = nil
- @user = user
- end
-
def results
if valid?
apply_orders(apply_filters(default_scope))
@@ -122,9 +116,6 @@ class Queries::BaseQuery
protected
- attr_accessor :user
- attr_writer :group_by
-
def filters_valid
filters.each do |filter|
next if filter.valid?
@@ -195,7 +186,7 @@ class Queries::BaseQuery
end
def build_orders
- return orders if group_by.nil? || has_group_by_order?
+ return orders if !respond_to?(:group_by) || group_by.nil? || has_group_by_order?
[group_by_order] + orders
end
diff --git a/app/models/queries/capabilities/capability_query.rb b/app/models/queries/capabilities/capability_query.rb
index a6152a1aa50..7b7cfb9dce5 100644
--- a/app/models/queries/capabilities/capability_query.rb
+++ b/app/models/queries/capabilities/capability_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Capabilities::CapabilityQuery < Queries::BaseQuery
+class Queries::Capabilities::CapabilityQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Capability
end
diff --git a/app/models/queries/days/day_query.rb b/app/models/queries/days/day_query.rb
index 1a54a453081..a4560d9c1a4 100644
--- a/app/models/queries/days/day_query.rb
+++ b/app/models/queries/days/day_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Days::DayQuery < Queries::BaseQuery
+class Queries::Days::DayQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Day
end
diff --git a/app/models/queries/groups/group_query.rb b/app/models/queries/groups/group_query.rb
index 676d48192c7..e691edfe491 100644
--- a/app/models/queries/groups/group_query.rb
+++ b/app/models/queries/groups/group_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Groups::GroupQuery < Queries::BaseQuery
+class Queries::Groups::GroupQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Group
end
diff --git a/app/models/queries/members/member_query.rb b/app/models/queries/members/member_query.rb
index 50c7c948c53..2dd2b1e6ff8 100644
--- a/app/models/queries/members/member_query.rb
+++ b/app/models/queries/members/member_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Members::MemberQuery < Queries::BaseQuery
+class Queries::Members::MemberQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Member
end
diff --git a/app/models/queries/news/news_query.rb b/app/models/queries/news/news_query.rb
index 71ed883995d..b9e84cb5bd4 100644
--- a/app/models/queries/news/news_query.rb
+++ b/app/models/queries/news/news_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::News::NewsQuery < Queries::BaseQuery
+class Queries::News::NewsQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
News
end
diff --git a/app/models/queries/non_working_days/non_working_day_query.rb b/app/models/queries/non_working_days/non_working_day_query.rb
index 7cf9c3c9f15..ba9378e856b 100644
--- a/app/models/queries/non_working_days/non_working_day_query.rb
+++ b/app/models/queries/non_working_days/non_working_day_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::NonWorkingDays::NonWorkingDayQuery < Queries::BaseQuery
+class Queries::NonWorkingDays::NonWorkingDayQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
NonWorkingDay
end
diff --git a/app/models/queries/notifications/notification_query.rb b/app/models/queries/notifications/notification_query.rb
index 12082ec0551..2fd7587a4b9 100644
--- a/app/models/queries/notifications/notification_query.rb
+++ b/app/models/queries/notifications/notification_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Notifications::NotificationQuery < Queries::BaseQuery
+class Queries::Notifications::NotificationQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Notification
end
diff --git a/app/models/queries/placeholder_users/placeholder_user_query.rb b/app/models/queries/placeholder_users/placeholder_user_query.rb
index 58c83151472..d167ad15578 100644
--- a/app/models/queries/placeholder_users/placeholder_user_query.rb
+++ b/app/models/queries/placeholder_users/placeholder_user_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::PlaceholderUsers::PlaceholderUserQuery < Queries::BaseQuery
+class Queries::PlaceholderUsers::PlaceholderUserQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
PlaceholderUser
end
diff --git a/app/models/queries/principals/principal_query.rb b/app/models/queries/principals/principal_query.rb
index 14523d7eb9f..d97c21c9cfe 100644
--- a/app/models/queries/principals/principal_query.rb
+++ b/app/models/queries/principals/principal_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Principals::PrincipalQuery < Queries::BaseQuery
+class Queries::Principals::PrincipalQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Principal
end
diff --git a/app/models/queries/projects/project_query.rb b/app/models/queries/projects/project_query.rb
index 12266ee90c2..2906d69ddf3 100644
--- a/app/models/queries/projects/project_query.rb
+++ b/app/models/queries/projects/project_query.rb
@@ -26,9 +26,14 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Projects::ProjectQuery < Queries::BaseQuery
+class Queries::Projects::ProjectQuery < ApplicationRecord
+ include Queries::BaseQuery
include Queries::Serialization::Hash
+ belongs_to :user
+
+ serialize :filters, Queries::Serialization::Filters.new(self)
+
def self.model
Project
end
diff --git a/app/models/queries/queries/query_query.rb b/app/models/queries/queries/query_query.rb
index a5ebc0c20d3..f79d8e691b5 100644
--- a/app/models/queries/queries/query_query.rb
+++ b/app/models/queries/queries/query_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Queries::QueryQuery < Queries::BaseQuery
+class Queries::Queries::QueryQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Query
end
diff --git a/app/models/queries/relations/relation_query.rb b/app/models/queries/relations/relation_query.rb
index 74371bd6763..0d3c149ab49 100644
--- a/app/models/queries/relations/relation_query.rb
+++ b/app/models/queries/relations/relation_query.rb
@@ -28,7 +28,10 @@
module Queries
module Relations
- class RelationQuery < ::Queries::BaseQuery
+ class RelationQuery
+ include ::Queries::BaseQuery
+ include ::Queries::UnpersistedQuery
+
def self.model
Relation
end
diff --git a/app/models/queries/roles/role_query.rb b/app/models/queries/roles/role_query.rb
index 63e18506cae..500a94de4ac 100644
--- a/app/models/queries/roles/role_query.rb
+++ b/app/models/queries/roles/role_query.rb
@@ -26,4 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Roles::RoleQuery < Queries::BaseQuery; end
+class Queries::Roles::RoleQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+end
diff --git a/app/models/queries/serialization/filters.rb b/app/models/queries/serialization/filters.rb
new file mode 100644
index 00000000000..1d54edbd68b
--- /dev/null
+++ b/app/models/queries/serialization/filters.rb
@@ -0,0 +1,64 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class Queries::Serialization::Filters
+ include Queries::Filters::AvailableFilters
+ include Queries::Filters::AvailableFilters::ClassMethods
+
+ def load(serialized_filter_hash)
+ return [] if serialized_filter_hash.nil?
+
+ serialized_filter_hash.map do |serialized_filter|
+ filter = filter_for(serialized_filter['field'], no_memoization: true)
+ filter.operator = serialized_filter['operator']
+ filter.values = serialized_filter['values']
+
+ filter
+ end
+ end
+
+ def dump(filters)
+ (filters || []).map do |filter|
+ {
+ field: filter.field,
+ operator: filter.operator,
+ values: filter.values
+ }
+ end
+ end
+
+ def registered_filters
+ Queries::Register.filters[klass]
+ end
+
+ def initialize(klass)
+ @klass = klass
+ end
+
+ attr_reader :klass
+end
diff --git a/app/models/queries/unpersisted_query.rb b/app/models/queries/unpersisted_query.rb
new file mode 100644
index 00000000000..5b7ddf6df48
--- /dev/null
+++ b/app/models/queries/unpersisted_query.rb
@@ -0,0 +1,49 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+module Queries::UnpersistedQuery
+ extend ActiveSupport::Concern
+
+ included do
+ attr_accessor :filters,
+ :orders
+ attr_reader :group_by
+
+ def initialize(*args)
+ @filters = []
+ @orders = []
+ @group_by = nil
+ @user = args.first[:user] if args&.first
+ end
+
+ protected
+
+ attr_accessor :user
+ attr_writer :group_by
+ end
+end
diff --git a/app/models/queries/users/user_query.rb b/app/models/queries/users/user_query.rb
index 59d60fcf68b..10ea8372346 100644
--- a/app/models/queries/users/user_query.rb
+++ b/app/models/queries/users/user_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Users::UserQuery < Queries::BaseQuery
+class Queries::Users::UserQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
User
end
diff --git a/app/models/queries/versions/version_query.rb b/app/models/queries/versions/version_query.rb
index 192d0bc2c4e..f71fe835bab 100644
--- a/app/models/queries/versions/version_query.rb
+++ b/app/models/queries/versions/version_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Versions::VersionQuery < Queries::BaseQuery
+class Queries::Versions::VersionQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Version
end
diff --git a/app/models/queries/views/view_query.rb b/app/models/queries/views/view_query.rb
index aed4603268f..97e0e9e5b4c 100644
--- a/app/models/queries/views/view_query.rb
+++ b/app/models/queries/views/view_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Views::ViewQuery < Queries::BaseQuery
+class Queries::Views::ViewQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def default_scope
super.where(query_id: Query.visible(User.current))
end
diff --git a/app/models/queries/week_days/week_day_query.rb b/app/models/queries/week_days/week_day_query.rb
index d196fa09f94..299bf9084e8 100644
--- a/app/models/queries/week_days/week_day_query.rb
+++ b/app/models/queries/week_days/week_day_query.rb
@@ -26,5 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::WeekDays::WeekDayQuery < Queries::BaseQuery
+class Queries::WeekDays::WeekDayQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
end
diff --git a/app/models/user.rb b/app/models/user.rb
index bbba1c38f7c..756c8231423 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -82,7 +82,14 @@ class User < Principal
inverse_of: :user,
dependent: :destroy
- has_many :notification_settings, dependent: :destroy
+ has_many :notification_settings,
+ dependent: :destroy
+
+ has_many :project_queries,
+ class_name: 'Queries::Projects::ProjectQuery',
+ inverse_of: :user,
+ dependent: :destroy
+
# Users blocked via brute force prevention
# use lambda here, so time is evaluated on each query
diff --git a/db/migrate/20240104172050_add_project_queries.rb b/db/migrate/20240104172050_add_project_queries.rb
new file mode 100644
index 00000000000..4295cac5f50
--- /dev/null
+++ b/db/migrate/20240104172050_add_project_queries.rb
@@ -0,0 +1,13 @@
+class AddProjectQueries < ActiveRecord::Migration[7.0]
+ def change
+ create_table :project_queries do |t|
+ t.string :name, null: false
+ t.references :user, null: false
+ t.json :filters, default: []
+ t.json :columns, default: []
+ t.json :orders, default: []
+
+ t.timestamps
+ end
+ end
+end
diff --git a/modules/costs/app/models/queries/time_entries/time_entry_query.rb b/modules/costs/app/models/queries/time_entries/time_entry_query.rb
index c21b783b806..fffff7101b1 100644
--- a/modules/costs/app/models/queries/time_entries/time_entry_query.rb
+++ b/modules/costs/app/models/queries/time_entries/time_entry_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::TimeEntries::TimeEntryQuery < Queries::BaseQuery
+class Queries::TimeEntries::TimeEntryQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
TimeEntry
end
diff --git a/modules/documents/app/models/queries/documents/document_query.rb b/modules/documents/app/models/queries/documents/document_query.rb
index dc74ff2c0fe..972c64990d3 100644
--- a/modules/documents/app/models/queries/documents/document_query.rb
+++ b/modules/documents/app/models/queries/documents/document_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Documents::DocumentQuery < Queries::BaseQuery
+class Queries::Documents::DocumentQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
Document
end
diff --git a/modules/grids/app/queries/grids/query.rb b/modules/grids/app/queries/grids/query.rb
index d0641b54ca7..8d28af391c4 100644
--- a/modules/grids/app/queries/grids/query.rb
+++ b/modules/grids/app/queries/grids/query.rb
@@ -27,7 +27,10 @@
#++
module Grids
- class Query < ::Queries::BaseQuery
+ class Query
+ include ::Queries::BaseQuery
+ include ::Queries::UnpersistedQuery
+
def self.model
Grids::Grid
end
diff --git a/modules/meeting/app/models/queries/meetings/meeting_query.rb b/modules/meeting/app/models/queries/meetings/meeting_query.rb
index 0f0aeefa92c..1560acc1e62 100644
--- a/modules/meeting/app/models/queries/meetings/meeting_query.rb
+++ b/modules/meeting/app/models/queries/meetings/meeting_query.rb
@@ -27,7 +27,10 @@
#++
module Queries::Meetings
- class MeetingQuery < ::Queries::BaseQuery
+ class MeetingQuery
+ include ::Queries::BaseQuery
+ include ::Queries::UnpersistedQuery
+
def self.model
Meeting
end
diff --git a/modules/storages/app/models/queries/storages/file_links/file_link_query.rb b/modules/storages/app/models/queries/storages/file_links/file_link_query.rb
index ab61781f0f0..41fe78f3986 100644
--- a/modules/storages/app/models/queries/storages/file_links/file_link_query.rb
+++ b/modules/storages/app/models/queries/storages/file_links/file_link_query.rb
@@ -30,7 +30,10 @@
# reference to the embedded model.
# The namespace is programmatically derived from the model name. See app/services/params_to_query_service.rb:130
-class Queries::Storages::FileLinks::FileLinkQuery < Queries::BaseQuery
+class Queries::Storages::FileLinks::FileLinkQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
class << self
# We need to overwrite the model method, as the standard implementation cannot derive the name from nested
# namespaces. See app/models/queries/base_query.rb:31
diff --git a/modules/storages/app/models/queries/storages/project_storages/project_storages_query.rb b/modules/storages/app/models/queries/storages/project_storages/project_storages_query.rb
index 297d95e2a42..a8bd39504e5 100644
--- a/modules/storages/app/models/queries/storages/project_storages/project_storages_query.rb
+++ b/modules/storages/app/models/queries/storages/project_storages/project_storages_query.rb
@@ -26,7 +26,10 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Queries::Storages::ProjectStorages::ProjectStoragesQuery < Queries::BaseQuery
+class Queries::Storages::ProjectStorages::ProjectStoragesQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
def self.model
@model ||= ::Storages::ProjectStorage
end
diff --git a/modules/storages/app/models/queries/storages/storages/storage_query.rb b/modules/storages/app/models/queries/storages/storages/storage_query.rb
index 1fb0577d906..c736a180b9c 100644
--- a/modules/storages/app/models/queries/storages/storages/storage_query.rb
+++ b/modules/storages/app/models/queries/storages/storages/storage_query.rb
@@ -29,7 +29,10 @@
#++
module Queries::Storages::Storages
- class StorageQuery < Queries::BaseQuery
+ class StorageQuery
+ include Queries::BaseQuery
+ include Queries::UnpersistedQuery
+
class << self
def model
@model ||= ::Storages::Storage
diff --git a/spec/factories/queries/project_query_factory.rb b/spec/factories/queries/project_query_factory.rb
new file mode 100644
index 00000000000..db576b7221d
--- /dev/null
+++ b/spec/factories/queries/project_query_factory.rb
@@ -0,0 +1,34 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+FactoryBot.define do
+ factory :project_query, class: 'Queries::Projects::ProjectQuery' do
+ sequence(:name) { |n| "Project query #{n}" }
+ user
+ end
+end
diff --git a/spec/models/queries/projects/project_query_results_spec.rb b/spec/models/queries/projects/project_query_results_spec.rb
new file mode 100644
index 00000000000..ee0a4fc638f
--- /dev/null
+++ b/spec/models/queries/projects/project_query_results_spec.rb
@@ -0,0 +1,156 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+RSpec.describe Queries::Projects::ProjectQuery, 'results' do
+ let(:instance) { described_class.new }
+ let(:base_scope) { Project.order(id: :desc) }
+ let(:current_user) { build_stubbed(:admin) }
+
+ before do
+ login_as(current_user)
+ end
+
+ context 'without a filter' do
+ context 'as an admin' do
+ it 'is the same as getting all projects' do
+ expect(instance.results.to_sql).to eql base_scope.to_sql
+ end
+ end
+
+ context 'as a non admin' do
+ let(:current_user) { build_stubbed(:user) }
+
+ it 'is the same as getting all visible projects' do
+ expect(instance.results.to_sql).to eql base_scope.where(id: Project.visible).to_sql
+ end
+ end
+ end
+
+ context 'with a parent filter' do
+ context 'with a "=" operator' do
+ before do
+ allow(Project)
+ .to receive_message_chain(:visible, :pluck) # rubocop:disable RSpec/MessageChain
+ .with(:id)
+ .and_return([8])
+
+ instance.where('parent_id', '=', ['8'])
+ end
+
+ it 'is the same as handwriting the query' do
+ expected = base_scope
+ .where(projects: { parent_id: ['8'] })
+
+ expect(instance.results.to_sql).to eql expected.to_sql
+ end
+ end
+ end
+
+ context 'with an ancestor filter' do
+ context 'with a "=" operator' do
+ before do
+ instance.where('ancestor', '=', ['8'])
+ end
+
+ describe '#results' do
+ it 'is the same as handwriting the query' do
+ projects_table = Project.arel_table
+ projects_ancestor_table = projects_table.alias(:ancestor_projects)
+
+ condition = projects_table[:lft]
+ .gt(projects_ancestor_table[:lft])
+ .and(projects_table[:rgt].lt(projects_ancestor_table[:rgt]))
+ .and(projects_ancestor_table[:id].in(['8']))
+
+ arel = projects_table
+ .join(projects_ancestor_table)
+ .on(condition)
+
+ expected = base_scope.joins(arel.join_sources)
+
+ expect(instance.results.to_sql).to eql expected.to_sql
+ end
+ end
+ end
+
+ context 'with a "!" operator' do
+ before do
+ instance.where('ancestor', '!', ['8'])
+ end
+
+ describe '#results' do
+ it 'is the same as handwriting the query' do
+ projects_table = Project.arel_table
+ projects_ancestor_table = projects_table.alias(:ancestor_projects)
+
+ condition = projects_table[:lft]
+ .gt(projects_ancestor_table[:lft])
+ .and(projects_table[:rgt].lt(projects_ancestor_table[:rgt]))
+
+ arel = projects_table
+ .outer_join(projects_ancestor_table)
+ .on(condition)
+
+ where_condition = projects_ancestor_table[:id]
+ .not_in(['8'])
+ .or(projects_ancestor_table[:id].eq(nil))
+
+ expected = base_scope
+ .joins(arel.join_sources)
+ .where(where_condition)
+
+ expect(instance.results.to_sql).to eql expected.to_sql
+ end
+ end
+ end
+ end
+
+ context 'with an order by id asc' do
+ describe '#results' do
+ it 'returns all visible projects ordered by id asc' do
+ expect(instance.order(id: :asc).results.to_sql)
+ .to eql base_scope.except(:order).order(id: :asc).to_sql
+ end
+ end
+ end
+
+ context 'with an order by typeahead asc' do
+ before do
+ instance.order(typeahead: :asc)
+ end
+
+ describe '#results' do
+ it 'returns all visible projects ordered by lft asc' do
+ expect(instance.results.to_sql)
+ .to eql base_scope.except(:order).order(lft: :asc, id: :desc).to_sql
+ end
+ end
+ end
+end
diff --git a/spec/models/queries/projects/project_query_spec.rb b/spec/models/queries/projects/project_query_spec.rb
index 2a2bf519c24..929c5240880 100644
--- a/spec/models/queries/projects/project_query_spec.rb
+++ b/spec/models/queries/projects/project_query_spec.rb
@@ -1,6 +1,6 @@
-#-- copyright
+# -- copyright
# OpenProject is an open source project management software.
-# Copyright (C) 2012-2024 the OpenProject GmbH
+# Copyright (C) 2010-2024 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
@@ -24,133 +24,46 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
-#++
+# ++
require 'spec_helper'
RSpec.describe Queries::Projects::ProjectQuery do
let(:instance) { described_class.new }
- let(:base_scope) { Project.all.order(id: :desc) }
- let(:current_user) { build_stubbed(:admin) }
- before do
- login_as(current_user)
- end
-
- context 'without a filter' do
- context 'as an admin' do
- it 'is the same as getting all projects' do
- expect(instance.results.to_sql).to eql base_scope.to_sql
- end
- end
+ shared_let(:current_user) { create(:user) }
- context 'as a non admin' do
- let(:current_user) { build_stubbed(:user) }
-
- it 'is the same as getting all visible projects' do
- expect(instance.results.to_sql).to eql base_scope.where(id: Project.visible).to_sql
- end
+ context 'when persisting' do
+ let(:properties) do
+ {
+ name: 'some name',
+ user: current_user
+ }
end
- end
-
- context 'with a parent filter' do
- context 'with a "=" operator' do
- before do
- allow(Project)
- .to receive_message_chain(:visible, :pluck)
- .with(:id)
- .and_return([8])
- instance.where('parent_id', '=', ['8'])
- end
+ it 'takes a name property' do
+ instance = described_class.create(**properties)
- it 'is the same as handwriting the query' do
- expected = base_scope
- .where(["projects.parent_id IN (?)", ['8']])
-
- expect(instance.results.to_sql).to eql expected.to_sql
- end
+ expect(described_class.find(instance.id).name)
+ .to eql properties[:name]
end
- end
-
- context 'with an ancestor filter' do
- context 'with a "=" operator' do
- before do
- instance.where('ancestor', '=', ['8'])
- end
-
- describe '#results' do
- it 'is the same as handwriting the query' do
- projects_table = Project.arel_table
- projects_ancestor_table = projects_table.alias(:ancestor_projects)
-
- condition = projects_table[:lft]
- .gt(projects_ancestor_table[:lft])
- .and(projects_table[:rgt].lt(projects_ancestor_table[:rgt]))
- .and(projects_ancestor_table[:id].in(['8']))
-
- arel = projects_table
- .join(projects_ancestor_table)
- .on(condition)
- expected = base_scope.joins(arel.join_sources)
+ it 'takes a user property' do
+ instance = described_class.create(**properties)
- expect(instance.results.to_sql).to eql expected.to_sql
- end
- end
+ expect(described_class.find(instance.id).user)
+ .to eql properties[:user]
end
- context 'with a "!" operator' do
- before do
- instance.where('ancestor', '!', ['8'])
- end
+ it 'takes filters' do
+ instance = described_class.new(**properties)
- describe '#results' do
- it 'is the same as handwriting the query' do
- projects_table = Project.arel_table
- projects_ancestor_table = projects_table.alias(:ancestor_projects)
+ instance.where('active', '=', OpenProject::Database::DB_VALUE_TRUE)
- condition = projects_table[:lft]
- .gt(projects_ancestor_table[:lft])
- .and(projects_table[:rgt].lt(projects_ancestor_table[:rgt]))
-
- arel = projects_table
- .outer_join(projects_ancestor_table)
- .on(condition)
-
- where_condition = projects_ancestor_table[:id]
- .not_in(['8'])
- .or(projects_ancestor_table[:id].eq(nil))
-
- expected = base_scope
- .joins(arel.join_sources)
- .where(where_condition)
-
- expect(instance.results.to_sql).to eql expected.to_sql
- end
- end
- end
- end
-
- context 'with an order by id asc' do
- describe '#results' do
- it 'returns all visible projects ordered by id asc' do
- expect(instance.order(id: :asc).results.to_sql)
- .to eql base_scope.except(:order).order(id: :asc).to_sql
- end
- end
- end
-
- context 'with an order by typeahead asc' do
- before do
- instance.order(typeahead: :asc)
- end
+ instance.save!
- describe '#results' do
- it 'returns all visible projects ordered by lft asc' do
- expect(instance.results.to_sql)
- .to eql base_scope.except(:order).order(lft: :asc, id: :desc).to_sql
- end
+ expect(described_class.find(instance.id).filters.map { |f| { field: f.field, operator: f.operator, values: f.values } })
+ .to eql [{ field: :active, operator: '=', values: [OpenProject::Database::DB_VALUE_TRUE] }]
end
end
end
diff --git a/spec/workers/principals/delete_job_integration_spec.rb b/spec/workers/principals/delete_job_integration_spec.rb
index 99fc63c87f7..432b4d6af0a 100644
--- a/spec/workers/principals/delete_job_integration_spec.rb
+++ b/spec/workers/principals/delete_job_integration_spec.rb
@@ -214,7 +214,7 @@ RSpec.describe Principals::DeleteJob, type: :model do
it { expect(Watcher.find_by(id: watch.id)).to be_nil }
end
- shared_examples_for 'token handling' do
+ shared_examples_for 'rss token handling' do
let(:token) do
Token::RSS.new(user: principal, value: 'loremipsum')
end
@@ -254,7 +254,7 @@ RSpec.describe Principals::DeleteJob, type: :model do
it { expect(Query.find_by(id: query.id)).to be_nil }
end
- shared_examples_for 'token handling' do
+ shared_examples_for 'backup token handling' do
let!(:backup_token) do
create(:backup_token, user: principal)
end
@@ -311,6 +311,16 @@ RSpec.describe Principals::DeleteJob, type: :model do
end
end
+ shared_examples_for 'project query handling' do
+ let!(:query) { create(:project_query, user: principal) }
+
+ it 'removes the query' do
+ job
+
+ expect(Queries::Projects::ProjectQuery.find_by(id: query.id)).to be_nil
+ end
+ end
+
shared_examples_for 'public cost_query handling' do
let!(:query) { create(:public_cost_query, user: principal) }
@@ -455,13 +465,15 @@ RSpec.describe Principals::DeleteJob, type: :model do
it_behaves_like 'hourly_rate handling'
it_behaves_like 'member handling'
it_behaves_like 'watcher handling'
- it_behaves_like 'token handling'
+ it_behaves_like 'rss token handling'
+ it_behaves_like 'backup token handling'
it_behaves_like 'notification handling'
it_behaves_like 'private query handling'
it_behaves_like 'issue category handling'
it_behaves_like 'private cost_query handling'
it_behaves_like 'public cost_query handling'
it_behaves_like 'cost_query handling'
+ it_behaves_like 'project query handling'
it_behaves_like 'mention rewriting'
end