Document left_joins_messageable smell

This commit is contained in:
Jose Farias
2024-10-28 10:27:50 -06:00
parent 5cdf83163c
commit cf9b07711a
2 changed files with 2 additions and 2 deletions
+1 -2
View File
@@ -11,14 +11,13 @@ class Bubble < ApplicationRecord
scope :reverse_chronologically, -> { order created_at: :desc, id: :desc }
scope :chronologically, -> { order created_at: :asc, id: :asc }
# FIXME: Compute activity and comment count at write time so it's easier to query for.
scope :left_joins_comments, -> do
left_joins(:messages).merge(Message.left_joins_messageable(:comments))
end
scope :ordered_by_activity, -> do
left_joins_comments.select("bubbles.*, COUNT(comments.id) + bubbles.boost_count AS activity").group(:id).order("activity DESC")
end
scope :ordered_by_comments, -> do
left_joins_comments.select("bubbles.*, COUNT(comments.id) AS comment_count").group(:id).order("comment_count DESC")
end
+1
View File
@@ -5,6 +5,7 @@ class Message < ApplicationRecord
scope :chronologically, -> { order created_at: :asc, id: :desc }
# FIXME: Will be made redundant when we compute activity and comment count at write time. See commit.
scope :left_joins_messageable, ->(messageable_type) do
joins "LEFT OUTER JOIN #{messageable_type} ON messages.messageable_id = #{messageable_type}.id"
end