refactored search

This commit is contained in:
albert
2013-01-10 17:45:52 -05:00
parent 13271e9bf5
commit 8749c43b3e
85 changed files with 946 additions and 304 deletions

View File

@@ -9,57 +9,60 @@ class Comment < ActiveRecord::Base
attr_accessible :body, :post_id
attr_accessor :do_not_bump_post
scope :recent, :order => "comments.id desc", :limit => 6
module SearchMethods
extend ActiveSupport::Concern
def recent
order("comments.id desc").limit(6)
end
module ClassMethods
def body_matches(query)
where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")
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
return q if params.blank?
if params[:body_matches]
q = q.body_matches(params[:body_matches])
end
def hidden(user)
where("score < ?", user.comment_threshold)
if params[:post_tags_match]
q = q.post_tags_match(params[:post_tags_match])
end
def visible(user)
where("score >= ?", user.comment_threshold)
if params[:creator_name]
q = q.for_user_name(params[:creator_name])
end
def post_tags_match(query)
joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)
if params[:creator_id]
q = q.for_creator(params[:creator_id].to_i)
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
q
end
end
include SearchMethods
extend SearchMethods
def initialize_creator
self.creator_id = CurrentUser.user.id