work on refactoring search

This commit is contained in:
albert
2013-01-08 15:53:21 -05:00
parent 80f34d08d9
commit 13271e9bf5
19 changed files with 152 additions and 45 deletions

View File

@@ -14,10 +14,8 @@ class Artist < ActiveRecord::Base
accepts_nested_attributes_for :wiki_page
attr_accessible :body, :name, :url_string, :other_names, :group_name, :wiki_page_attributes, :notes, :is_active, :as => [:member, :privileged, :contributor, :janitor, :moderator, :default, :admin]
attr_accessible :is_banned, :as => :admin
scope :url_match, lambda {|string| where(["id in (?)", Artist.find_all_by_url(string).map(&:id)])}
scope :other_names_match, lambda {|string| where(["other_names_index @@ to_tsquery('danbooru', ?)", Artist.normalize_name(string)])}
scope :name_equals, lambda {|string| where("name = ?", string)}
search_methods :url_match, :other_names_match
scope :active, where("is_active = true")
scope :banned, where("is_banned = true")
module UrlMethods
extend ActiveSupport::Concern
@@ -192,6 +190,71 @@ class Artist < ActiveRecord::Base
end
end
module SearchMethods
extend ActiveSupport::Concern
module ClassMethods
def url_matches(string)
matches = find_all_by_url(string).map(&:id)
if matches.any?
where("id in (?)", matches)
else
where("false")
end
end
def other_names_match(string)
where("other_names_index @@ to_tsquery('danbooru', ?)", Artist.normalize_name(string))
end
def group_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like
where("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)
end
def any_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like
where("(name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', ?))", stripped_name, normalize_name(name))
end
def search(params)
q = active
case params[:name]
when /^http/
q = q.url_matches(params[:name])
when /name:(.+)/
q = q.name_matches($1)
when /other:(.+)/
q = q.other_names_match($1)
when /group:(.+)/
q = q.group_name_matches($1)
when /status:banned/
q = q.banned
when /./
q = q.any_name_matches(params[:name])
end
if params[:id]
q = q.where("id = ?", params[:id])
end
q
end
end
end
include UrlMethods
include NameMethods
include GroupMethods
@@ -200,6 +263,7 @@ class Artist < ActiveRecord::Base
include NoteMethods
include TagMethods
include BanMethods
include SearchMethods
def status
if is_banned?

View File

@@ -2,6 +2,16 @@ class ArtistVersion < ActiveRecord::Base
belongs_to :updater
belongs_to :artist
def self.search(params)
q = scoped
if params[:artist_id]
q = q.where("artist_id = ?", params[:artist_id].to_i)
end
q
end
def updater_name
User.id_to_name(updater_id).tr("_", " ")
end

View File

@@ -11,6 +11,20 @@ class Ban < ActiveRecord::Base
exists?(["user_id = ? AND expires_at > ?", user.id, Time.now])
end
def self.search(params)
q = scoped
if params[:banner_name]
q = q.where("banner_id = (select _.id from users _ where lower(_.name) = ?)", params[:banner_name].downcase)
end
if params[:user_name]
q = q.where("user_id = (select _.id from users _ where lower(_.name) = ?)", params[:user_name].downcase)
end
q
end
def initialize_banner_id
self.banner_id = CurrentUser.id
end

View File

@@ -10,15 +10,57 @@ class Comment < ActiveRecord::Base
attr_accessor :do_not_bump_post
scope :recent, :order => "comments.id desc", :limit => 6
scope :body_matches, lambda {|query| where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")}
scope :hidden, lambda {|user| where("score < ?", user.comment_threshold)}
scope :visible, lambda {|user| where("score >= ?", user.comment_threshold)}
scope :post_tag_match, lambda {|query| joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)}
scope :for_user, lambda {|user_id| where("creator_id = ?", user_id)}
scope :for_user_name, lambda {|user_name| where("creator_id = (select _.id from users _ where lower(_.name) = lower(?))", user_name)}
search_methods :body_matches, :post_tag_match, :for_user_name
module SearchMethods
extend ActiveSupport::Concern
module ClassMethods
def body_matches(query)
where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")
end
def hidden(user)
where("score < ?", user.comment_threshold)
end
def visible(user)
where("score >= ?", user.comment_threshold)
end
def post_tags_match(query)
joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)
end
def for_creator(user_id)
where("creator_id = ?", user_id)
end
def for_creator_name(user_name)
where("creator_id = (select _.id from users _ where lower(_.name) = lower(?))", user_name)
end
def search(params)
q = scoped
if params[:body_matches]
q = q.body_matches(params[:body_matches])
end
if params[:post_tags_match]
q = q.post_tags_match(params[:post_tags_match])
end
if params[:creator_name]
q = q.for_user_name(params[:creator_name])
end
q
end
end
end
include SearchMethods
def initialize_creator
self.creator_id = CurrentUser.user.id
self.ip_addr = CurrentUser.ip_addr

