From b7feddf6b78019bc7b8cc260c94da52c9eb9a4ba Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Thu, 3 Oct 2024 14:40:51 +0100 Subject: [PATCH] Temporarily wire up filtering on the form --- app/controllers/bubbles_controller.rb | 5 +++++ app/models/bubble.rb | 9 +++++++++ app/models/concerns/searchable.rb | 2 +- app/views/bubbles/index.html.erb | 4 +++- test/models/bubble_test.rb | 8 ++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/controllers/bubbles_controller.rb b/app/controllers/bubbles_controller.rb index 9f19ba510..8b8d1f47b 100644 --- a/app/controllers/bubbles_controller.rb +++ b/app/controllers/bubbles_controller.rb @@ -5,6 +5,11 @@ class BubblesController < ApplicationController def index @bubbles = @bucket.bubbles.not_popped.reverse_chronologically + + if params[:filter].present? + @bubbles = @bubbles.mentioning(params[:filter]) + end + @most_active_bubbles = @bubbles.ordered_by_activity.limit(10) if params[:tag_id] diff --git a/app/models/bubble.rb b/app/models/bubble.rb index c4a0d780f..68468f821 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -13,4 +13,13 @@ class Bubble < ApplicationRecord 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 + + scope :mentioning, ->(query) do + bubbles = search(query).select(:id).to_sql + comments = Comment.search(query).select(:bubble_id).to_sql + + left_joins(:comments) + .where("bubbles.id in (#{bubbles}) or comments.bubble_id in (#{comments})") + .distinct + end end diff --git a/app/models/concerns/searchable.rb b/app/models/concerns/searchable.rb index b3c120862..125deb54e 100644 --- a/app/models/concerns/searchable.rb +++ b/app/models/concerns/searchable.rb @@ -10,7 +10,7 @@ module Searchable after_update_commit :update_in_search_index after_destroy_commit :remove_from_search_index - scope :search, ->(query) { joins("join #{using} idx on id = idx.rowid").where("idx.#{field} match ?", query) } + scope :search, ->(query) { joins("join #{using} idx on #{table_name}.id = idx.rowid").where("idx.#{field} match ?", query) } end end diff --git a/app/views/bubbles/index.html.erb b/app/views/bubbles/index.html.erb index 19bcaff5f..ef94e45c0 100644 --- a/app/views/bubbles/index.html.erb +++ b/app/views/bubbles/index.html.erb @@ -105,7 +105,9 @@
- +
+ +