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

@@ -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