work on refactoring search
This commit is contained in:
3
Gemfile
3
Gemfile
@@ -23,14 +23,13 @@ gem "super_exception_notifier"
|
||||
gem "simple_form"
|
||||
gem "mechanize", :git => 'git://github.com/caribio/mechanize.git'
|
||||
gem "nokogiri"
|
||||
gem "meta_search", :git => "git://github.com/ernie/meta_search.git"
|
||||
gem "whenever", :require => false
|
||||
gem "sanitize", :git => "git://github.com/rgrove/sanitize.git"
|
||||
gem 'rmagick', :require => "RMagick"
|
||||
gem 'daemons'
|
||||
gem 'newrelic_rpm'
|
||||
gem 'net-ssh'
|
||||
gem 'net-sftp'
|
||||
gem 'newrelic_rpm'
|
||||
|
||||
group :development do
|
||||
gem 'pry'
|
||||
|
||||
13
Gemfile.lock
13
Gemfile.lock
@@ -10,16 +10,6 @@ GIT
|
||||
ntlm-http (~> 0.1, >= 0.1.1)
|
||||
webrobots (~> 0.0, >= 0.0.9)
|
||||
|
||||
GIT
|
||||
remote: git://github.com/ernie/meta_search.git
|
||||
revision: c7f084412c483456830b8ec26514991e17da94c7
|
||||
specs:
|
||||
meta_search (1.1.3)
|
||||
actionpack (~> 3.1)
|
||||
activerecord (~> 3.1)
|
||||
activesupport (~> 3.1)
|
||||
polyamorous (~> 0.5.0)
|
||||
|
||||
GIT
|
||||
remote: git://github.com/rgrove/sanitize.git
|
||||
revision: 4925ac8058320fa587b6bdd7556fdf76e2ad5d31
|
||||
@@ -106,8 +96,6 @@ GEM
|
||||
nokogiri (1.5.5)
|
||||
ntlm-http (0.1.1)
|
||||
pg (0.12.2)
|
||||
polyamorous (0.5.0)
|
||||
activerecord (~> 3.0)
|
||||
polyglot (0.3.3)
|
||||
pry (0.9.10)
|
||||
coderay (~> 1.0.5)
|
||||
@@ -196,7 +184,6 @@ DEPENDENCIES
|
||||
ffaker!
|
||||
mechanize!
|
||||
memcache-client
|
||||
meta_search!
|
||||
mocha
|
||||
net-sftp
|
||||
net-ssh
|
||||
|
||||
@@ -18,8 +18,7 @@ class ArtistsController < ApplicationController
|
||||
end
|
||||
|
||||
def index
|
||||
@search = Artist.search(params[:search])
|
||||
@artists = @search.paginate(params[:page])
|
||||
@artists = Artist.search(params[:search]).paginate(params[:page])
|
||||
respond_with(@artists) do |format|
|
||||
format.xml do
|
||||
render :xml => @artists.to_xml(:include => [:urls])
|
||||
@@ -31,7 +30,6 @@ class ArtistsController < ApplicationController
|
||||
end
|
||||
|
||||
def search
|
||||
@search = Artist.search(params[:search])
|
||||
end
|
||||
|
||||
def show
|
||||
|
||||
@@ -71,8 +71,7 @@ private
|
||||
end
|
||||
|
||||
def index_by_comment
|
||||
@search = Comment.search(params[:search])
|
||||
@comments = @search.paginate(params[:page])
|
||||
@comments = Comment.search(params[:search]).paginate(params[:page])
|
||||
respond_with(@comments) do |format|
|
||||
format.html {render :action => "index_by_comment"}
|
||||
end
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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|
|
||||
|
||||
@@ -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|
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -195,7 +195,7 @@ Danbooru::Application.routes.draw do
|
||||
match "/artist" => redirect {|params, req| "/artists?page=#{req.params[:page]}"}
|
||||
match "/artist/index" => redirect {|params, req| "/artists?page=#{req.params[:page]}"}
|
||||
match "/artist/show/:id" => redirect("/artists/%{id}")
|
||||
match "/artist/history/:id" => redirect("/artist_versions?search[artist_id_eq]=%{id}")
|
||||
match "/artist/history/:id" => redirect("/artist_versions?search[artist_id]=%{id}")
|
||||
|
||||
match "/comment" => redirect {|params, req| "/comments?page=#{req.params[:page]}"}
|
||||
match "/comment/index" => redirect {|params, req| "/comments?page=#{req.params[:page]}"}
|
||||
@@ -215,7 +215,7 @@ Danbooru::Application.routes.draw do
|
||||
match "/pool" => redirect {|params, req| "/pools?page=#{req.params[:page]}"}
|
||||
match "/pool/index" => redirect {|params, req| "/pools?page=#{req.params[:page]}"}
|
||||
match "/pool/show/:id" => redirect("/pools/%{id}")
|
||||
match "/pool/history/:id" => redirect("/pool_versions?search[pool_id_eq]=%{id}")
|
||||
match "/pool/history/:id" => redirect("/pool_versions?search[pool_id]=%{id}")
|
||||
match "/pool/recent_changes" => redirect("/pool_versions")
|
||||
|
||||
match "/post/index.xml", :controller => "legacy", :action => "posts", :format => "xml"
|
||||
|
||||
@@ -2474,7 +2474,8 @@ CREATE TABLE uploads (
|
||||
post_id integer,
|
||||
md5_confirmation character varying(255),
|
||||
created_at timestamp without time zone NOT NULL,
|
||||
updated_at timestamp without time zone NOT NULL
|
||||
updated_at timestamp without time zone NOT NULL,
|
||||
server text
|
||||
);
|
||||
|
||||
|
||||
@@ -6169,4 +6170,6 @@ INSERT INTO schema_migrations (version) VALUES ('20110722211855');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20110815233456');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20111101212358');
|
||||
INSERT INTO schema_migrations (version) VALUES ('20111101212358');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20130106210658');
|
||||
Reference in New Issue
Block a user