Introduce a Bubble.tagged_with scope to simplify controller finding
This commit is contained in:
@@ -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,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) }
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user