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

display query name as title

ulferts626dee44555f
diff --git a/app/components/projects/index_page_header_component.html.erb b/app/components/projects/index_page_header_component.html.erb
index 7d2180d1212..b1cd915420d 100644
--- a/app/components/projects/index_page_header_component.html.erb
+++ b/app/components/projects/index_page_header_component.html.erb
@@ -1,5 +1,5 @@
<%= render(Primer::OpenProject::PageHeader.new) do |header| %>
- <% header.with_title(variant: :large) { t(:label_project_plural) } %>
+ <% header.with_title(variant: :large, data: { 'test-selector': 'project-query-name'}) { page_title } %>
<% header.with_actions do %>
<%= render(
diff --git a/app/components/projects/index_page_header_component.rb b/app/components/projects/index_page_header_component.rb
index 24828218703..802c523388e 100644
--- a/app/components/projects/index_page_header_component.rb
+++ b/app/components/projects/index_page_header_component.rb
@@ -50,6 +50,10 @@ class Projects::IndexPageHeaderComponent < ApplicationComponent
.pluck(:id)
end
+ def page_title
+ query.name || t(:label_project_plural)
+ end
+
def gantt_portfolio_title
title = t('projects.index.open_as_gantt_title')
diff --git a/app/controllers/projects/menus_controller.rb b/app/controllers/projects/menus_controller.rb
index 2cbdadb8b84..ff905ca4b52 100644
--- a/app/controllers/projects/menus_controller.rb
+++ b/app/controllers/projects/menus_controller.rb
@@ -48,13 +48,9 @@ module Projects
def static_filters
[
- filter_menu_item(I18n.t(:'projects.sidemenu.all'),
- [],
- selected: filters_in_props.nil? && query_in_props.nil?),
- filter_menu_item(I18n.t(:'projects.sidemenu.my'),
- [{ member_of: { operator: '=', values: ['t'] } }]),
- filter_menu_item(I18n.t(:'projects.sidemenu.archived'),
- [{ active: { operator: '=', values: ['f'] } }])
+ query_menu_item(Queries::Projects::DefaultFactory.static_query_all, selected: no_query_props?),
+ query_menu_item(Queries::Projects::DefaultFactory.static_query_my, id: 'my'),
+ query_menu_item(Queries::Projects::DefaultFactory.static_query_archived, id: 'archived')
]
end
@@ -67,16 +63,10 @@ module Projects
end
end
- def filter_menu_item(title, filters, selected: filter_item_selected?(filters))
- OpenProject::Menu::MenuItem.new(title:,
- href: projects_path_with_filters(filters),
- selected:)
- end
-
- def query_menu_item(query)
+ def query_menu_item(query, id: nil, selected: query_item_selected?(id || query.id))
OpenProject::Menu::MenuItem.new(title: query.name,
- href: projects_path(query_id: query.id),
- selected: query_item_selected?(query))
+ href: projects_path(query_id: id || query.id),
+ selected:)
end
def projects_path_with_filters(filters)
@@ -85,25 +75,12 @@ module Projects
projects_path(filters: filters.to_json, hide_filters_section: true)
end
- def filter_item_selected?(filters)
- filters == filters_in_props && query_in_props.nil?
+ def query_item_selected?(id)
+ id.to_s == params[:query_id] && params[:filters].nil?
end
- def query_item_selected?(query)
- query.id.to_s == query_in_props && filters_in_props.nil?
- end
-
- def query_in_props
- params[:query_id]
- end
-
- def filters_in_props
- @filters_in_props ||= begin
- # Will have to recheck params[:filters] again in case it is empty but that is cheap.
- JSON.parse(params[:filters]).map(&:deep_symbolize_keys) if params[:filters]
- rescue JSON::ParserError
- nil
- end
+ def no_query_props?
+ params[:query_id].nil? && params[:filters].nil?
end
end
end
diff --git a/app/controllers/projects/queries_controller.rb b/app/controllers/projects/queries_controller.rb
index fa755bdf6a9..3f3e30c621e 100644
--- a/app/controllers/projects/queries_controller.rb
+++ b/app/controllers/projects/queries_controller.rb
@@ -41,13 +41,5 @@ class Projects::QueriesController < ApplicationController
def load_query
@query = ParamsToQueryService.new(Project, current_user).call(params)
-
- # Set default filter on status no filter is provided.
- @query.where('active', '=', OpenProject::Database::DB_VALUE_TRUE) unless params[:filters]
-
- # Order lft for now
- @query.order(lft: :asc) unless params[:sortBy]
-
- @query
end
end
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index 3f04158d283..e329c0607cd 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -117,14 +117,6 @@ class ProjectsController < ApplicationController
def load_query
@query = ParamsToQueryService.new(Project, current_user).call(params)
-
- # Set default filter on status no filter is provided.
- @query.where('active', '=', OpenProject::Database::DB_VALUE_TRUE) unless params[:filters]
-
- # Order lft if no order is provided.
- @query.order(lft: :asc) unless params[:sortBy]
-
- @query
end
def export_list(mime_type)
diff --git a/app/models/queries/projects/default_factory.rb b/app/models/queries/projects/default_factory.rb
new file mode 100644
index 00000000000..c954557c019
--- /dev/null
+++ b/app/models/queries/projects/default_factory.rb
@@ -0,0 +1,70 @@
+# -- 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.
+# ++
+
+class Queries::Projects::DefaultFactory
+ class << self
+ def find(id)
+ static_query(id) || Queries::Projects::ProjectQuery.find(id)
+ end
+
+ def static_query(id)
+ case id
+ when nil, 'all'
+ static_query_all
+ when 'my'
+ static_query_my
+ when 'archived'
+ static_query_archived
+ end
+ end
+
+ def static_query_all
+ Queries::Projects::ProjectQuery.new(name: I18n.t(:'projects.sidemenu.all')) do |query|
+ query.where('active', '=', OpenProject::Database::DB_VALUE_TRUE)
+
+ query.order(lft: :asc)
+ end
+ end
+
+ def static_query_my
+ Queries::Projects::ProjectQuery.new(name: I18n.t(:'projects.sidemenu.my')) do |query|
+ query.where('member_of', '=', OpenProject::Database::DB_VALUE_TRUE)
+
+ query.order(lft: :asc)
+ end
+ end
+
+ def static_query_archived
+ Queries::Projects::ProjectQuery.new(name: I18n.t(:'projects.sidemenu.archived')) do |query|
+ query.where('active', '=', OpenProject::Database::DB_VALUE_FALSE)
+
+ query.order(lft: :asc)
+ end
+ end
+ end
+end
diff --git a/app/services/params_to_query_service.rb b/app/services/params_to_query_service.rb
index da0052c290d..899a832b3f5 100644
--- a/app/services/params_to_query_service.rb
+++ b/app/services/params_to_query_service.rb
@@ -28,15 +28,18 @@
class ParamsToQueryService
attr_accessor :user,
- :query_class
+ :query_class,
+ :model
def initialize(model, user, query_class: nil)
- set_query_class(query_class, model)
+ self.model = model
self.user = user
+
+ set_query_class(query_class)
end
def call(params)
- query = find_query(params)
+ query = find_query(params[:query_id])
query = apply_filters(query, params)
apply_order(query, params)
@@ -45,9 +48,15 @@ class ParamsToQueryService
private
- def find_query(params)
- if params[:query_id]
- query_class.find(params[:query_id])
+ def find_query(query_id)
+ model_name = model.name
+
+ if "::Queries::#{model_name.pluralize}".constantize.const_defined?(:DefaultFactory) && query_id
+ factory = "::Queries::#{model_name.pluralize}::DefaultFactory".constantize
+
+ factory.find(query_id)
+ elsif query_class.respond_to?(:find) && query_id
+ query_class.find(query_id)
else
query_class.new(user:)
end
@@ -56,6 +65,8 @@ class ParamsToQueryService
def apply_filters(query, params)
return query if params[:filters].blank?
+ query.filters = []
+
filters = parse_filters_from_json(params[:filters])
filters[:attributes].each do |filter_name|
@@ -70,6 +81,8 @@ class ParamsToQueryService
def apply_order(query, params)
return query unless params[:sortBy]
+ query.orders = []
+
sort = parse_sorting_from_json(params[:sortBy])
hash_sort = sort.each_with_object({}) do |(attribute, direction), hash|
@@ -131,7 +144,7 @@ class ParamsToQueryService
@conversion_model ||= ::API::Utilities::QueryFiltersNameConverterContext.new(query_class)
end
- def set_query_class(query_class, model)
+ def set_query_class(query_class)
self.query_class = if query_class
query_class
else
diff --git a/spec/features/projects/projects_index_spec.rb b/spec/features/projects/projects_index_spec.rb
index a5f207f41a7..85cc7906d98 100644
--- a/spec/features/projects/projects_index_spec.rb
+++ b/spec/features/projects/projects_index_spec.rb
@@ -1182,6 +1182,8 @@ RSpec.describe 'Projects index page',
# Reloading the persisted query will reconstruct filters and columns
projects_page.set_sidebar_filter('My saved query')
+ projects_page.expect_title('My saved query')
+
projects_page.expect_projects_listed(project)
projects_page.expect_projects_not_listed(public_project, development_project)
end
diff --git a/spec/services/params_to_query_service_spec.rb b/spec/services/params_to_query_service_capability_query_spec.rb
similarity index 58%
rename from spec/services/params_to_query_service_spec.rb
rename to spec/services/params_to_query_service_capability_query_spec.rb
index 09e776a20d3..cfddd7a7d84 100644
--- a/spec/services/params_to_query_service_spec.rb
+++ b/spec/services/params_to_query_service_capability_query_spec.rb
@@ -28,68 +28,33 @@
require 'spec_helper'
-RSpec.describe ParamsToQueryService do
+RSpec.describe ParamsToQueryService, 'capability query' do
# This spec does currently not cover the whole functionality.
-
let(:user) { build_stubbed(:admin) }
-
+ let(:model) { Capability }
let(:params) { {} }
let(:instance) { described_class.new(model, user) }
let(:service_call) { instance.call(params) }
- context 'for a new Project query' do
- let(:model) { Project }
-
+ context 'for a new query' do
context 'when applying filters' do
let(:params) do
- { filters: JSON.dump([{ active: { operator: "=", values: ["t"] } },
- { created_at: { operator: ">t-", values: ["4"] } }]) }
+ { filters: JSON.dump([{ principal: { operator: "=", values: ["4"] } },
+ { context: { operator: "=", values: ["g"] } },
+ { action: { operator: "=", values: ["projects/create"] } }]) }
end
it 'returns a new query' do
expect(service_call)
- .to be_a Queries::Projects::ProjectQuery
+ .to be_a Queries::Capabilities::CapabilityQuery
end
it 'applies the filters' do
expect(service_call.filters.map { |f| { field: f.field, operator: f.operator, values: f.values } })
- .to contain_exactly({ field: :active, operator: "=", values: ["t"] },
- { field: :created_at, operator: ">t-", values: ["4"] })
- end
- end
-
- context 'with an empty filters string' do
- let(:params) do
- { filters: "" }
+ .to contain_exactly({ field: :principal_id, operator: "=", values: ["4"] },
+ { field: :context, operator: "=", values: ["g"] },
+ { field: :action, operator: "=", values: ["projects/create"] })
end
-
- it 'returns a new query' do
- expect(service_call)
- .to be_a Queries::Projects::ProjectQuery
- end
-
- it 'applies no filters' do
- expect(service_call.filters)
- .to be_empty
- end
- end
- end
-
- context 'for a persisted Project query' do
- let(:model) { Project }
- let(:projects_query) { build_stubbed(:project_query) }
- let(:params) { { query_id: projects_query.id.to_s } }
-
- before do
- allow(Queries::Projects::ProjectQuery)
- .to receive(:find)
- .with(projects_query.id.to_s)
- .and_return(projects_query)
- end
-
- it 'returns the query' do
- expect(service_call)
- .to eq projects_query
end
end
end
diff --git a/spec/services/params_to_query_service_project_query_spec.rb b/spec/services/params_to_query_service_project_query_spec.rb
new file mode 100644
index 00000000000..efdfd3e3adf
--- /dev/null
+++ b/spec/services/params_to_query_service_project_query_spec.rb
@@ -0,0 +1,191 @@
+# -- 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.
+# ++
+
+require 'spec_helper'
+
+RSpec.describe ParamsToQueryService, 'project query' do
+ # This spec does currently not cover the whole functionality.
+
+ let(:user) { build_stubbed(:admin) }
+ let(:model) { Project }
+ let(:params) { {} }
+ let(:instance) { described_class.new(model, user) }
+ let(:service_call) { instance.call(params) }
+
+ context 'for a new query' do
+ context 'when applying filters' do
+ let(:params) do
+ { filters: JSON.dump([{ active: { operator: "=", values: ["t"] } },
+ { created_at: { operator: ">t-", values: ["4"] } }]) }
+ end
+
+ it 'returns a new query' do
+ expect(service_call)
+ .to be_a Queries::Projects::ProjectQuery
+ end
+
+ it 'applies the filters' do
+ expect(service_call.filters.map { |f| { field: f.field, operator: f.operator, values: f.values } })
+ .to contain_exactly({ field: :active, operator: "=", values: ["t"] },
+ { field: :created_at, operator: ">t-", values: ["4"] })
+ end
+ end
+
+ context 'with an empty filters string' do
+ let(:params) do
+ { filters: "" }
+ end
+
+ it 'returns a new query' do
+ expect(service_call)
+ .to be_a Queries::Projects::ProjectQuery
+ end
+
+ it 'applies the active filter' do
+ expect(service_call.filters.map { |f| { field: f.field, operator: f.operator, values: f.values } })
+ .to contain_exactly({ field: :active, operator: "=", values: ["t"] })
+ end
+
+ it 'sorts by lft asc' do
+ expect(service_call.orders.map { |o| { attribute: o.attribute, direction: o.direction } })
+ .to eql [{ attribute: :lft, direction: :asc }]
+ end
+ end
+
+ context 'when sending neither filters nor orders props' do
+ it 'returns a new query' do
+ expect(service_call)
+ .to be_a Queries::Projects::ProjectQuery
+ end
+
+ it 'sets the expected name' do
+ expect(service_call.name)
+ .to eql I18n.t(:'projects.sidemenu.all')
+ end
+
+ it 'applies the active filter' do
+ expect(service_call.filters.map { |f| { field: f.field, operator: f.operator, values: f.values } })
+ .to contain_exactly({ field: :active, operator: "=", values: ["t"] })
+ end
+
+ it 'sorts by lft asc' do
+ expect(service_call.orders.map { |o| { attribute: o.attribute, direction: o.direction } })
+ .to eql [{ attribute: :lft, direction: :asc }]
+ end
+ end
+ end
+
+ context "when providing the 'all' identifier" do
+ let(:params) { { query_id: 'all' } }
+
+ it 'returns a new query' do
+ expect(service_call)
+ .to be_a Queries::Projects::ProjectQuery
+ end
+
+ it 'sets the expected name' do
+ expect(service_call.name)
+ .to eql I18n.t(:'projects.sidemenu.all')
+ end
+
+ it 'applies the active filter' do
+ expect(service_call.filters.map { |f| { field: f.field, operator: f.operator, values: f.values } })
+ .to contain_exactly({ field: :active, operator: "=", values: ["t"] })
+ end
+
+ it 'sorts by lft asc' do
+ expect(service_call.orders.map { |o| { attribute: o.attribute, direction: o.direction } })
+ .to eql [{ attribute: :lft, direction: :asc }]
+ end
+ end
+
+ context "when providing the 'my' identifier" do
+ let(:params) { { query_id: 'my' } }
+
+ it 'returns a new query' do
+ expect(service_call)
+ .to be_a Queries::Projects::ProjectQuery
+ end
+
+ it 'sets the expected name' do
+ expect(service_call.name)
+ .to eql I18n.t(:'projects.sidemenu.my')
+ end
+
+ it 'applies the active filter' do
+ expect(service_call.filters.map { |f| { field: f.field, operator: f.operator, values: f.values } })
+ .to contain_exactly({ field: :member_of, operator: "=", values: ["t"] })
+ end
+
+ it 'sorts by lft asc' do
+ expect(service_call.orders.map { |o| { attribute: o.attribute, direction: o.direction } })
+ .to eql [{ attribute: :lft, direction: :asc }]
+ end
+ end
+
+ context "when providing the 'archived' identifier" do
+ let(:params) { { query_id: 'archived' } }
+
+ it 'returns a new query' do
+ expect(service_call)
+ .to be_a Queries::Projects::ProjectQuery
+ end
+
+ it 'sets the expected name' do
+ expect(service_call.name)
+ .to eql I18n.t(:'projects.sidemenu.archived')
+ end
+
+ it 'applies the active filter' do
+ expect(service_call.filters.map { |f| { field: f.field, operator: f.operator, values: f.values } })
+ .to contain_exactly({ field: :active, operator: "=", values: ["f"] })
+ end
+
+ it 'sorts by lft asc' do
+ expect(service_call.orders.map { |o| { attribute: o.attribute, direction: o.direction } })
+ .to eql [{ attribute: :lft, direction: :asc }]
+ end
+ end
+
+ context 'for a persisted query' do
+ let(:projects_query) { build_stubbed(:project_query) }
+ let(:params) { { query_id: projects_query.id.to_s } }
+
+ before do
+ allow(Queries::Projects::ProjectQuery)
+ .to receive(:find)
+ .with(projects_query.id.to_s)
+ .and_return(projects_query)
+ end
+
+ it 'returns the query' do
+ expect(service_call)
+ .to eq projects_query
+ end
+ end
+end
diff --git a/spec/support/pages/projects/index.rb b/spec/support/pages/projects/index.rb
index 278fca206ae..b18235df5c3 100644
--- a/spec/support/pages/projects/index.rb
+++ b/spec/support/pages/projects/index.rb
@@ -69,6 +69,11 @@ module Pages
end
end
+ def expect_title(name)
+ expect(page)
+ .to have_css('[data-test-selector="project-query-name"]', text: name)
+ end
+
def expect_sidebar_filter(filter_name, selected: false)
within '#main-menu' do
expect(page).to have_css(".op-sidemenu--item-action#{selected ? '.selected' : ''}", text: filter_name)