refactored search
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user