Fix comments-dependent scopes

This commit is contained in:
Jose Farias
2024-10-27 19:50:06 -06:00
parent 3bb6419c08
commit 2fb6ed76b3
3 changed files with 21 additions and 3 deletions
+8 -3
View File
@@ -11,11 +11,16 @@ class Bubble < ApplicationRecord
scope :reverse_chronologically, -> { order created_at: :desc, id: :desc }
scope :chronologically, -> { order created_at: :asc, id: :asc }
scope :ordered_by_activity, -> do
left_joins(:messages).merge(Message.comments).group(:id).order(Arel.sql("COUNT(messages.id) + boost_count DESC"))
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) + boost_count AS activity").group(:id).order("activity DESC")
end
scope :ordered_by_comments, -> do
left_joins(:messages).merge(Message.comments).group(:id).order("COUNT(messages.id) DESC")
left_joins_comments.select("bubbles.*, COUNT(comments.id) AS comment_count").group(:id).order("comment_count DESC")
end
scope :with_status, ->(status) do
+4
View File
@@ -4,4 +4,8 @@ class Message < ApplicationRecord
delegated_type :messageable, types: Messageable::TYPES, inverse_of: :message, dependent: :destroy
scope :chronologically, -> { order created_at: :asc, id: :desc }
scope :left_joins_messageable, ->(messageable_type) do
joins "LEFT OUTER JOIN #{messageable_type} ON messages.messageable_id = #{messageable_type}.id"
end
end
+9
View File
@@ -24,6 +24,15 @@ class BubbleTest < ActiveSupport::TestCase
assert_includes Bubble.search("haggis"), bubble
end
test "ordering by activity" do
bubbles(:layout).update! boost_count: 1_000
assert_equal bubbles(:layout, :logo, :shipping, :text), Bubble.ordered_by_activity.to_a
end
test "ordering by comments" do
assert_equal bubbles(:logo, :layout, :shipping, :text), Bubble.ordered_by_comments.to_a
end
test "mentioning" do
bubble = buckets(:writebook).bubbles.create! title: "Insufficient haggis", creator: users(:kevin)
bubbles(:logo).capture Comment.new(body: "I hate haggis")