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

Introduce DSL to build item groups

Jan Sandbrink22b9d3f9c726
diff --git a/app/components/admin/custom_fields/hierarchy/item_component.rb b/app/components/admin/custom_fields/hierarchy/item_component.rb
index 0b0e9a84123..68212550b73 100644
--- a/app/components/admin/custom_fields/hierarchy/item_component.rb
+++ b/app/components/admin/custom_fields/hierarchy/item_component.rb
@@ -72,31 +72,30 @@ module Admin
end
def menu_items(menu) # rubocop:disable Metrics/AbcSize
- edit_action_item(menu)
- menu.with_divider
- add_above_action_item(menu)
- add_below_action_item(menu)
- add_sub_item_action_item(menu)
+ with_item_group(menu) { edit_action_item(menu) }
- if OpenProject::FeatureDecisions.change_hierarchy_item_parent_active?
- menu.with_divider
- change_parent_item(menu)
+ with_item_group(menu) do
+ add_above_action_item(menu)
+ add_below_action_item(menu)
+ add_sub_item_action_item(menu)
end
- unless first_item? && last_item?
- menu.with_divider
- end
- unless first_item?
- move_to_top_action_item(menu)
- move_up_action_item(menu)
+ if OpenProject::FeatureDecisions.change_hierarchy_item_parent_active?
+ with_item_group(menu) { change_parent_item(menu) }
end
- unless last_item?
- move_down_action_item(menu)
- move_to_bottom_action_item(menu)
+
+ with_item_group(menu) do
+ unless first_item?
+ move_to_top_action_item(menu)
+ move_up_action_item(menu)
+ end
+ unless last_item?
+ move_down_action_item(menu)
+ move_to_bottom_action_item(menu)
+ end
end
- menu.with_divider
- deletion_action_item(menu)
+ with_item_group(menu) { deletion_action_item(menu) }
end
private
diff --git a/app/components/op_primer/component_helpers.rb b/app/components/op_primer/component_helpers.rb
index 7b47bc68836..11f4f78117a 100644
--- a/app/components/op_primer/component_helpers.rb
+++ b/app/components/op_primer/component_helpers.rb
@@ -61,5 +61,32 @@ module OpPrimer
render(row, &)
end
end
+
+ # Intended to help in building action menus with different sections, separated by dividers. Wrap all items that
+ # should go into the same section into a with_item_group block. Dividers are only rendered if needed (no empty sections).
+ #
+ # @example
+ # def render_action_menu(menu)
+ # with_item_group { menu.with_item(label: "Item alone in section") }
+ # with_item_group do
+ # menu.with_item(label: "hidden section") if false
+ # end
+ # with_item_group do
+ # menu.with_item(label: "first in section")
+ # menu.with_item(label: "last in section")
+ # end
+ # end
+ def with_item_group(menu)
+ pending_divider = menu.items.any? && menu.items.last[:type] != :divider
+ if pending_divider
+ menu.with_divider
+ end
+
+ size_before = menu.items.size
+ yield
+
+ # undo insertion of divider, if no items were added in this section
+ menu.items.pop if menu.items.size == size_before
+ end
end
end