2025-03-10 09:53:13 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2011-05-29 13:11:52 -07:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2011-05-29 13:11:52 -07:00
|
|
|
# This program is free software; you can redistribute it and/or
|
2013-06-05 16:27:56 +02:00
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2013-09-16 17:59:31 +02:00
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2011-05-29 13:11:52 -07:00
|
|
|
#++
|
|
|
|
|
|
2020-04-09 11:54:26 +02:00
|
|
|
class Query < ApplicationRecord
|
2018-04-05 16:51:14 +02:00
|
|
|
include Timelines
|
2022-10-30 10:23:05 +01:00
|
|
|
include Timestamps
|
2018-09-03 13:38:08 +02:00
|
|
|
include Highlighting
|
2019-02-18 15:59:54 +01:00
|
|
|
include ManualSorting
|
2021-09-13 09:39:58 +02:00
|
|
|
include Queries::Filters::AvailableFilters
|
2013-09-23 17:25:27 +02:00
|
|
|
|
2006-12-16 13:37:32 +00:00
|
|
|
belongs_to :project
|
|
|
|
|
belongs_to :user
|
2021-12-01 10:32:43 +01:00
|
|
|
has_many :views,
|
|
|
|
|
dependent: :destroy
|
2023-05-30 18:42:33 +02:00
|
|
|
has_many :ical_token_query_assignments
|
2023-05-30 18:07:02 +02:00
|
|
|
has_many :ical_tokens,
|
2023-05-30 15:18:46 +02:00
|
|
|
through: :ical_token_query_assignments,
|
|
|
|
|
class_name: "Token::ICal"
|
2025-03-26 16:45:02 +01:00
|
|
|
has_many :export_settings, dependent: :destroy
|
2023-05-30 18:07:02 +02:00
|
|
|
# no `dependent: :destroy` as the ical_tokens are destroyed in the following before_destroy callback
|
|
|
|
|
# dependent: :destroy is not possible as this would only delete the ical_token_query_assignments
|
|
|
|
|
before_destroy :destroy_ical_tokens
|
2021-12-01 10:32:43 +01:00
|
|
|
|
2023-12-18 15:39:30 +02:00
|
|
|
serialize :filters, coder: Queries::WorkPackages::FilterSerializer
|
|
|
|
|
serialize :column_names, type: Array
|
|
|
|
|
serialize :sort_criteria, type: Array
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2022-04-04 09:05:46 +02:00
|
|
|
validates :include_subprojects,
|
|
|
|
|
inclusion: [true, false]
|
|
|
|
|
|
2013-11-20 14:58:28 +01:00
|
|
|
validate :validate_work_package_filters
|
2017-03-14 12:57:26 +00:00
|
|
|
validate :validate_columns
|
|
|
|
|
validate :validate_sort_criteria
|
|
|
|
|
validate :validate_group_by
|
2017-04-07 14:02:17 +02:00
|
|
|
validate :validate_show_hierarchies
|
2023-05-10 13:28:19 +03:00
|
|
|
validate :validate_timestamps
|
2012-08-28 17:53:39 +02:00
|
|
|
|
2021-12-01 10:32:43 +01:00
|
|
|
include Scopes::Scoped
|
2026-05-29 13:38:58 +02:00
|
|
|
|
2021-12-10 15:35:54 +01:00
|
|
|
scopes :visible,
|
|
|
|
|
:having_views
|
2015-08-31 14:52:44 +02:00
|
|
|
|
2017-04-19 16:16:14 +02:00
|
|
|
scope(:global, -> { where(project_id: nil) })
|
2015-08-31 14:52:44 +02:00
|
|
|
|
2025-03-11 16:15:18 +01:00
|
|
|
def self.new_default(attributes = nil)
|
2017-02-10 08:39:51 +01:00
|
|
|
new(attributes).tap do |query|
|
|
|
|
|
query.add_default_filter
|
|
|
|
|
query.set_default_sort
|
2017-04-07 10:30:07 +02:00
|
|
|
query.show_hierarchies = true
|
2022-03-23 09:49:41 +01:00
|
|
|
query.include_subprojects = Setting.display_subprojects_work_packages?
|
2017-02-10 08:39:51 +01:00
|
|
|
end
|
2013-11-21 17:08:47 +01:00
|
|
|
end
|
|
|
|
|
|
2020-08-24 10:59:01 +02:00
|
|
|
##
|
|
|
|
|
# Ensure the filters receive
|
|
|
|
|
# the query context as this appears to be lost
|
|
|
|
|
# whenever the field is reloaded from the serialized value
|
|
|
|
|
def filters
|
|
|
|
|
super.tap do |filters|
|
|
|
|
|
filters.each do |filter|
|
|
|
|
|
filter.context = self
|
|
|
|
|
end
|
2016-10-27 15:47:38 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-02-10 08:39:51 +01:00
|
|
|
def set_default_sort
|
|
|
|
|
return if sort_criteria.any?
|
|
|
|
|
|
2022-10-26 10:22:14 +02:00
|
|
|
self.sort_criteria = [%w[id asc]]
|
2017-02-10 08:39:51 +01:00
|
|
|
end
|
|
|
|
|
|
2017-04-19 16:17:16 +02:00
|
|
|
def context
|
|
|
|
|
self
|
|
|
|
|
end
|
2016-10-27 15:47:38 +02:00
|
|
|
|
2017-11-29 10:32:57 +01:00
|
|
|
def to_s
|
|
|
|
|
name
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-21 17:08:47 +01:00
|
|
|
def add_default_filter
|
2022-01-14 16:58:33 +01:00
|
|
|
return if filters.present?
|
2016-10-27 15:47:38 +02:00
|
|
|
|
|
|
|
|
add_filter("status_id", "o", [""])
|
2007-05-08 12:46:15 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-11-20 14:58:28 +01:00
|
|
|
def validate_work_package_filters
|
2014-11-03 21:35:22 +01:00
|
|
|
filters.each do |filter|
|
2013-12-05 17:14:27 +01:00
|
|
|
unless filter.valid?
|
2017-04-07 15:17:09 +02:00
|
|
|
errors.add :base, filter.error_messages
|
2013-12-05 17:14:27 +01:00
|
|
|
end
|
2012-08-28 17:53:39 +02:00
|
|
|
end
|
2006-12-16 13:37:32 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2017-03-14 12:57:26 +00:00
|
|
|
def validate_columns
|
2025-03-05 09:44:50 +01:00
|
|
|
available_names = displayable_columns.map { |c| c.name.to_sym }
|
2017-03-14 12:57:26 +00:00
|
|
|
|
2017-05-30 11:41:45 +02:00
|
|
|
(column_names - available_names).each do |name|
|
|
|
|
|
errors.add :column_names,
|
2020-08-17 08:46:42 +02:00
|
|
|
:invalid,
|
|
|
|
|
value: name
|
2017-03-14 12:57:26 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_sort_criteria
|
|
|
|
|
available_criteria = sortable_columns.map(&:name).map(&:to_s)
|
|
|
|
|
|
|
|
|
|
sort_criteria.each do |name, _dir|
|
|
|
|
|
unless available_criteria.include? name.to_s
|
2020-08-17 08:46:42 +02:00
|
|
|
errors.add :sort_criteria, :invalid, value: name
|
2017-03-14 12:57:26 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_group_by
|
2025-03-05 09:44:50 +01:00
|
|
|
unless group_by.blank? || groupable_columns.map { |c| c.name.to_s }.include?(group_by.to_s)
|
2020-08-17 08:46:42 +02:00
|
|
|
errors.add :group_by, :invalid, value: group_by
|
2017-03-14 12:57:26 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-07 14:02:17 +02:00
|
|
|
def validate_show_hierarchies
|
|
|
|
|
if show_hierarchies && group_by.present?
|
|
|
|
|
errors.add :show_hierarchies, :group_by_hierarchies_exclusive, group_by:
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-09 15:13:37 +03:00
|
|
|
def validate_timestamps
|
2023-05-10 13:28:19 +03:00
|
|
|
forbidden_timestamps = timestamps - allowed_timestamps
|
2023-05-09 15:13:37 +03:00
|
|
|
if forbidden_timestamps.any?
|
|
|
|
|
errors.add :timestamps, :forbidden, values: forbidden_timestamps.join(", ")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-12-10 15:35:54 +01:00
|
|
|
def hidden
|
|
|
|
|
views.empty?
|
|
|
|
|
end
|
|
|
|
|
|
2017-05-24 08:03:14 +02:00
|
|
|
# Try to fix an invalid query
|
|
|
|
|
#
|
|
|
|
|
# Fixes:
|
|
|
|
|
# * filters:
|
|
|
|
|
# Reduces the filter's values to those that are valid.
|
|
|
|
|
# If the filter remains invalid, it is removed.
|
|
|
|
|
# * group_by:
|
|
|
|
|
# Removes the group by if it is invalid
|
|
|
|
|
# * sort_criteria
|
|
|
|
|
# Removes all invalid criteria
|
2017-05-30 11:41:45 +02:00
|
|
|
# * columns
|
|
|
|
|
# Removes all invalid columns
|
2017-05-24 08:03:14 +02:00
|
|
|
#
|
|
|
|
|
# If the query has been valid or if the error
|
|
|
|
|
# is not one of the addressed, the query is unchanged.
|
|
|
|
|
def valid_subset!
|
|
|
|
|
valid_filter_subset!
|
|
|
|
|
valid_group_by_subset!
|
|
|
|
|
valid_sort_criteria_subset!
|
2017-05-30 11:41:45 +02:00
|
|
|
valid_column_subset!
|
2023-05-10 13:28:19 +03:00
|
|
|
valid_timestamps_subset!
|
2017-05-24 08:03:14 +02:00
|
|
|
end
|
|
|
|
|
|
2006-12-16 13:37:32 +00:00
|
|
|
def add_filter(field, operator, values)
|
2016-10-27 15:47:38 +02:00
|
|
|
filter = filter_for(field)
|
2013-11-20 15:16:26 +01:00
|
|
|
|
2016-10-27 15:47:38 +02:00
|
|
|
filter.operator = operator
|
|
|
|
|
filter.values = values
|
|
|
|
|
|
|
|
|
|
filters << filter
|
2006-12-16 13:37:32 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-11-19 17:12:31 +01:00
|
|
|
def filter_for(field)
|
2016-10-27 15:47:38 +02:00
|
|
|
filter = (filters || []).detect { |f| f.field.to_s == field.to_s } || super
|
2013-11-19 17:12:31 +01:00
|
|
|
|
2017-04-19 16:17:16 +02:00
|
|
|
filter.context = self
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2016-10-27 15:47:38 +02:00
|
|
|
filter
|
2007-04-17 10:53:20 +00:00
|
|
|
end
|
2007-10-01 08:44:17 +00:00
|
|
|
|
2023-05-26 13:57:28 +02:00
|
|
|
# Removes the filter with the given name
|
|
|
|
|
# from the query without persisting the change.
|
|
|
|
|
#
|
|
|
|
|
# @param [String] name the filter to remove
|
|
|
|
|
def remove_filter(name)
|
|
|
|
|
filters.delete_if { |f| f.field.to_s == name.to_s }
|
|
|
|
|
end
|
|
|
|
|
|
2026-05-28 17:21:10 +02:00
|
|
|
# Mirrors `Queries::BaseQuery#find_active_filter` so that consumers built
|
2026-06-01 14:29:06 +02:00
|
|
|
# on top of the modern query API (e.g. `Filters::FilterFormComponent`) can ask any
|
2026-05-28 17:21:10 +02:00
|
|
|
# query — including this legacy work-package one — for its active filter
|
|
|
|
|
# by name. Signature kept identical to BaseQuery's (symbol arg in, filter
|
|
|
|
|
# or nil out).
|
|
|
|
|
def find_active_filter(name)
|
|
|
|
|
filters.detect { |f| f.name == name }
|
|
|
|
|
end
|
|
|
|
|
|
2026-05-29 13:38:58 +02:00
|
|
|
# The manual-sort filter is added programmatically when the user drags
|
|
|
|
|
# work packages to reorder them — it has no operator/value UI of its own
|
|
|
|
|
# (type `:empty_value`), so it doesn't belong in the picker that
|
2026-06-01 14:29:06 +02:00
|
|
|
# `Filters::FilterFormComponent` builds. Mirrors how
|
2026-05-29 13:38:58 +02:00
|
|
|
# `Queries::Filters::AvailableFilters#available_advanced_filters` already
|
|
|
|
|
# excludes the inline `name_and_identifier` quick-filter on projects.
|
|
|
|
|
def available_advanced_filters
|
|
|
|
|
super.grep_v(::Queries::WorkPackages::Filter::ManualSortFilter)
|
|
|
|
|
end
|
|
|
|
|
|
2014-09-08 16:36:31 +01:00
|
|
|
def normalized_name
|
|
|
|
|
name.parameterize.underscore
|
|
|
|
|
end
|
|
|
|
|
|
2007-10-01 08:44:17 +00:00
|
|
|
def available_columns
|
2017-04-03 13:26:49 +02:00
|
|
|
if @available_columns &&
|
2025-03-12 17:43:29 +01:00
|
|
|
(@available_columns_project == (project&.cache_key_with_version || 0))
|
2017-04-03 13:26:49 +02:00
|
|
|
return @available_columns
|
|
|
|
|
end
|
|
|
|
|
|
2025-03-12 17:43:29 +01:00
|
|
|
@available_columns_project = project&.cache_key_with_version || 0
|
2025-03-12 14:03:58 +01:00
|
|
|
@available_columns = ::Query.available_columns(project)
|
2010-04-20 15:42:52 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2025-03-12 14:03:58 +01:00
|
|
|
def self.available_columns(project = nil)
|
2026-04-08 09:17:01 +02:00
|
|
|
RequestStore.fetch(:"available_columns_#{project&.id}") do
|
|
|
|
|
Queries::Register
|
|
|
|
|
.selects[self]
|
|
|
|
|
.map { |col| col.instances(project) }
|
|
|
|
|
.flatten
|
|
|
|
|
end
|
2017-01-26 16:52:39 +01:00
|
|
|
end
|
|
|
|
|
|
2022-10-26 10:22:14 +02:00
|
|
|
def self.displayable_columns
|
|
|
|
|
available_columns.select(&:displayable?)
|
|
|
|
|
end
|
|
|
|
|
|
2017-01-26 17:08:23 +01:00
|
|
|
def self.groupable_columns
|
2017-06-06 19:36:30 +02:00
|
|
|
available_columns.select(&:groupable)
|
2017-01-26 17:08:23 +01:00
|
|
|
end
|
|
|
|
|
|
2017-01-30 09:57:25 +01:00
|
|
|
def self.sortable_columns
|
2022-10-26 10:22:14 +02:00
|
|
|
available_columns.select(&:sortable)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def displayable_columns
|
|
|
|
|
available_columns.select(&:displayable?)
|
2010-04-20 15:42:52 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-04-26 13:09:14 +00:00
|
|
|
# Returns an array of columns that can be used to group the results
|
|
|
|
|
def groupable_columns
|
2014-11-03 21:35:22 +01:00
|
|
|
available_columns.select(&:groupable)
|
2009-04-26 13:09:14 +00:00
|
|
|
end
|
2010-03-19 15:42:03 +00:00
|
|
|
|
2017-02-01 16:45:36 +01:00
|
|
|
# Returns an array of columns that can be used to sort the results
|
2010-03-19 15:42:03 +00:00
|
|
|
def sortable_columns
|
2022-10-26 10:22:14 +02:00
|
|
|
available_columns.select(&:sortable)
|
2017-02-01 16:45:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Returns a Hash of sql columns for sorting by column
|
|
|
|
|
def sortable_key_by_column_name
|
2019-02-18 15:59:54 +01:00
|
|
|
column_sortability = sortable_columns.inject({}) do |h, column|
|
2016-10-27 15:47:38 +02:00
|
|
|
h[column.name.to_s] = column.sortable
|
|
|
|
|
h
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
{ "id" => "#{WorkPackage.table_name}.id" }
|
|
|
|
|
.merge(column_sortability)
|
2010-03-19 15:42:03 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2020-08-24 07:22:43 +02:00
|
|
|
def summed_up_columns
|
|
|
|
|
available_columns.select(&:summable?)
|
|
|
|
|
end
|
|
|
|
|
|
2007-10-01 08:44:17 +00:00
|
|
|
def columns
|
2017-06-06 19:36:30 +02:00
|
|
|
column_list = if has_default_columns?
|
2017-09-15 14:32:08 +02:00
|
|
|
column_list = Setting.work_package_list_default_columns.dup.map(&:to_sym)
|
2017-06-06 19:36:30 +02:00
|
|
|
# Adds the project column by default for cross-project lists
|
2023-05-09 15:13:37 +03:00
|
|
|
column_list += [:project] if project.nil? && column_list.exclude?(:project)
|
2017-06-06 19:36:30 +02:00
|
|
|
column_list
|
|
|
|
|
else
|
|
|
|
|
column_names
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# preserve the order
|
2023-09-06 11:06:36 +02:00
|
|
|
column_list.filter_map { |name| displayable_columns.find { |col| col.name == name.to_sym } }
|
2007-10-01 08:44:17 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2007-10-01 08:44:17 +00:00
|
|
|
def column_names=(names)
|
2017-04-24 16:30:25 +02:00
|
|
|
col_names = Array(names)
|
2022-01-14 16:58:33 +01:00
|
|
|
.compact_blank
|
2017-04-24 16:30:25 +02:00
|
|
|
.map(&:to_sym)
|
|
|
|
|
|
|
|
|
|
write_attribute(:column_names, col_names)
|
2007-10-01 08:44:17 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2007-10-01 08:44:17 +00:00
|
|
|
def has_column?(column)
|
2023-05-09 15:13:37 +03:00
|
|
|
column_names&.include?(column.name)
|
2007-10-01 08:44:17 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2007-10-01 10:44:45 +00:00
|
|
|
def has_default_columns?
|
2016-10-25 14:18:57 +02:00
|
|
|
column_names.empty?
|
2007-10-01 10:44:45 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-03-12 18:06:54 +00:00
|
|
|
def sort_criteria=(arg)
|
|
|
|
|
if arg.is_a?(Hash)
|
2014-11-04 11:07:27 +01:00
|
|
|
arg = arg.keys.sort.map { |k| arg[k] }
|
2009-03-12 18:06:54 +00:00
|
|
|
end
|
2017-04-19 16:16:14 +02:00
|
|
|
c = arg.reject { |k, _o| k.to_s.blank? }.slice(0, 3).map { |k, o| [k.to_s, o == "desc" ? o : "asc"] }
|
2009-03-12 18:06:54 +00:00
|
|
|
write_attribute(:sort_criteria, c)
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-03-12 18:06:54 +00:00
|
|
|
def sort_criteria
|
2019-03-13 13:24:10 +01:00
|
|
|
(read_attribute(:sort_criteria) || []).tap do |criteria|
|
|
|
|
|
criteria.map! do |attr, direction|
|
|
|
|
|
attr = "id" if attr == "parent"
|
|
|
|
|
[attr, direction]
|
|
|
|
|
end
|
|
|
|
|
end
|
2009-03-12 18:06:54 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-03-12 18:06:54 +00:00
|
|
|
def sort_criteria_key(arg)
|
|
|
|
|
sort_criteria && sort_criteria[arg] && sort_criteria[arg].first
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-03-12 18:06:54 +00:00
|
|
|
def sort_criteria_order(arg)
|
|
|
|
|
sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
|
|
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2017-04-04 14:22:05 +02:00
|
|
|
def sort_criteria_columns
|
2017-05-24 21:11:54 +02:00
|
|
|
sort_criteria
|
2025-03-11 10:18:26 +01:00
|
|
|
.filter_map do |attribute, direction|
|
2017-05-24 21:11:54 +02:00
|
|
|
attribute = attribute.to_sym
|
2025-03-11 10:18:26 +01:00
|
|
|
col = sort_criteria_column(attribute)
|
2017-05-24 21:11:54 +02:00
|
|
|
|
2025-03-11 10:18:26 +01:00
|
|
|
[col, direction] if col
|
2017-05-24 21:11:54 +02:00
|
|
|
end
|
2017-04-04 14:22:05 +02:00
|
|
|
end
|
|
|
|
|
|
2020-02-04 08:41:54 +01:00
|
|
|
def sort_criteria_column(attribute)
|
|
|
|
|
sortable_columns
|
|
|
|
|
.detect { |candidate| candidate.name == attribute }
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-23 21:45:38 +01:00
|
|
|
def ordered?
|
2017-01-24 10:49:54 +01:00
|
|
|
sort_criteria.any?
|
|
|
|
|
end
|
|
|
|
|
|
2009-04-26 13:09:14 +00:00
|
|
|
# Returns true if the query is a grouped query
|
|
|
|
|
def grouped?
|
2010-12-21 21:46:54 +00:00
|
|
|
!group_by_column.nil?
|
2009-04-26 13:09:14 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-08-29 12:05:36 +02:00
|
|
|
def display_sums?
|
2020-08-24 07:22:43 +02:00
|
|
|
display_sums
|
2013-08-29 12:05:36 +02:00
|
|
|
end
|
|
|
|
|
|
2009-04-26 13:09:14 +00:00
|
|
|
def group_by_column
|
2014-11-03 21:35:22 +01:00
|
|
|
groupable_columns.detect { |c| c.groupable && c.name.to_s == group_by }
|
2009-04-26 13:09:14 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-11-16 18:07:30 +00:00
|
|
|
def group_by_statement
|
2024-10-08 19:44:51 +02:00
|
|
|
group_by_column&.groupable
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-14 18:32:28 +02:00
|
|
|
def group_by_select
|
|
|
|
|
group_by_column&.groupable_select || group_by_statement
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-08 19:44:51 +02:00
|
|
|
def group_by_join_statement
|
|
|
|
|
group_by_column&.groupable_join
|
2009-11-16 18:07:30 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2008-09-11 17:03:26 +00:00
|
|
|
def statement
|
2017-04-20 17:10:37 +02:00
|
|
|
return "1=0" unless valid?
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2017-04-20 17:10:37 +02:00
|
|
|
statement_filters
|
|
|
|
|
.map { |filter| "(#{filter.where})" }
|
2022-01-14 16:58:33 +01:00
|
|
|
.compact_blank
|
2017-04-20 17:10:37 +02:00
|
|
|
.join(" AND ")
|
2006-12-16 13:37:32 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2013-08-29 12:05:36 +02:00
|
|
|
# Returns the result set
|
2019-09-20 08:34:51 +02:00
|
|
|
def results
|
|
|
|
|
Results.new(self)
|
2009-11-28 10:08:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Returns the journals
|
|
|
|
|
# Valid options are :order, :offset, :limit
|
2025-05-21 13:22:23 +03:00
|
|
|
# NOTE: Internal comments are NEVER included "FOR NOW". This is a stop gap measure before we
|
|
|
|
|
# evaluate whether we want to maintain the journals atom export or not.
|
|
|
|
|
def work_package_journals(options = {}) # rubocop:disable Metrics/AbcSize
|
2016-08-24 14:10:11 +02:00
|
|
|
Journal.includes(:user)
|
2025-05-21 13:22:23 +03:00
|
|
|
.where(journable_type: WorkPackage.to_s, restricted: false)
|
2016-08-24 14:10:11 +02:00
|
|
|
.joins("INNER JOIN work_packages ON work_packages.id = journals.journable_id")
|
|
|
|
|
.joins("INNER JOIN projects ON work_packages.project_id = projects.id")
|
|
|
|
|
.joins("INNER JOIN users AS authors ON work_packages.author_id = authors.id")
|
|
|
|
|
.joins("INNER JOIN types ON work_packages.type_id = types.id")
|
|
|
|
|
.joins("INNER JOIN statuses ON work_packages.status_id = statuses.id")
|
|
|
|
|
.order(options[:order])
|
|
|
|
|
.limit(options[:limit])
|
|
|
|
|
.offset(options[:offset])
|
|
|
|
|
.references(:users)
|
|
|
|
|
.merge(WorkPackage.visible)
|
2009-11-28 10:29:48 +00:00
|
|
|
rescue ::ActiveRecord::StatementInvalid => e
|
2012-07-26 11:00:59 +02:00
|
|
|
raise ::Query::StatementInvalid.new(e.message)
|
2009-11-28 10:08:29 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2017-04-20 17:10:37 +02:00
|
|
|
def project_limiting_filter
|
2022-01-14 17:31:47 +01:00
|
|
|
return if project_filter_set?
|
2020-09-23 15:46:20 +02:00
|
|
|
|
2017-12-07 10:00:13 +01:00
|
|
|
subproject_filter = Queries::WorkPackages::Filter::SubprojectFilter.create!
|
2017-04-20 17:10:37 +02:00
|
|
|
subproject_filter.context = self
|
|
|
|
|
|
2022-03-28 09:45:55 +02:00
|
|
|
subproject_filter.operator = if include_subprojects?
|
2024-03-21 11:31:17 +01:00
|
|
|
"*"
|
2017-04-20 17:10:37 +02:00
|
|
|
else
|
|
|
|
|
"!*"
|
|
|
|
|
end
|
|
|
|
|
subproject_filter
|
|
|
|
|
end
|
|
|
|
|
|
2025-03-31 10:33:29 +02:00
|
|
|
def export_settings_for(format)
|
2025-04-02 13:05:26 +02:00
|
|
|
export_settings.where(format:).first_or_initialize
|
2025-03-31 10:33:29 +02:00
|
|
|
end
|
|
|
|
|
|
2008-06-22 15:35:11 +00:00
|
|
|
private
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2020-09-23 15:46:20 +02:00
|
|
|
##
|
|
|
|
|
# Determine whether there are explicit filters
|
2022-01-14 17:31:47 +01:00
|
|
|
# on whether work packages from
|
|
|
|
|
# * subprojects
|
|
|
|
|
# * other projects
|
|
|
|
|
# are used.
|
|
|
|
|
def project_filter_set?
|
2020-09-23 15:46:20 +02:00
|
|
|
filters.any? do |filter|
|
2022-01-14 17:31:47 +01:00
|
|
|
filter.is_a?(::Queries::WorkPackages::Filter::SubprojectFilter) ||
|
|
|
|
|
filter.is_a?(::Queries::WorkPackages::Filter::ProjectFilter)
|
2020-09-23 15:46:20 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-15 12:13:53 +01:00
|
|
|
def for_all?
|
|
|
|
|
@for_all ||= project.nil?
|
|
|
|
|
end
|
2017-04-03 13:26:49 +02:00
|
|
|
|
2017-04-20 17:10:37 +02:00
|
|
|
def statement_filters
|
2020-09-23 15:46:20 +02:00
|
|
|
if project
|
|
|
|
|
filters + [project_limiting_filter].compact
|
2017-04-20 17:10:37 +02:00
|
|
|
else
|
|
|
|
|
filters
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-05-24 08:03:14 +02:00
|
|
|
|
2023-05-10 13:28:19 +03:00
|
|
|
def allowed_timestamps
|
2023-07-31 23:38:41 +03:00
|
|
|
Timestamp.allowed(timestamps)
|
2023-05-10 13:28:19 +03:00
|
|
|
end
|
|
|
|
|
|
2017-05-24 08:03:14 +02:00
|
|
|
def valid_filter_subset!
|
2017-05-30 11:41:45 +02:00
|
|
|
filters.each(&:valid_values!).select! do |filter|
|
|
|
|
|
filter.available? && filter.valid?
|
2017-05-24 08:03:14 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def valid_group_by_subset!
|
|
|
|
|
unless groupable_columns.map(&:name).map(&:to_s).include?(group_by.to_s)
|
|
|
|
|
self.group_by = nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def valid_sort_criteria_subset!
|
|
|
|
|
available_criteria = sortable_columns.map(&:name).map(&:to_s)
|
|
|
|
|
|
2017-05-30 11:41:45 +02:00
|
|
|
sort_criteria.select! do |criteria|
|
|
|
|
|
available_criteria.include? criteria.first.to_s
|
2017-05-24 08:03:14 +02:00
|
|
|
end
|
|
|
|
|
end
|
2017-05-30 11:41:45 +02:00
|
|
|
|
|
|
|
|
def valid_column_subset!
|
2022-10-26 10:22:14 +02:00
|
|
|
available_names = displayable_columns.map(&:name).map(&:to_sym)
|
2017-05-30 11:41:45 +02:00
|
|
|
|
|
|
|
|
self.column_names &= available_names
|
|
|
|
|
end
|
2023-05-10 13:28:19 +03:00
|
|
|
|
|
|
|
|
def valid_timestamps_subset!
|
|
|
|
|
self.timestamps &= allowed_timestamps
|
|
|
|
|
end
|
2023-05-30 15:18:46 +02:00
|
|
|
|
|
|
|
|
# dependent::destroy does not work for has_many :through associations
|
2023-05-30 18:07:02 +02:00
|
|
|
# only the ical_token_query_assignments would be destroyed
|
2023-05-30 15:18:46 +02:00
|
|
|
def destroy_ical_tokens
|
2023-05-30 18:07:02 +02:00
|
|
|
ical_tokens.each(&:destroy)
|
2023-05-30 15:18:46 +02:00
|
|
|
end
|
2006-12-16 13:37:32 +00:00
|
|
|
end
|