Temporarily wire up filtering on the form

This commit is contained in:
Kevin McConnell
2024-10-03 14:40:51 +01:00
parent 4161862966
commit b7feddf6b7
5 changed files with 26 additions and 2 deletions
+5
View File
@@ -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]
+9
View File
@@ -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
+1 -1
View File
@@ -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
+3 -1
View File
@@ -105,7 +105,9 @@
<section class="bubbles-list unpad-inline center margin-block-start">
<header class="flex-inline align-center center gap txt-small">
<input type="search" class="input center flex-inline" placeholder="Type to filter…">
<form>
<input type="search" name="filter" class="input center flex-inline" placeholder="Type to filter…" value="<%= params[:filter] %>">
</form>
</header>
<ul class="unpad margin-none flex flex-column txt-align-start center">
<%= render partial: "bubbles/list/bubble", collection: @bubbles %>
+8
View File
@@ -6,4 +6,12 @@ class BubbleTest < ActiveSupport::TestCase
assert_includes Bubble.search("haggis"), bubble
end
test "mentioning" do
bubble = buckets(:writebook).bubbles.create! title: "Insufficient haggis", creator: users(:kevin)
bubbles(:logo).comments.create! body: "I hate haggis", creator: users(:kevin)
bubbles(:text).comments.create! body: "I love haggis", creator: users(:kevin)
assert_equal [ bubble, bubbles(:logo), bubbles(:text) ].sort, Bubble.mentioning("haggis").sort
end
end