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

[59069] Implemented filtering for hierarchies

Bruno Pagnob815fcc11097
diff --git a/app/models/queries/filters.rb b/app/models/queries/filters.rb
index 293f2469f20..503c69862db 100644
--- a/app/models/queries/filters.rb
+++ b/app/models/queries/filters.rb
@@ -40,7 +40,8 @@ module Queries::Filters
search: Queries::Filters::Strategies::Search,
float: Queries::Filters::Strategies::Float,
inexistent: Queries::Filters::Strategies::Inexistent,
- empty_value: Queries::Filters::Strategies::EmptyValue
+ empty_value: Queries::Filters::Strategies::EmptyValue,
+ hierarchy: Queries::Filters::Strategies::Hierarchy
}.freeze
##
diff --git a/app/models/queries/filters/shared/custom_field_filter.rb b/app/models/queries/filters/shared/custom_field_filter.rb
index 1e3cea7c7d5..45a9c3831e5 100644
--- a/app/models/queries/filters/shared/custom_field_filter.rb
+++ b/app/models/queries/filters/shared/custom_field_filter.rb
@@ -70,29 +70,12 @@ module Queries::Filters::Shared::CustomFieldFilter
##
# Create a filter instance for the given custom field
def from_custom_field!(custom_field:, **)
- constant_name = subfilter_module(custom_field)
- clazz = "::Queries::Filters::Shared::CustomFields::#{constant_name}".constantize
- clazz.create!(custom_field:, custom_field_context:, **)
+ subfilter_class(custom_field).create!(custom_field:, custom_field_context:, **)
rescue NameError => e
Rails.logger.error "Failed to constantize custom field filter for #{name}. #{e}"
raise ::Queries::Filters::InvalidError
end
- ##
- # Get the subfilter class name for the given custom field
- def subfilter_module(custom_field)
- case custom_field.field_format
- when "user"
- :User
- when "list", "version"
- :ListOptional
- when "bool"
- :Bool
- else
- :Base
- end
- end
-
def all_custom_fields
key = ["Queries::Filters::Shared::CustomFieldFilter",
custom_field_context.custom_field_class,
@@ -102,5 +85,22 @@ module Queries::Filters::Shared::CustomFieldFilter
custom_field_context.custom_field_class.all.to_a
end
end
+
+ private
+
+ def subfilter_class(custom_field)
+ case custom_field.field_format
+ when "user"
+ ::Queries::Filters::Shared::CustomFields::User
+ when "list", "version"
+ ::Queries::Filters::Shared::CustomFields::ListOptional
+ when "hierarchy"
+ ::Queries::Filters::Shared::CustomFields::Hierarchy
+ when "bool"
+ ::Queries::Filters::Shared::CustomFields::Bool
+ else
+ ::Queries::Filters::Shared::CustomFields::Base
+ end
+ end
end
end
diff --git a/app/models/queries/filters/shared/custom_fields/base.rb b/app/models/queries/filters/shared/custom_fields/base.rb
index 1a6bc559cf7..07cf1b0cb30 100644
--- a/app/models/queries/filters/shared/custom_fields/base.rb
+++ b/app/models/queries/filters/shared/custom_fields/base.rb
@@ -89,6 +89,8 @@ module Queries::Filters::Shared
:text
when "date"
:date
+ when "hierarchy"
+ :hierarchy
else
:string
end
diff --git a/app/models/queries/filters/shared/custom_fields/hierarchy.rb b/app/models/queries/filters/shared/custom_fields/hierarchy.rb
new file mode 100644
index 00000000000..81538bc4bdb
--- /dev/null
+++ b/app/models/queries/filters/shared/custom_fields/hierarchy.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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
+ module Filters
+ module Shared
+ module CustomFields
+ class Hierarchy < Base
+ def ar_object_filter?
+ true
+ end
+
+ def value_objects
+ CustomField::Hierarchy::Item
+ .where(id: @values)
+ .map { |item| HierarchyItemFilterAdapter.new(item:) }
+ end
+ end
+
+ class HierarchyItemFilterAdapter
+ attr_reader :name
+
+ delegate :id, to: :item
+
+ def initialize(item:)
+ @item = item
+ @name = item.label
+ end
+
+ private
+
+ attr_accessor :item
+ end
+ end
+ end
+ end
+end
diff --git a/app/models/queries/filters/strategies/hierarchy.rb b/app/models/queries/filters/strategies/hierarchy.rb
new file mode 100644
index 00000000000..fc65be5c736
--- /dev/null
+++ b/app/models/queries/filters/strategies/hierarchy.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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
+ module Filters
+ module Strategies
+ class Hierarchy < BaseStrategy
+ self.supported_operators = %w[= !]
+ self.default_operator = "="
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/queries/query_representer.rb b/lib/api/v3/queries/query_representer.rb
index 87a70888bf7..6eeca1ae994 100644
--- a/lib/api/v3/queries/query_representer.rb
+++ b/lib/api/v3/queries/query_representer.rb
@@ -344,8 +344,7 @@ module API
def filters
represented.filters.map do |filter|
- ::API::V3::Queries::Filters::QueryFilterInstanceRepresenter
- .new(filter)
+ ::API::V3::Queries::Filters::QueryFilterInstanceRepresenter.new(filter)
end
end
diff --git a/lib/api/v3/queries/schemas/custom_option_filter_dependency_representer.rb b/lib/api/v3/queries/schemas/custom_option_filter_dependency_representer.rb
index b840bcd15e0..b733effa24f 100644
--- a/lib/api/v3/queries/schemas/custom_option_filter_dependency_representer.rb
+++ b/lib/api/v3/queries/schemas/custom_option_filter_dependency_representer.rb
@@ -30,8 +30,7 @@ module API
module V3
module Queries
module Schemas
- class CustomOptionFilterDependencyRepresenter <
- FilterDependencyRepresenter
+ class CustomOptionFilterDependencyRepresenter < FilterDependencyRepresenter
schema_with_allowed_collection :values,
type: ->(*) { type },
writable: true,
diff --git a/lib/api/v3/queries/schemas/filter_dependency_representer_factory.rb b/lib/api/v3/queries/schemas/filter_dependency_representer_factory.rb
index 1b90fccb394..0d4651f2c48 100644
--- a/lib/api/v3/queries/schemas/filter_dependency_representer_factory.rb
+++ b/lib/api/v3/queries/schemas/filter_dependency_representer_factory.rb
@@ -96,7 +96,7 @@ module API
"API::V3::Queries::Schemas::CustomOptionFilterDependencyRepresenter"
when "bool"
"API::V3::Queries::Schemas::BooleanFilterDependencyRepresenter"
- when "user", "version", "float"
+ when "user", "version", "float", "hierarchy"
"API::V3::Queries::Schemas::#{format.camelize}FilterDependencyRepresenter"
when "string", "link"
"API::V3::Queries::Schemas::TextFilterDependencyRepresenter"
diff --git a/lib/api/v3/queries/schemas/hierarchy_filter_dependency_representer.rb b/lib/api/v3/queries/schemas/hierarchy_filter_dependency_representer.rb
new file mode 100644
index 00000000000..e8187e0cb6b
--- /dev/null
+++ b/lib/api/v3/queries/schemas/hierarchy_filter_dependency_representer.rb
@@ -0,0 +1,40 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 API
+ module V3
+ module Queries
+ module Schemas
+ class HierarchyFilterDependencyRepresenter < FilterDependencyRepresenter
+ def type = "[]CustomField::Hierarchy::Item"
+ def href_callback = api_v3_paths.custom_field_items(filter.custom_field.id)
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index 3a862ac231c..6f46cffc495 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -215,6 +215,11 @@ module API
"#{root}/custom_field_items/#{id}"
end
+ # API::V3::Queries::Filters::QueryFilterInstanceRepresenter need a path derived from a class name.
+ def self.hierarchy_item_filter_adapter(id)
+ custom_field_item(id)
+ end
+
def self.custom_field_item_branch(id)
"#{custom_field_item(id)}/branch"
end
diff --git a/spec/lib/api/v3/queries/schemas/filter_dependency_representer_factory_spec.rb b/spec/lib/api/v3/queries/schemas/filter_dependency_representer_factory_spec.rb
index 767dbd11edc..d57d262df4f 100644
--- a/spec/lib/api/v3/queries/schemas/filter_dependency_representer_factory_spec.rb
+++ b/spec/lib/api/v3/queries/schemas/filter_dependency_representer_factory_spec.rb
@@ -157,6 +157,17 @@ RSpec.describe API::V3::Queries::Schemas::FilterDependencyRepresenterFactory do
it_behaves_like "includes the cf json_cache_key mixin"
end
+ describe "type hierarchy" do
+ let(:hierarchy_root) { build_stubbed(:hierarchy_item) }
+ let(:custom_field) { build_stubbed(:custom_field, field_format: "hierarchy", hierarchy_root:) }
+
+ it "is the hierarchy dependency" do
+ expect(subject).to be_a(API::V3::Queries::Schemas::HierarchyFilterDependencyRepresenter)
+ end
+
+ it_behaves_like "includes the cf json_cache_key mixin"
+ end
+
context "type date" do
let(:custom_field) { build_stubbed(:date_wp_custom_field) }
diff --git a/spec/lib/api/v3/queries/schemas/hierarchy_filter_dependency_representer_spec.rb b/spec/lib/api/v3/queries/schemas/hierarchy_filter_dependency_representer_spec.rb
new file mode 100644
index 00000000000..33ac0010489
--- /dev/null
+++ b/spec/lib/api/v3/queries/schemas/hierarchy_filter_dependency_representer_spec.rb
@@ -0,0 +1,106 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 API::V3::Queries::Schemas::HierarchyFilterDependencyRepresenter do
+ include API::V3::Utilities::PathHelper
+
+ let(:project) { build_stubbed(:project) }
+ let(:query) { build_stubbed(:query, project:) }
+ let(:hierarchy_cf) { build_stubbed(:custom_field, field_format: "hierarchy", hierarchy_root:) }
+ let(:hierarchy_root) { build_stubbed(:hierarchy_item) }
+ let(:filter) do
+ Queries::WorkPackages::Filter::CustomFieldFilter.from_custom_field!(
+ custom_field: hierarchy_cf,
+ context: query
+ )
+ end
+ let(:form_embedded) { false }
+
+ let(:instance) { described_class.new(filter, operator, form_embedded:) }
+
+ subject(:generated) { instance.to_json }
+
+ describe "generation" do
+ describe "properties" do
+ describe "values" do
+ let(:path) { "values" }
+ let(:type) { "[]CustomField::Hierarchy::Item" }
+ let(:href) { api_v3_paths.custom_field_items(hierarchy_cf.id) }
+
+ context "for operator 'Queries::Operators::Equals'" do
+ let(:operator) { Queries::Operators::Equals }
+
+ it_behaves_like "filter dependency with allowed link"
+ end
+
+ context "for operator 'Queries::Operators::NotEquals'" do
+ let(:operator) { Queries::Operators::NotEquals }
+
+ it_behaves_like "filter dependency with allowed link"
+ end
+ end
+ end
+
+ describe "caching" do
+ let(:operator) { Queries::Operators::Equals }
+
+ before do
+ # fill the cache
+ instance.to_json
+
+ allow(instance).to receive(:to_hash).and_call_original
+ end
+
+ it "is cached" do
+ instance.to_json
+ expect(instance).not_to have_received(:to_hash)
+ end
+
+ it "busts the cache on a different operator" do
+ instance.send(:operator=, Queries::Operators::NotEquals)
+ instance.to_json
+ expect(instance).to have_received(:to_hash)
+ end
+
+ it "busts the cache on changes to the locale" do
+ I18n.with_locale(:de) do
+ instance.to_json
+ end
+ expect(instance).to have_received(:to_hash)
+ end
+
+ it "busts the cache on different form_embedded" do
+ instance.send(:form_embedded=, true)
+ instance.to_json
+ expect(instance).to have_received(:to_hash)
+ end
+ end
+ end
+end