Introduce a Bubble.tagged_with scope to simplify controller finding

This commit is contained in:
Jeffrey Hardy
2024-10-02 15:23:30 -04:00
parent d3748bd495
commit 889dfdd06a
3 changed files with 13 additions and 6 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ class BubblesController < ApplicationController
if params[:tag_id]
@tag = Current.account.tags.find(params[:tag_id])
@bubbles = @bubbles.joins(:tags).where(tags: @tag)
@most_active_bubbles = @most_active_bubbles.joins(:tags).where(tags: @tag)
@bubbles = @bubbles.tagged_with(@tag)
@most_active_bubbles = @most_active_bubbles.tagged_with(@tag)
end
end
+1 -4
View File
@@ -1,5 +1,5 @@
class Bubble < ApplicationRecord
include Assignable, Colored, Searchable
include Assignable, Colored, Searchable, Taggable
belongs_to :bucket
belongs_to :creator, class_name: "User", default: -> { Current.user }
@@ -7,9 +7,6 @@ class Bubble < ApplicationRecord
has_many :comments, dependent: :destroy
has_many :boosts, dependent: :destroy
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
has_one_attached :image, dependent: :purge_later
scope :reverse_chronologically, -> { order(created_at: :desc, id: :desc) }
+10
View File
@@ -0,0 +1,10 @@
module Bubble::Taggable
extend ActiveSupport::Concern
included do
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
scope :tagged_with, ->(tag) { joins(:taggings).where(taggings: { tag: tag }) }
end
end