From 49a3e569c21954562e4cac6c10a95ff268be500b Mon Sep 17 00:00:00 2001 From: Jeffrey Hardy Date: Wed, 2 Oct 2024 09:08:00 -0400 Subject: [PATCH] Use chained scopes --- app/controllers/bubbles_controller.rb | 10 +++++----- app/models/bubble.rb | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/controllers/bubbles_controller.rb b/app/controllers/bubbles_controller.rb index d6054e15d..f6e73ba92 100644 --- a/app/controllers/bubbles_controller.rb +++ b/app/controllers/bubbles_controller.rb @@ -4,13 +4,13 @@ class BubblesController < ApplicationController before_action :set_bubble, only: %i[ show edit update ] def index + @bubbles = @bucket.bubbles.reverse_chronologically + @most_active_bubbles = @bucket.bubbles.ordered_by_activity.limit(10) + if params[:tag_id] @tag = Current.account.tags.find(params[:tag_id]) - @bubbles = @tag.bubbles - @most_active_bubbles = @tag.bubbles.left_joins(:comments, :boosts).group(:id).order(Arel.sql("COUNT(comments.id) + COUNT(boosts.id) DESC")).limit(10) - else - @bubbles = @bucket.bubbles.order(created_at: :desc) - @most_active_bubbles = @bucket.bubbles.left_joins(:comments, :boosts).group(:id).order(Arel.sql("COUNT(comments.id) + COUNT(boosts.id) DESC")).limit(10) + @bubbles = @bubbles.joins(:tags).where(tags: @tag) + @most_active_bubbles = @most_active_bubbles.joins(:tags).where(tags: @tag) end end diff --git a/app/models/bubble.rb b/app/models/bubble.rb index c93c8680c..07277955b 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -15,5 +15,8 @@ class Bubble < ApplicationRecord has_one_attached :image, dependent: :purge_later + scope :reverse_chronologically, -> { order(created_at: :desc, id: :desc) } + scope :ordered_by_activity, -> { left_joins(:comments, :boosts).group(:id).order(Arel.sql("COUNT(comments.id) + COUNT(boosts.id) DESC")) } + searchable_by :title, using: :bubbles_search_index end