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

[#50893] Remove n+1 queries

Dombi Attila1117a38a39b2
diff --git a/modules/meeting/app/components/meeting_agenda_items/item_component.rb b/modules/meeting/app/components/meeting_agenda_items/item_component.rb
index 44e1e9792c6..a261f667945 100644
--- a/modules/meeting/app/components/meeting_agenda_items/item_component.rb
+++ b/modules/meeting/app/components/meeting_agenda_items/item_component.rb
@@ -34,13 +34,20 @@ module MeetingAgendaItems
with_collection_parameter :meeting_agenda_item
- def initialize(meeting_agenda_item:, state: :show, container: nil, display_notes_input: nil)
+ def initialize(
+ meeting_agenda_item:,
+ state: :show,
+ container: nil,
+ display_notes_input: nil,
+ first_and_last: []
+ )
super
@meeting_agenda_item = meeting_agenda_item
@state = state
@display_notes_input = display_notes_input
@container = container
+ @first_and_last = first_and_last
end
def wrapper_uniq_by
@@ -51,7 +58,7 @@ module MeetingAgendaItems
component_wrapper(:border_box_row, **wrapper_arguments) do
case @state
when :show
- render(MeetingAgendaItems::ItemComponent::ShowComponent.new(**child_component_params))
+ render(MeetingAgendaItems::ItemComponent::ShowComponent.new(**show_component_params))
when :edit
render(MeetingAgendaItems::ItemComponent::EditComponent.new(**child_component_params))
end
@@ -66,7 +73,11 @@ module MeetingAgendaItems
{
meeting_agenda_item: @meeting_agenda_item,
display_notes_input: (@display_notes_input if @state == :edit)
- }.compact
+ }.compact
+ end
+
+ def show_component_params
+ child_component_params.merge(first_and_last: @first_and_last).compact
end
def wrapper_arguments
diff --git a/modules/meeting/app/components/meeting_agenda_items/item_component/show_component.rb b/modules/meeting/app/components/meeting_agenda_items/item_component/show_component.rb
index 9802c66ec24..a013a8dfd9f 100644
--- a/modules/meeting/app/components/meeting_agenda_items/item_component/show_component.rb
+++ b/modules/meeting/app/components/meeting_agenda_items/item_component/show_component.rb
@@ -33,11 +33,12 @@ module MeetingAgendaItems
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- def initialize(meeting_agenda_item:)
+ def initialize(meeting_agenda_item:, first_and_last: [])
super
@meeting_agenda_item = meeting_agenda_item
@meeting = meeting_agenda_item.meeting
+ @first_and_last = first_and_last
end
def wrapper_uniq_by
@@ -54,9 +55,28 @@ module MeetingAgendaItems
@meeting.open? && User.current.allowed_in_project?(:manage_agendas, @meeting.project)
end
+ def first?
+ @first ||=
+ if @first_and_last.first
+ @first_and_last.first == @meeting_agenda_item
+ else
+ @meeting_agenda_item.first?
+ end
+ end
+
+ def last?
+ @last ||=
+ if @first_and_last.last
+ @first_and_last.last == @meeting_agenda_item
+ else
+ @meeting_agenda_item.last?
+ end
+ end
+
def meeting_closed?
end
+
def edit_action_item(menu)
menu.with_item(label: t("label_edit"),
href: edit_meeting_agenda_item_path(@meeting_agenda_item.meeting, @meeting_agenda_item),
@@ -78,10 +98,10 @@ module MeetingAgendaItems
end
def move_actions(menu)
- move_action_item(menu, :highest, t("label_agenda_item_move_to_top"), "move-to-top") unless @meeting_agenda_item.first?
- move_action_item(menu, :higher, t("label_agenda_item_move_up"), "chevron-up") unless @meeting_agenda_item.first?
- move_action_item(menu, :lower, t("label_agenda_item_move_down"), "chevron-down") unless @meeting_agenda_item.last?
- move_action_item(menu, :lowest, t("label_agenda_item_move_to_bottom"), "move-to-bottom") unless @meeting_agenda_item.last?
+ move_action_item(menu, :highest, t("label_agenda_item_move_to_top"), "move-to-top") unless first?
+ move_action_item(menu, :higher, t("label_agenda_item_move_up"), "chevron-up") unless first?
+ move_action_item(menu, :lower, t("label_agenda_item_move_down"), "chevron-down") unless last?
+ move_action_item(menu, :lowest, t("label_agenda_item_move_to_bottom"), "move-to-bottom") unless last?
end
def delete_action_item(menu)
diff --git a/modules/meeting/app/components/meeting_agenda_items/list_component.html.erb b/modules/meeting/app/components/meeting_agenda_items/list_component.html.erb
index db10ce79e62..76e1fcdd821 100644
--- a/modules/meeting/app/components/meeting_agenda_items/list_component.html.erb
+++ b/modules/meeting/app/components/meeting_agenda_items/list_component.html.erb
@@ -23,10 +23,12 @@
end
end
else
+ first_and_last = [@meeting.agenda_items.first, @meeting.agenda_items.last]
render(
MeetingAgendaItems::ItemComponent.with_collection(
- @meeting.agenda_items,
- container: border_box
+ @meeting.agenda_items.with_includes_to_render,
+ container: border_box,
+ first_and_last:
)
)
end
diff --git a/modules/meeting/app/controllers/concerns/meetings/agenda_component_streams.rb b/modules/meeting/app/controllers/concerns/meetings/agenda_component_streams.rb
index e37741c1328..4d65caf4657 100644
--- a/modules/meeting/app/controllers/concerns/meetings/agenda_component_streams.rb
+++ b/modules/meeting/app/controllers/concerns/meetings/agenda_component_streams.rb
@@ -89,9 +89,12 @@ module Meetings
end
def update_show_items_via_turbo_stream(meeting: @meeting)
- meeting.agenda_items.each do |meeting_agenda_item|
+ agenda_items = meeting.agenda_items.with_includes_to_render
+ first_and_last = [agenda_items.first, agenda_items.last]
+
+ agenda_items.each do |meeting_agenda_item|
update_via_turbo_stream(
- component: MeetingAgendaItems::ItemComponent::ShowComponent.new(meeting_agenda_item:)
+ component: MeetingAgendaItems::ItemComponent::ShowComponent.new(meeting_agenda_item:, first_and_last:)
)
end
end
@@ -136,7 +139,8 @@ module Meetings
replace_via_turbo_stream(
component: MeetingAgendaItems::ItemComponent.new(
state:,
- meeting_agenda_item:
+ meeting_agenda_item:,
+ display_notes_input:
)
)
update_show_items_via_turbo_stream
diff --git a/modules/meeting/app/models/meeting_agenda_item.rb b/modules/meeting/app/models/meeting_agenda_item.rb
index 3a99d356200..b130005d214 100644
--- a/modules/meeting/app/models/meeting_agenda_item.rb
+++ b/modules/meeting/app/models/meeting_agenda_item.rb
@@ -43,6 +43,8 @@ class MeetingAgendaItem < ApplicationRecord
acts_as_list scope: :meeting
default_scope { order(:position) }
+ scope :with_includes_to_render, -> { includes(:author, :meeting) }
+
validates :meeting_id, presence: true
validates :title, presence: true, if: Proc.new { |item| item.simple? }
validates :work_package_id, presence: true, if: Proc.new { |item| item.work_package? }, on: :create
diff --git a/modules/meeting/app/models/structured_meeting.rb b/modules/meeting/app/models/structured_meeting.rb
index 4cc8d7a4213..84e03b1a17b 100644
--- a/modules/meeting/app/models/structured_meeting.rb
+++ b/modules/meeting/app/models/structured_meeting.rb
@@ -32,11 +32,33 @@ class StructuredMeeting < Meeting
# triggered by MeetingAgendaItem#after_create/after_destroy/after_save
def calculate_agenda_item_time_slots
current_time = start_time
- agenda_items.order(:position).each do |top|
- start_time = current_time
- current_time += top.duration_in_minutes&.minutes || 0.minutes
- end_time = current_time
- top.update_columns(start_time:, end_time:) # avoid callbacks, infinite loop otherwise
+ MeetingAgendaItem.transaction do
+ changed_items = agenda_items.order(:position).map do |top|
+ start_time = current_time
+ current_time += top.duration_in_minutes&.minutes || 0.minutes
+ end_time = current_time
+ top.assign_attributes(start_time:, end_time:)
+ top
+ end
+
+ MeetingAgendaItem.import(
+ changed_items,
+ on_duplicate_key_update: {
+ conflict_target: [:id],
+ columns: %i[meeting_id
+ author_id
+ title
+ notes
+ position
+ duration_in_minutes
+ start_time
+ end_time
+ created_at
+ updated_at
+ work_package_id
+ item_type]
+ }
+ )
end
end
@@ -45,10 +67,10 @@ class StructuredMeeting < Meeting
end
def duration_exceeded_by_agenda_items?
- agenda_items_sum_duration_in_minutes > (duration*60 || 0)
+ agenda_items_sum_duration_in_minutes > (duration * 60)
end
def duration_exceeded_by_agenda_items_in_minutes
- agenda_items_sum_duration_in_minutes - (duration*60 || 0)
+ agenda_items_sum_duration_in_minutes - (duration * 60)
end
end
diff --git a/modules/meeting/spec/models/meeting_acts_as_journalized_spec.rb b/modules/meeting/spec/models/meeting_acts_as_journalized_spec.rb
index 8f21e9648eb..c0e5df9e411 100644
--- a/modules/meeting/spec/models/meeting_acts_as_journalized_spec.rb
+++ b/modules/meeting/spec/models/meeting_acts_as_journalized_spec.rb
@@ -280,7 +280,7 @@ RSpec.describe Meeting do
describe '#destroy' do
before do
- meeting.agenda_items << create(:meeting_agenda_item)
+ meeting.agenda_items << build(:meeting_agenda_item)
meeting.save
end