From a181e6d0db0f43f88625b9cc2663cc1c7d89cdca Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 23 Feb 2020 00:20:09 -0600 Subject: [PATCH] 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 --- app/controllers/tags_controller.rb | 1 + app/models/tag.rb | 12 ++++++++++++ app/views/tags/_search.html.erb | 2 +- app/views/tags/index.html.erb | 7 +++++++ config/initializers/core_extensions.rb | 5 +++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 4a3cd5665..3ba2ca844 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -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 diff --git a/app/models/tag.rb b/app/models/tag.rb index f8ea67f69..50e2013ae 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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 diff --git a/app/views/tags/_search.html.erb b/app/views/tags/_search.html.erb index 3e83c5b18..a4fb623b6 100644 --- a/app/views/tags/_search.html.erb +++ b/app/views/tags/_search.html.erb @@ -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] %> diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index 20cd6286c..86b138ee0 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -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) %> | diff --git a/config/initializers/core_extensions.rb b/config/initializers/core_extensions.rb index 37d839e6d..61f85f9d0 100644 --- a/config/initializers/core_extensions.rb +++ b/config/initializers/core_extensions.rb @@ -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