Include aliased tags in autocomplete

This commit is contained in:
Toks
2015-04-03 19:31:46 -04:00
parent d59b442f38
commit 558fd4d8ef
8 changed files with 65 additions and 4 deletions

View File

@@ -173,11 +173,9 @@
}
$.ajax({
url: "/tags.json",
url: "/tags/autocomplete.json",
data: {
"search[order]": "count",
"search[name_matches]": term + "*",
"limit": 10
},
method: "get",
success: function(data) {
@@ -185,6 +183,7 @@
return {
type: "tag",
label: tag.name.replace(/_/g, " "),
antecedent: tag.antecedent_name,
value: tag.name,
category: tag.category,
post_count: tag.post_count
@@ -202,7 +201,16 @@
}
Danbooru.Autocomplete.render_item = function(list, item) {
var $link = $("<a/>").text(item.label);
var $link = $("<a/>");
if (item.antecedent) {
var antecedent = item.antecedent.replace(/_/g, " ");
var arrow = $("<span/>").html(" &rarr; ").addClass("autocomplete-arrow");
$link.append(document.createTextNode(antecedent));
$link.append(arrow);
}
$link.append(document.createTextNode(item.label));
$link.attr("href", "/posts?tags=" + encodeURIComponent(item.value));
$link.click(function(e) {
e.preventDefault();

View File

@@ -5,4 +5,8 @@
.ui-menu-item a {
padding: 1px .2em;
}
.autocomplete-arrow {
color: black;
}
}

View File

@@ -17,6 +17,16 @@ class TagsController < ApplicationController
end
end
def autocomplete
@tags = Tag.names_matches_with_aliases(params[:search][:name_matches])
respond_with(@tags) do |format|
format.xml do
render :xml => @tags.to_xml(:root => "tags")
end
end
end
def search
end

View File

@@ -12,5 +12,6 @@ class DailyMaintenance
TagSubscription.process_all
ApiCacheGenerator.new.generate_tag_cache
ForumSubscription.process_all!
TagAlias.update_cached_post_counts_for_all
end
end

View File

@@ -673,6 +673,23 @@ class Tag < ActiveRecord::Base
q
end
def names_matches_with_aliases(name)
query1 = Tag.select("tags.name, tags.post_count, tags.category, null AS antecedent_name")
.search(:name_matches => name, :order => "count").limit(10)
name = name.mb_chars.downcase.to_escaped_for_sql_like
query2 = TagAlias.select("tags.name, tag_aliases.post_count, tags.category, tag_aliases.antecedent_name")
.joins("INNER JOIN tags ON tags.name = tag_aliases.consequent_name")
.where("tag_aliases.antecedent_name LIKE ? ESCAPE E'\\\\'", name)
.where("tags.name NOT LIKE ? ESCAPE E'\\\\'", name)
.where("tag_aliases.post_count > 0")
.order("tag_aliases.post_count desc")
.limit(20) # Get 20 records even though only 10 will be displayed in case some duplicates get filtered out.
sql_query = "((#{query1.to_sql}) UNION ALL (#{query2.to_sql})) AS unioned_query"
Tag.select("DISTINCT ON (name, post_count) *").from(sql_query).order("post_count desc").limit(10)
end
end
def editable_by?(user)

View File

@@ -254,4 +254,8 @@ class TagAlias < ActiveRecord::Base
update_forum_topic_for_reject
destroy
end
def self.update_cached_post_counts_for_all
execute_sql("UPDATE tag_aliases SET post_count = tags.post_count FROM tags WHERE tags.name = tag_aliases.consequent_name")
end
end

View File

@@ -213,6 +213,9 @@ Rails.application.routes.draw do
resource :source, :only => [:show]
resources :tags do
resource :correction, :only => [:new, :create, :show], :controller => "tag_corrections"
collection do
get :autocomplete
end
end
resources :tag_aliases do
resource :correction, :controller => "tag_alias_corrections"

View File

@@ -0,0 +1,14 @@
class AddAntecedentNamePatternIndexAndPostCountToTagAliases < ActiveRecord::Migration
def up
execute "set statement_timeout = 0"
execute "create index index_tag_aliases_on_antecedent_name_pattern on tag_aliases (antecedent_name text_pattern_ops)"
add_column :tag_aliases, :post_count, :integer, :null => false, :default => 0
add_index :tag_aliases, :post_count
end
def down
execute "set statement_timeout = 0"
execute "drop index index_tag_aliases_on_antecedent_name_pattern"
remove_column :tag_aliases, :post_count
end
end