View File

@@ -20,7 +20,6 @@ class Dmail < ActiveRecord::Base
scope :visible, lambda {where("owner_id = ?", CurrentUser.id)}
scope :to_name_matches, lambda {|name| where("to_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)}
scope :from_name_matches, lambda {|name| where("from_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)}
search_method :to_name_matches, :from_name_matches
module AddressMethods
def to_name

View File

@@ -13,7 +13,6 @@ class ForumPost < ActiveRecord::Base
scope :body_matches, lambda {|body| where(["forum_posts.text_index @@ plainto_tsquery(?)", body])}
scope :for_user, lambda {|user_id| where("forum_posts.creator_id = ?", user_id)}
scope :active, where("is_deleted = false")
search_methods :body_matches
def self.new_reply(params)
if params[:topic_id]

View File

@@ -12,7 +12,6 @@ class ForumTopic < ActiveRecord::Base
validates_associated :original_post
scope :title_matches, lambda {|title| where(["text_index @@ plainto_tsquery(?)", title])}
scope :active, where("is_deleted = false")
search_methods :title_matches
accepts_nested_attributes_for :original_post
def editable_by?(user)

View File

@@ -15,7 +15,6 @@ class Note < ActiveRecord::Base
scope :active, where("is_active = TRUE")
scope :body_matches, lambda {|query| where("body_index @@ plainto_tsquery(?)", query.scan(/\S+/).join(" & "))}
scope :post_tag_match, lambda {|query| joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)}
search_methods :body_matches, :post_tag_match
def presenter
@presenter ||= NotePresenter.new(self)

View File

@@ -47,7 +47,6 @@ class Post < ActiveRecord::Base
scope :positive, where("score > 1")
scope :negative, where("score < -1")
scope :updater_name_matches, lambda {|name| where("updater_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)}
search_methods :tag_match, :updater_name_matches
scope :after_id, Proc.new {|num|
if num.present?
where("id > ?", num.to_i).reorder("id asc")

View File

@@ -4,7 +4,6 @@ class Tag < ActiveRecord::Base
has_one :wiki_page, :foreign_key => "name", :primary_key => "title"
scope :name_matches, lambda {|name| where("name LIKE ? ESCAPE E'\\\\'", name.downcase.to_escaped_for_sql_like)}
scope :named, lambda {|name| where("name = ?", TagAlias.to_aliased([name]).join(""))}
search_methods :name_matches
class CategoryMapping
Danbooru.config.reverse_tag_category_mapping.each do |value, category|

View File

@@ -8,7 +8,6 @@ class TagAlias < ActiveRecord::Base
validate :absence_of_transitive_relation
belongs_to :creator, :class_name => "User"
scope :name_matches, lambda {|name| where("(antecedent_name = ? or consequent_name = ?)", name.downcase, name.downcase)}
search_method :name_matches
def self.to_aliased(names)
alias_hash = Cache.get_multi(names.flatten, "ta") do |name|

View File

@@ -7,7 +7,6 @@ class TagImplication < ActiveRecord::Base
validates_uniqueness_of :antecedent_name, :scope => :consequent_name
validate :absence_of_circular_relation
scope :name_matches, lambda {|name| where("(antecedent_name = ? or consequent_name = ?)", name.downcase, name.downcase)}
search_method :name_matches
module DescendantMethods
extend ActiveSupport::Concern

View File

@@ -44,7 +44,6 @@ class User < ActiveRecord::Base
scope :admins, where("is_admin = TRUE")
scope :with_email, lambda {|email| email.blank? ? where("FALSE") : where(["email = ?", email])}
scope :find_for_password_reset, lambda {|name, email| email.blank? ? where("FALSE") : where(["name = ? AND email = ?", name, email])}
search_method :named
module BanMethods
def validate_ip_addr_is_not_banned