From 886ee169113031c610f114243ce1570cdda09322 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 6 Sep 2019 17:54:07 -0500 Subject: [PATCH] pools: allow searching pools by post tags. Find all pools containing at least one post tagged 'translated': https://danbooru.donmai.us/pools?search[post_tags_match]=translated --- app/models/pool.rb | 9 +++++++++ app/views/pools/_search.html.erb | 9 +++++++++ test/unit/pool_test.rb | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/app/models/pool.rb b/app/models/pool.rb index 1517da54e..c4b0d6402 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -50,6 +50,11 @@ class Pool < ApplicationRecord where_ilike(:name, name) end + def post_tags_match(query) + posts = Post.tag_match(query).select(:id).reorder(nil) + joins("CROSS JOIN unnest(post_ids) AS post_id").group(:id).where("post_id IN (?)", posts) + end + def default_order order(updated_at: :desc) end @@ -60,6 +65,10 @@ class Pool < ApplicationRecord q = q.search_attributes(params, :creator, :is_active, :is_deleted, :name, :description, :post_ids) q = q.text_attribute_matches(:description, params[:description_matches]) + if params[:post_tags_match] + q = q.post_tags_match(params[:post_tags_match]) + end + if params[:name_matches].present? q = q.name_matches(params[:name_matches]) end diff --git a/app/views/pools/_search.html.erb b/app/views/pools/_search.html.erb index b5a11bc1d..15f817d3f 100644 --- a/app/views/pools/_search.html.erb +++ b/app/views/pools/_search.html.erb @@ -19,6 +19,15 @@ + +