Fix #1898: Include alias predicates in non-empty Tags search.

Make searches on the /tags index includes aliases too. Show matching
aliases like this:

   Name: gray*

   ? 75098 grey_hair <- gray_hair
   ? 35345 grey_eyes <- gray_eyes
This commit is contained in:
evazion
2020-02-23 00:20:09 -06:00
parent a8e5412d9c
commit a181e6d0db
5 changed files with 26 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ class TagsController < ApplicationController
def index
@tags = Tag.paginated_search(params)
@tags = @tags.includes(:consequent_aliases) if request.format.html?
respond_with(@tags)
end

View File

@@ -816,6 +816,14 @@ class Tag < ApplicationRecord
where_like(:name, normalize_name(name))
end
def alias_matches(name)
where(name: TagAlias.active.where_ilike(:antecedent_name, normalize_name(name)).select(:consequent_name))
end
def name_or_alias_matches(name)
name_matches(name).or(alias_matches(name))
end
def search(params)
q = super
@@ -833,6 +841,10 @@ class Tag < ApplicationRecord
q = q.where("tags.name": normalize_name(params[:name_normalize]).split(","))
end
if params[:name_or_alias_matches].present?
q = q.name_or_alias_matches(params[:name_or_alias_matches])
end
if params[:hide_empty].blank? || params[:hide_empty].to_s.truthy?
q = q.where("post_count > 0")
end

View File

@@ -1,5 +1,5 @@
<%= search_form_for(tags_path) do |f| %>
<%= f.input :name_matches, label: "Name", hint: "Use * for wildcard", input_html: { value: params[:search][:name_matches], data: { autocomplete: "tag" } } %>
<%= f.input :name_or_alias_matches, label: "Name", hint: "Use * for wildcard", input_html: { value: params[:search][:name_or_alias_matches], data: { autocomplete: "tag" } } %>
<%= f.input :category, label: "Category", collection: TagCategory.canonical_mapping.to_a, include_blank: true,selected: params[:search][:category] %>
<%= f.input :order, collection: [%w[Newest date], %w[Count count], %w[Name name]], include_blank: false, selected: params[:search][:order] %>
<%= f.input :hide_empty, label: "Hide empty?", collection: %w[yes no], selected: params[:search][:hide_empty] %>

View File

@@ -9,6 +9,13 @@
<% t.column "Name", td: {class: "col-expand"} do |tag| %>
<%= link_to_wiki "?", tag.name, class: tag_class(tag) %>
<%= link_to tag.name, posts_path(tags: tag.name), class: tag_class(tag) %>
<% pattern = params.dig(:search, :name_or_alias_matches).to_s %>
<% tag_alias = tag.consequent_aliases.find { |tag_alias| tag_alias.antecedent_name.ilike?(pattern) } %>
<% if tag_alias.present? %>
← <%= link_to tag_alias.antecedent_name, tag_alias, class: "fineprint" %>
<% end %>
<% end %>
<% t.column column: "control" do |tag| %>
<%= link_to_if tag.editable_by?(CurrentUser.user), "Edit", edit_tag_path(tag) %> |

View File

@@ -31,6 +31,11 @@ module Danbooru
def falsy?
self.match?(/\A(false|f|no|n|off|0)\z/i)
end
def ilike?(pattern)
pattern = Regexp.escape(pattern).gsub(/\\\*/, ".*")
match?(/\A#{pattern}\z/i)
end
end
end
end