Files
Jan Sandbrink f9d8bc6614 Introduce SubclassResponsibility error
This error is intended for cases when a method is
intentionally not implemented, because the module/class defining
it expects a subclass (or class including the module) to implement
the method.

This is intended to distinguish it from other cases, such as:
* feature not implemented yet
* edge case of a method call not yet supported

Notably it avoids the misuse of the Ruby-defined NotImplementedError,
which is only intended for much more specific scenarios:

> Raised when a feature is not implemented on the current platform. For example, methods depending on the fsync or fork system calls may raise this exception [...]

Also see https://docs.ruby-lang.org/en/master/NotImplementedError.html
2026-03-27 08:14:56 +01:00

160 lines
4.6 KiB
Ruby

# 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.
# ++
class Submenu
include Rails.application.routes.url_helpers
attr_reader :view_type, :project, :params
def initialize(view_type:, params:, project: nil)
@view_type = view_type
@project = project
@params = params
end
def menu_items
[
menu_group(header: I18n.t("js.label_starred_queries"), children: starred_queries),
menu_group(header: I18n.t("js.label_default_queries"), children: default_queries),
menu_group(header: I18n.t("js.label_global_queries"), children: global_queries),
menu_group(header: I18n.t("js.label_custom_queries"), children: custom_queries)
]
end
def selected_menu_group
menu_items.detect do |group|
group.children.detect(&:selected)
end
end
def selected_menu_item
menu_items.each do |group|
group.children.each do |item|
return item if item.selected
end
end
nil
end
def starred_queries
base_query
.where("starred" => "t")
.pluck(:id, :name)
.map { |id, name| menu_item(title: name, query_params: query_params(id)) }
.sort_by { |item| item.title.downcase }
end
def default_queries
raise SubclassResponsibilityError
end
def global_queries
base_query
.where("starred" => "f")
.where("public" => "t")
.pluck(:id, :name)
.map { |id, name| menu_item(title: name, query_params: query_params(id)) }
.sort_by { |item| item.title.downcase }
end
def custom_queries
base_query
.where("starred" => "f")
.where("public" => "f")
.pluck(:id, :name)
.map { |id, name| menu_item(title: name, query_params: query_params(id)) }
.sort_by { |item| item.title.downcase }
end
def base_query
base_query ||= Query
.visible(User.current)
.includes(:project)
.joins(:views)
.where("views.type" => view_type)
if project.present?
base_query.where("queries.project_id" => project.id)
else
base_query.where("queries.project_id" => nil)
end
end
def query_params(id)
{ query_id: id }
end
def menu_group(header:, children:)
OpenProject::Menu::MenuGroup.new(header:, children:)
end
def menu_item(title:, icon_key: nil, count: nil, show_enterprise_icon: false,
query_params: {}, selected: selected?(query_params), href: query_path(query_params))
OpenProject::Menu::MenuItem.new(title:,
href:,
icon: icon_map.fetch(icon_key, icon_key),
count:,
selected:,
favorited: favorited?(query_params),
show_enterprise_icon:)
end
def selected?(query_params)
query_params.each_key do |filter_key|
next if filter_key == :show_enterprise_icon
if params[filter_key] != query_params[filter_key].to_s
return false
end
end
if query_params.empty? && %i[filters query_props query_id name].any? { params.key?(it) }
return false
end
true
end
def favorited?(_query_params)
false
end
def icon_map
{}
end
def query_path(query_params)
raise SubclassResponsibilityError
end
def url_helpers
@url_helpers ||= OpenProject::StaticRouting::StaticRouter.new.url_helpers
end
end