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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -19,6 +19,15 @@
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th><label for="search_post_tags">Post Tags</th>
|
||||
<td>
|
||||
<div class="input">
|
||||
<%= text_field "search", "post_tags_match", :value => params[:search][:post_tags_match], :data => { autocomplete: "tag-query" } %>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th><label for="search_creator_name">Creator</th>
|
||||
<td>
|
||||
|
||||
@@ -78,6 +78,18 @@ class PoolTest < ActiveSupport::TestCase
|
||||
|
||||
assert_equal([@pool1.id], Pool.search(post_id_count: 2).pluck(:id))
|
||||
end
|
||||
|
||||
should "find pools by post tags" do
|
||||
@pool1 = create(:pool, name: "pool1")
|
||||
@pool2 = create(:pool, name: "pool2")
|
||||
@post1 = create(:post, tag_string: "pool:pool1 bkub")
|
||||
@post2 = create(:post, tag_string: "pool:pool1 fumimi")
|
||||
@post3 = create(:post, tag_string: "pool:pool2 bkub fumimi")
|
||||
|
||||
assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "bkub").pluck(:id))
|
||||
assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "fumimi").pluck(:id))
|
||||
assert_equal([@pool2.id], Pool.search(post_tags_match: "bkub fumimi").pluck(:id))
|
||||
end
|
||||
end
|
||||
|
||||
context "Creating a pool" do
|
||||
|
||||
Reference in New Issue
Block a user