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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
enhanced and used eager loading wrapper in order to avoid n+1, added further rendering optimizations
Jonas Jabari6c6e7ca5b0ce
diff --git a/app/components/work_packages/activities_tab/journals/index_component.rb b/app/components/work_packages/activities_tab/journals/index_component.rbindex 6d2022b92b9..469f0e3d4e1 100644--- a/app/components/work_packages/activities_tab/journals/index_component.rb+++ b/app/components/work_packages/activities_tab/journals/index_component.rb@@ -63,34 +63,48 @@ module WorkPackagesjournal_sorting == "desc"end- def journals+ def base_journalswork_package.journals- .includes(:user, :notifications)+ .includes(+ :user,+ :customizable_journals,+ :attachable_journals,+ :storable_journals,+ :notifications+ ).reorder(version: journal_sorting).with_sequence_versionend+ def journals+ API::V3::Activities::ActivityEagerLoadingWrapper.wrap(base_journals)+ end+def recent_journals- if journal_sorting_desc?- journals.first(MAX_RECENT_JOURNALS)- else- journals.last(MAX_RECENT_JOURNALS)- end+ recent_ones = if journal_sorting_desc?+ base_journals.first(MAX_RECENT_JOURNALS)+ else+ base_journals.last(MAX_RECENT_JOURNALS)+ end++ API::V3::Activities::ActivityEagerLoadingWrapper.wrap(recent_ones)enddef older_journals- if journal_sorting_desc?- journals.offset(MAX_RECENT_JOURNALS)- else- total = journals.count- limit = [total - MAX_RECENT_JOURNALS, 0].max- journals.limit(limit)- end+ older_ones = if journal_sorting_desc?+ base_journals.offset(MAX_RECENT_JOURNALS)+ else+ total = base_journals.count+ limit = [total - MAX_RECENT_JOURNALS, 0].max+ base_journals.limit(limit)+ end++ API::V3::Activities::ActivityEagerLoadingWrapper.wrap(older_ones)enddef journal_with_notes- journals.where.not(notes: "")+ base_journals.where.not(notes: "")enddef wp_journals_grouped_emoji_reactionsdiff --git a/app/components/work_packages/activities_tab/journals/item_component.rb b/app/components/work_packages/activities_tab/journals/item_component.rbindex 0030013b1fd..af733a7474c 100644--- a/app/components/work_packages/activities_tab/journals/item_component.rb+++ b/app/components/work_packages/activities_tab/journals/item_component.rb@@ -75,7 +75,7 @@ module WorkPackagesenddef has_unread_notifications?- journal.notifications.where(read_ian: false, recipient_id: User.current.id).any?+ journal.has_unread_notifications_for_user?(User.current)enddef notification_on_details?diff --git a/app/components/work_packages/activities_tab/journals/item_component/details.html.erb b/app/components/work_packages/activities_tab/journals/item_component/details.html.erbindex 2bed453e20e..8a8c6c7c595 100644--- a/app/components/work_packages/activities_tab/journals/item_component/details.html.erb+++ b/app/components/work_packages/activities_tab/journals/item_component/details.html.erb@@ -14,12 +14,12 @@when :only_commentsrender_empty_line(details_container) unless journal.notes.blank? && !journal.noop?when :only_changes- if journal.details.any?+ if has_details?render_details_header(details_container)render_details(details_container)endelse- if journal.details.any?+ if has_details?render_details_header(details_container) unless journal.notes.present?render_details(details_container)elsif !journal.notes.present?diff --git a/app/components/work_packages/activities_tab/journals/item_component/details.rb b/app/components/work_packages/activities_tab/journals/item_component/details.rbindex 59f0c5ed4fb..529fbcd6ab9 100644--- a/app/components/work_packages/activities_tab/journals/item_component/details.rb+++ b/app/components/work_packages/activities_tab/journals/item_component/details.rb@@ -53,6 +53,14 @@ module WorkPackagesjournal.idend+ def journal_details+ @journal_details ||= journal.details+ end++ def has_details?+ @has_details ||= journal_details.any?+ end+def render_details_header(details_container)details_container.with_row(flex_layout: true,@@ -223,7 +231,7 @@ module WorkPackagesenddef render_journal_details(details_container_inner)- journal.details.each do |detail|+ journal_details.each do |detail|rendered_detail = journal.render_detail(detail)render_single_detail(details_container_inner, rendered_detail) if rendered_detail.present?enddiff --git a/app/models/journal.rb b/app/models/journal.rbindex 04f66834916..95da1c3ed52 100644--- a/app/models/journal.rb+++ b/app/models/journal.rb@@ -177,6 +177,16 @@ class Journal < ApplicationRecordcause_type.present?end+ def has_unread_notifications_for_user?(user)+ # we optionally set the instance variable @unread_notifications in the ActivityEagerLoadingWrapper+ # in order to avoid N+1 queries+ if instance_variable_defined?(:@unread_notifications)+ @unread_notifications&.any? { |notification| notification.recipient_id == user.id }+ else+ notifications.where(read_ian: false, recipient_id: user.id).any?+ end+ end+privatedef has_file_links?diff --git a/lib/api/v3/activities/activity_eager_loading_wrapper.rb b/lib/api/v3/activities/activity_eager_loading_wrapper.rbindex b21a7abc355..8b7f20a6123 100644--- a/lib/api/v3/activities/activity_eager_loading_wrapper.rb+++ b/lib/api/v3/activities/activity_eager_loading_wrapper.rb@@ -36,6 +36,7 @@ module APIset_journable(journals)set_predecessor(journals)set_data(journals)+ set_notifications(journals)endsuper@@ -72,6 +73,20 @@ module APIendend+ def set_notifications(journals)+ notifications = Notification+ .where(journal_id: journals.map(&:id))+ .where(read_ian: false)+ .group_by(&:journal_id)++ journals.each do |journal|+ journal.instance_variable_set(+ :@unread_notifications,+ notifications[journal.id]+ )+ end+ end+def journable_by_type_and_id(journals)journals.group_by(&:journable_type)