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

refactor: unify rendering of add relations menu items

Christophe Bliardd0286a45edd8
diff --git a/app/components/work_package_relations_tab/index_component.html.erb b/app/components/work_package_relations_tab/index_component.html.erb
index 45cf075a6b3..1b6e2ee3ab5 100644
--- a/app/components/work_package_relations_tab/index_component.html.erb
+++ b/app/components/work_package_relations_tab/index_component.html.erb
@@ -22,23 +22,7 @@
t(:label_relation)
end
- render_child_menu_items(menu)
-
- if should_render_add_relations?
- Relation::TYPES.each do |relation_type, type_configuration_hash|
- label_key = "#{I18N_NAMESPACE}.relations.#{type_configuration_hash[:name]}_singular"
- menu.with_item(
- label: t(label_key).upcase_first,
- href: new_relation_path(relation_type:),
- test_selector: new_button_test_selector(relation_type:),
- content_arguments: {
- data: { turbo_stream: true }
- }
- ) do |item|
- item.with_description.with_content(t("#{I18N_NAMESPACE}.relations.#{relation_type}_description"))
- end
- end
- end
+ render_add_relations_menu_items(menu, ADD_RELATION_MENU_TYPES)
end
end
end
diff --git a/app/components/work_package_relations_tab/index_component.rb b/app/components/work_package_relations_tab/index_component.rb
index def78ca9cf1..bdb454b23cc 100644
--- a/app/components/work_package_relations_tab/index_component.rb
+++ b/app/components/work_package_relations_tab/index_component.rb
@@ -11,6 +11,27 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
NEW_RELATION_ACTION_MENU = "new-relation-action-menu"
NEW_CHILD_ACTION_MENU = "new-child-action-menu"
I18N_NAMESPACE = "work_package_relations_tab"
+
+ ADD_CHILD_MENU_TYPES = [
+ "new_child",
+ Relation::TYPE_CHILD
+ ].freeze
+
+ ADD_RELATION_MENU_TYPES = [
+ *ADD_CHILD_MENU_TYPES,
+ Relation::TYPE_RELATES,
+ Relation::TYPE_FOLLOWS,
+ Relation::TYPE_PRECEDES,
+ Relation::TYPE_DUPLICATES,
+ Relation::TYPE_DUPLICATED,
+ Relation::TYPE_BLOCKS,
+ Relation::TYPE_BLOCKED,
+ Relation::TYPE_INCLUDES,
+ Relation::TYPE_PARTOF,
+ Relation::TYPE_REQUIRES,
+ Relation::TYPE_REQUIRED
+ ].freeze
+
include ApplicationHelper
include OpPrimer::ComponentHelpers
include Turbo::FramesHelper
@@ -92,39 +113,65 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
t("work_package_relations_tab.label_add_child_button")
end
- render_child_menu_items(menu)
+ render_add_relations_menu_items(menu, ADD_CHILD_MENU_TYPES)
end
end
end
end
end
- def render_child_menu_items(menu) # rubocop:disable Metrics/AbcSize
- return unless should_render_add_child?
-
- if helpers.current_user.allowed_in_project?(:add_work_packages, work_package.project)
- menu.with_item(
- label: t("work_package_relations_tab.relations.new_child"),
- href: new_project_work_packages_dialog_path(work_package.project, parent_id: work_package.id),
- content_arguments: {
- data: { turbo_stream: true }
- }
- ) do |item|
- item.with_description.with_content(t("work_package_relations_tab.relations.new_child_text"))
- end
- end
+ # Renders the menu items for the add relations action menu
+ #
+ # @param menu [Primer::Alpha::ActionMenu] The action menu component to render the items in
+ # @param relation_types [Array<String>] The relation types to render menu items for
+ def render_add_relations_menu_items(menu, relation_types)
+ relation_types
+ .filter { |relation_type| can_add_relation?(relation_type) }
+ .each { |relation_type| render_add_relation_menu_item(menu, relation_type) }
+ end
+ def render_add_relation_menu_item(menu, relation_type)
menu.with_item(
- label: t("work_package_relations_tab.relations.child"),
- href: new_work_package_children_relation_path(work_package),
+ label: label(relation_type),
+ href: new_relation_path(relation_type),
+ test_selector: new_button_test_selector(relation_type),
content_arguments: {
data: { turbo_stream: true }
}
) do |item|
- item.with_description.with_content(t("work_package_relations_tab.relations.child_description"))
+ item.with_description.with_content(description(relation_type))
+ end
+ end
+
+ def can_add_relation?(relation_type)
+ case relation_type
+ when "new_child"
+ should_render_add_child? && helpers.current_user.allowed_in_project?(:add_work_packages, work_package.project)
+ when Relation::TYPE_CHILD
+ should_render_add_child?
+ when *Relation::TYPES.keys
+ should_render_add_relations?
+ else
+ false
end
end
+ def label(relation_type)
+ label_key =
+ if Relation::TYPES.key?(relation_type)
+ "#{Relation::TYPES[relation_type][:name]}_singular"
+ else
+ relation_type
+ end
+
+ label = t("#{I18N_NAMESPACE}.relations.#{label_key}")
+ label.upcase_first
+ end
+
+ def description(relation_type)
+ I18n.t("#{I18N_NAMESPACE}.relations.#{relation_type}_description")
+ end
+
def render_items(border_box, relation_items)
relation_items.each do |relation_item|
relation = relation_item.relation || relation_item.related
@@ -154,17 +201,20 @@ class WorkPackageRelationsTab::IndexComponent < ApplicationComponent
&& item.instance_of?(relation_to_scroll_to.class)
end
- def new_relation_path(relation_type:)
- raise ArgumentError, "Invalid relation type: #{relation_type}" unless Relation::TYPES.key?(relation_type)
-
- if relation_type == Relation::TYPE_CHILD
- raise NotImplementedError, "Child relations are not supported yet"
- else
+ def new_relation_path(relation_type)
+ case relation_type
+ when "new_child"
+ new_project_work_packages_dialog_path(work_package.project, parent_id: work_package.id)
+ when Relation::TYPE_CHILD
+ new_work_package_children_relation_path(work_package)
+ when *Relation::TYPES.keys
new_work_package_relation_path(work_package, relation_type:)
+ else
+ raise ArgumentError, "Invalid relation type: #{relation_type}"
end
end
- def new_button_test_selector(relation_type:)
+ def new_button_test_selector(relation_type)
"op-new-relation-button-#{relation_type}"
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index a3366c4cfe2..bee3ba56a3c 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -812,7 +812,7 @@ en:
label_child_singular: "child"
label_child_plural: "children"
new_child: "Create new child"
- new_child_text: "Creates a related work package as a sub-item of the current (parent) work package"
+ new_child_description: "Creates a related work package as a sub-item of the current (parent) work package"
child: "Child"
child_description: "Makes the related work package a sub-item of the current (parent) work package"
label_closest: "Closest"