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

use left outer join instead of separately fetching favorites

Ivan Kuchin06cae5157587
diff --git a/app/menus/projects/menu.rb b/app/menus/projects/menu.rb
index fd31bab939c..9a8b1825dfe 100644
--- a/app/menus/projects/menu.rb
+++ b/app/menus/projects/menu.rb
@@ -120,12 +120,14 @@ module Projects
end
def persisted_filters
- @persisted_filters = ::ProjectQuery.visible(current_user).order(:name)
- .partition { |query| query.id.in?(favored_ids) }.flatten
+ @persisted_filters = ::ProjectQuery
+ .visible(current_user)
+ .with_favored_by_user(current_user)
+ .order(favored: :desc, name: :asc)
end
def favored_ids
- @favored_ids = ::ProjectQuery.favored_by(current_user).pluck(:id).to_set
+ @favored_ids = persisted_filters.select(&:favored).to_set(&:id)
end
def modification_params?
diff --git a/lib_static/open_project/acts/favorable.rb b/lib_static/open_project/acts/favorable.rb
index 5a4073c2d27..0a94279f6d7 100644
--- a/lib_static/open_project/acts/favorable.rb
+++ b/lib_static/open_project/acts/favorable.rb
@@ -45,7 +45,7 @@ module OpenProject
#
# acts_as_favorable expects that the including module defines a +visible?(user)+ method,
# as it's used to identify whether a user can actually favorite the object.
- def acts_as_favorable
+ def acts_as_favorable # rubocop:disable Metrics/AbcSize
return if included_modules.include?(InstanceMethods)
class_eval do
@@ -58,6 +58,21 @@ module OpenProject
includes(:favorites)
.where(favorites: { user_id: })
}
+
+ scope :with_favored_by_user, ->(user) {
+ favorite = ::Favorite.arel_table
+
+ join = arel_table
+ .join(favorite, Arel::Nodes::OuterJoin)
+ .on(
+ favorite[:favored_type].eq(base_class.name),
+ favorite[:favored_id].eq(arel_table[:id]),
+ favorite[:user_id].eq(user.id)
+ )
+ .join_sources
+
+ select(arel_table[Arel.star], "(favorites.id IS NOT NULL) AS favored").joins(join)
+ }
end
Registry.add(self)