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

Remove N+1 queries from CustomFieldSection rendering

Dombi Attilaabbaa50ced93
diff --git a/app/components/settings/project_custom_field_sections/index_component.html.erb b/app/components/settings/project_custom_field_sections/index_component.html.erb
index 68f40c5ed86..2d26ddc97e8 100644
--- a/app/components/settings/project_custom_field_sections/index_component.html.erb
+++ b/app/components/settings/project_custom_field_sections/index_component.html.erb
@@ -6,7 +6,7 @@
flex.with_row(
data: draggable_item_config(section)
) do
- render(Settings::ProjectCustomFieldSections::ShowComponent.new(project_custom_field_section: section))
+ render(Settings::ProjectCustomFieldSections::ShowComponent.new(project_custom_field_section: section, first_and_last:))
end
end
end
diff --git a/app/components/settings/project_custom_field_sections/index_component.rb b/app/components/settings/project_custom_field_sections/index_component.rb
index c4e6e2098d0..7f4d7bdff1a 100644
--- a/app/components/settings/project_custom_field_sections/index_component.rb
+++ b/app/components/settings/project_custom_field_sections/index_component.rb
@@ -39,6 +39,10 @@ module Settings
@project_custom_field_sections = project_custom_field_sections
end
+ def first_and_last
+ [@project_custom_field_sections.first, @project_custom_field_sections.last]
+ end
+
private
def wrapper_data_attributes
diff --git a/app/components/settings/project_custom_field_sections/show_component.rb b/app/components/settings/project_custom_field_sections/show_component.rb
index 1f3ed82c392..76fa3c16a73 100644
--- a/app/components/settings/project_custom_field_sections/show_component.rb
+++ b/app/components/settings/project_custom_field_sections/show_component.rb
@@ -33,11 +33,12 @@ module Settings
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- def initialize(project_custom_field_section:)
+ def initialize(project_custom_field_section:, first_and_last: [])
super
@project_custom_field_section = project_custom_field_section
@project_custom_fields = project_custom_field_section.custom_fields
+ @first_and_last = first_and_last
end
private
@@ -64,17 +65,12 @@ module Settings
end
def move_actions(menu)
- # TODO: these methods trigger database queries for each section displayed
- # it would be nice if can eager load this information
- first_in_list = @project_custom_field_section.first?
- last_in_list = @project_custom_field_section.last?
-
- unless first_in_list
+ unless first?
move_action_item(menu, :highest, t("label_agenda_item_move_to_top"),
"move-to-top")
move_action_item(menu, :higher, t("label_agenda_item_move_up"), "chevron-up")
end
- unless last_in_list
+ unless last?
move_action_item(menu, :lower, t("label_agenda_item_move_down"),
"chevron-down")
move_action_item(menu, :lowest, t("label_agenda_item_move_to_bottom"),
@@ -122,6 +118,24 @@ module Settings
item.with_leading_visual_icon(icon: :trash)
end
end
+
+ def first?
+ @first ||=
+ if @first_and_last.first
+ @first_and_last.first == @project_custom_field_section
+ else
+ @project_custom_field_section.first?
+ end
+ end
+
+ def last?
+ @last ||=
+ if @first_and_last.last
+ @first_and_last.last == @project_custom_field_section
+ else
+ @project_custom_field_section.last?
+ end
+ end
end
end
end