Merge pull request #2966 from evazion/fix-artist-autocomplete

Improve autocomplete on /artists, /wiki_pages, and /pools.
This commit is contained in:
Albert Yi
2017-04-10 14:29:05 -07:00
committed by GitHub
12 changed files with 60 additions and 34 deletions

View File

@@ -53,7 +53,9 @@
$.ajax({
url: "/artists.json",
data: {
"search[name]": "*" + req.term + "*",
"search[name]": req.term + "*",
"search[is_active]": true,
"search[order]": "post_count",
"limit": 10
},
method: "get",

View File

@@ -27,7 +27,6 @@
$.ajax({
url: "/pools.json",
data: {
"search[is_active]": "true",
"search[name_matches]": req.term,
"limit": 10
},

View File

@@ -20,8 +20,9 @@
$.ajax({
url: "/wiki_pages.json",
data: {
"search[title]": "*" + req.term + "*",
"search[title]": req.term + "*",
"search[hide_deleted]": "Yes",
"search[order]": "post_count",
"limit": 10
},
method: "get",

View File

@@ -38,7 +38,7 @@ class ArtistsController < ApplicationController
def index
search_params = params[:search].present? ? params[:search] : params
@artists = Artist.search(search_params).order("id desc").paginate(params[:page], :limit => params[:limit])
@artists = Artist.includes(:urls).search(search_params).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@artists) do |format|
format.xml do
render :xml => @artists.to_xml(:include => [:urls], :root => "artists")

View File

@@ -15,13 +15,13 @@ class WikiPagesController < ApplicationController
end
def index
@wiki_pages = WikiPage.search(params[:search]).order("updated_at desc").paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
@wiki_pages = WikiPage.search(params[:search]).paginate(params[:page], :limit => params[:limit], :search_count => params[:search])
respond_with(@wiki_pages) do |format|
format.html do
if params[:page].nil? || params[:page].to_i == 1
if @wiki_pages.count == 1
if @wiki_pages.length == 1
redirect_to(wiki_page_path(@wiki_pages.first))
elsif @wiki_pages.count == 0 && params[:search][:title].present? && params[:search][:title] !~ /\*/
elsif @wiki_pages.length == 0 && params[:search][:title].present? && params[:search][:title] !~ /\*/
redirect_to(wiki_pages_path(:search => {:title => "*#{params[:search][:title]}*"}))
end
end

View File

@@ -21,6 +21,11 @@ class Artist < ActiveRecord::Base
attr_accessible :is_active, :as => [:builder, :janitor, :moderator, :default, :admin]
attr_accessible :is_banned, :as => :admin
scope :active, lambda { where(is_active: true) }
scope :deleted, lambda { where(is_active: false) }
scope :banned, lambda { where(is_banned: true) }
scope :unbanned, lambda { where(is_banned: false) }
module UrlMethods
extend ActiveSupport::Concern
@@ -328,14 +333,6 @@ class Artist < ActiveRecord::Base
end
module SearchMethods
def active
where("is_active = true")
end
def banned
where("is_banned = true")
end
def url_matches(string)
matches = find_all_by_url(string).map(&:id)
@@ -362,33 +359,33 @@ class Artist < ActiveRecord::Base
def other_names_match(string)
if string =~ /\*/ && CurrentUser.is_builder?
where("other_names ILIKE ? ESCAPE E'\\\\'", string.to_escaped_for_sql_like)
where("artists.other_names ILIKE ? ESCAPE E'\\\\'", string.to_escaped_for_sql_like)
else
where("other_names_index @@ to_tsquery('danbooru', E?)", Artist.normalize_name(string).to_escaped_for_tsquery)
where("artists.other_names_index @@ to_tsquery('danbooru', E?)", Artist.normalize_name(string).to_escaped_for_tsquery)
end
end
def group_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like
where("group_name LIKE ? ESCAPE E'\\\\'", stripped_name)
where("artists.group_name LIKE ? ESCAPE E'\\\\'", stripped_name)
end
def name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like
where("name LIKE ? ESCAPE E'\\\\'", stripped_name)
where("artists.name LIKE ? ESCAPE E'\\\\'", stripped_name)
end
def named(name)
where("name = ?", normalize_name(name))
where(name: normalize_name(name))
end
def any_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like
if name =~ /\*/ && CurrentUser.is_builder?
where("(name LIKE ? ESCAPE E'\\\\' OR other_names LIKE ? ESCAPE E'\\\\')", stripped_name, stripped_name)
where("(artists.name LIKE ? ESCAPE E'\\\\' OR artists.other_names LIKE ? ESCAPE E'\\\\')", stripped_name, stripped_name)
else
name_for_tsquery = normalize_name(name).to_escaped_for_tsquery
where("(name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', E?))", stripped_name, name_for_tsquery)
where("(artists.name LIKE ? ESCAPE E'\\\\' OR artists.other_names_index @@ to_tsquery('danbooru', E?))", stripped_name, name_for_tsquery)
end
end
@@ -413,7 +410,7 @@ class Artist < ActiveRecord::Base
q = q.banned
when /status:active/
q = q.where("is_banned = false and is_active = true")
q = q.unbanned.active
when /./
q = q.any_name_matches(params[:name])
@@ -422,23 +419,25 @@ class Artist < ActiveRecord::Base
params[:order] ||= params.delete(:sort)
case params[:order]
when "name"
q = q.reorder("name")
q = q.order("artists.name")
when "updated_at"
q = q.reorder("updated_at desc")
q = q.order("artists.updated_at desc")
when "post_count"
q = q.joins(:tag).order("tags.post_count desc")
else
q = q.reorder("id desc")
q = q.order("artists.id desc")
end
if params[:is_active] == "true"
q = q.active
elsif params[:is_active] == "false"
q = q.where("is_active = false")
q = q.deleted
end
if params[:is_banned] == "true"
q = q.banned
elsif params[:is_banned] == "false"
q = q.where("is_banned = false")
q = q.unbanned
end
if params[:id].present?

View File

@@ -45,9 +45,9 @@ class Pool < ActiveRecord::Base
end
def name_matches(name)
name = name.tr(" ", "_")
name = normalize_name_for_search(name)
name = "*#{name}*" unless name =~ /\*/
where("name ilike ? escape E'\\\\'", name.to_escaped_for_sql_like)
where("lower(name) like ? escape E'\\\\'", name.to_escaped_for_sql_like)
end
def search(params)
@@ -139,6 +139,10 @@ class Pool < ActiveRecord::Base
name.gsub(/\s+/, "_")
end
def self.normalize_name_for_search(name)
normalize_name(name).mb_chars.downcase
end
def self.normalize_post_ids(post_ids, unique)
hoge = post_ids.scan(/\d+/)
if unique

View File

@@ -79,10 +79,15 @@ class WikiPage < ActiveRecord::Base
end
params[:order] ||= params.delete(:sort)
if params[:order] == "time" || params[:order] == "Date"
case params[:order]
when "time"
q = q.order("updated_at desc")
elsif params[:order] == "title" || params[:order] == "Name"
when "title"
q = q.order("title")
when "post_count"
q = q.joins(:tag).order("tags.post_count desc")
else
q = q.order("updated_at desc")
end
q

View File

@@ -15,7 +15,7 @@
<th><label for="search_order">Order</label>
<td>
<div class="input">
<%= select "search", "order", [["Recently created", "created_at"], ["Last updated", "updated_at"], ["Name", "name"]], :selected => params[:search][:order] %>
<%= select "search", "order", [["Recently created", "created_at"], ["Last updated", "updated_at"], ["Name", "name"], ["Post count", "post_count"]], :selected => params[:search][:order] %>
</div>
</td>
</tr>

View File

@@ -13,7 +13,7 @@
<div class="input">
<label for="search_order">Order</label>
<%= select "search", "order", ["Name", "Date"] %>
<%= select "search", "order", [%w[Name title], %w[Date time], %w[Posts post_count]] %>
</div>
<div class="input">

View File

@@ -0,0 +1,7 @@
class AddUpdatedAtIndexOnWikiPages < ActiveRecord::Migration
def change
WikiPage.without_timeout do
add_index :wiki_pages, :updated_at
end
end
end

View File

@@ -7217,6 +7217,13 @@ CREATE UNIQUE INDEX index_wiki_pages_on_title ON wiki_pages USING btree (title);
CREATE INDEX index_wiki_pages_on_title_pattern ON wiki_pages USING btree (title text_pattern_ops);
--
-- Name: index_wiki_pages_on_updated_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_wiki_pages_on_updated_at ON wiki_pages USING btree (updated_at);
--
-- Name: unique_schema_migrations; Type: INDEX; Schema: public; Owner: -
--
@@ -7562,3 +7569,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170316224630');
INSERT INTO schema_migrations (version) VALUES ('20170319000519');
INSERT INTO schema_migrations (version) VALUES ('20170329185605');