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

Implement hierarchy filter with descendants

Bruno Pagno130f0c4f8b83
diff --git a/app/models/queries/filters/strategies/hierarchy.rb b/app/models/queries/filters/strategies/hierarchy.rb
index fc65be5c736..158ff5ab0c6 100644
--- a/app/models/queries/filters/strategies/hierarchy.rb
+++ b/app/models/queries/filters/strategies/hierarchy.rb
@@ -32,8 +32,14 @@ module Queries
module Filters
module Strategies
class Hierarchy < BaseStrategy
- self.supported_operators = %w[= !]
+ self.supported_operators = %w[= ! eq_with_descendants]
self.default_operator = "="
+
+ def operator_map
+ super.dup.tap do |super_value|
+ super_value["eq_with_descendants"] = ::Queries::Operators::CustomFields::Hierarchies::EqualsWithDescendants
+ end
+ end
end
end
end
diff --git a/app/models/queries/operators/custom_fields/hierarchies/equals_with_descendants.rb b/app/models/queries/operators/custom_fields/hierarchies/equals_with_descendants.rb
new file mode 100644
index 00000000000..941df671456
--- /dev/null
+++ b/app/models/queries/operators/custom_fields/hierarchies/equals_with_descendants.rb
@@ -0,0 +1,49 @@
+#-- 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 Operators
+ module CustomFields
+ module Hierarchies
+ class EqualsWithDescendants < Base
+ label "equals_with_descendants"
+ set_symbol "eq_with_descendants"
+
+ def self.sql_for_field(values, db_table, db_field)
+ items = CustomField::Hierarchy::Item.find(values)
+ service = ::CustomFields::Hierarchy::HierarchicalItemService.new
+
+ actual_values = items.map { |item| service.get_descendants(item:).value! }.flatten.map(&:id).uniq
+
+ Equals.sql_for_field(actual_values, db_table, db_field)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
index 49b5f882da1..415caf1ed0c 100644
--- a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
+++ b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
@@ -88,6 +88,13 @@ module CustomFields
Success(item.self_and_ancestors.reverse)
end
+ # Gets all descendant nodes in a tree starting from the item/node.
+ # @param item [CustomField::Hierarchy::Item] the node
+ # @return [Success(Array<CustomField::Hierarchy::Item>)]
+ def get_descendants(item:)
+ Success(item.self_and_descendants)
+ end
+
# Move an item/node to a new parent item/node
# @param item [CustomField::Hierarchy::Item] the parent of the node
# @param new_parent [CustomField::Hierarchy::Item] the new parent of the node
diff --git a/config/locales/en.yml b/config/locales/en.yml
index c4e2d3c6de8..41d1051eaa3 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2398,6 +2398,7 @@ en:
label_environment: "Environment"
label_estimates_and_progress: "Estimates and progress"
label_equals: "is"
+ label_equals_with_descendants: "is any with descendants"
label_everywhere: "everywhere"
label_example: "Example"
label_experimental: "Experimental"
diff --git a/spec/features/work_packages/table/queries/hierarchy_cf_filter_spec.rb b/spec/features/work_packages/table/queries/hierarchy_cf_filter_spec.rb
index 1ac769d5e3e..fdcaf803acd 100644
--- a/spec/features/work_packages/table/queries/hierarchy_cf_filter_spec.rb
+++ b/spec/features/work_packages/table/queries/hierarchy_cf_filter_spec.rb
@@ -81,7 +81,7 @@ RSpec.describe "Work package filtering by hierarchy custom field", :js do
end
context "when is not" do
- it "is not" do
+ it "shows work packages that do not match the hierarchy cf filter" do
filters.open
# Filtering by hierarchy (!)
@@ -92,5 +92,31 @@ RSpec.describe "Work package filtering by hierarchy custom field", :js do
wp_table.expect_work_package_listed(wp_leia)
end
end
+
+ context "when equals with descendants" do
+ let!(:grogu) { service.insert_item(parent: luke, label: "Grogu").value! }
+ let!(:wp_grogu) do
+ create(:work_package, project:, subject: "Grogu's wp").tap do |wp|
+ wp.custom_field_values = { hierarchy_cf.id => grogu.id }
+ wp.save!
+ end
+ end
+
+ it "shows the work packages with associated hierarchy items to the branch" do
+ filters.open
+
+ # Filtering by hierarchy (!)
+
+ filters.add_filter_by(
+ hierarchy_cf.name,
+ "is any with descendants",
+ [luke.label],
+ hierarchy_cf.attribute_name(:camel_case)
+ )
+
+ wp_table.ensure_work_package_not_listed!(wp_leia)
+ wp_table.expect_work_package_listed(wp_luke, wp_grogu)
+ end
+ end
end
end
diff --git a/spec/models/queries/operators/custom_fields/hierarchies/equals_with_descendants_spec.rb b/spec/models/queries/operators/custom_fields/hierarchies/equals_with_descendants_spec.rb
new file mode 100644
index 00000000000..80e0510556f
--- /dev/null
+++ b/spec/models/queries/operators/custom_fields/hierarchies/equals_with_descendants_spec.rb
@@ -0,0 +1,61 @@
+#-- 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 Queries::Operators::CustomFields::Hierarchies::EqualsWithDescendants do
+ subject(:sql) { described_class.sql_for_field(values, db_table, db_field) }
+
+ let(:custom_field) { create(:custom_field, field_format: "hierarchy", hierarchy_root: nil) }
+ let!(:root) { service.generate_root(custom_field).value! }
+ let!(:germany) { service.insert_item(parent: root, label: "Germany", short: "DE").value! }
+ let!(:berlin) { service.insert_item(parent: germany, label: "Berlin").value! }
+ let!(:munich) { service.insert_item(parent: germany, label: "Munich").value! }
+ let!(:portugal) { service.insert_item(parent: root, label: "Portugal", short: "PT").value! }
+ let!(:lisbon) { service.insert_item(parent: portugal, label: "Lisboa").value! }
+ let(:service) { CustomFields::Hierarchy::HierarchicalItemService.new }
+
+ let(:db_table) { "custom_values" }
+ let(:db_field) { "value" }
+
+ context "when generating for a branch" do
+ let(:values) { [germany.id] }
+
+ it "generates the expected SQL" do
+ expect(sql).to eq("custom_values.value IN ('#{germany.id}','#{berlin.id}','#{munich.id}')")
+ end
+ end
+
+ context "when generating for a leaf" do
+ let(:values) { [lisbon.id] }
+
+ it "generates the expected SQL" do
+ expect(sql).to eq("custom_values.value IN ('#{lisbon.id}')")
+ end
+ end
+end
diff --git a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
index a8c7df56082..2756565477e 100644
--- a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
+++ b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
@@ -165,6 +165,31 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService do
end
end
+ describe "#get_descendants" do
+ let!(:subitem) { service.insert_item(parent: mara, label: "Sub1").value! }
+ let!(:subitem2) { service.insert_item(parent: mara, label: "sub two").value! }
+ let!(:unrelated_subitem) { service.insert_item(parent: luke, label: "not related").value! }
+
+ context "with a non-root node" do
+ it "returns all the descendants to that item" do
+ result = service.get_descendants(item: mara)
+ expect(result).to be_success
+
+ descendants = result.value!
+ expect(descendants.size).to eq(3)
+ expect(descendants).to contain_exactly(mara, subitem, subitem2)
+ end
+ end
+
+ context "with a leaf node" do
+ it "returns just the leaf node" do
+ result = service.get_descendants(item: subitem2)
+ expect(result).to be_success
+ expect(result.value!).to match_array(subitem2)
+ end
+ end
+ end
+
describe "#move_item" do
let(:lando) { service.insert_item(parent: root, label: "lando").value! }