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

@@ -10,9 +10,53 @@ class ForumPost < ActiveRecord::Base
validates_presence_of :body, :creator_id
validate :validate_topic_is_unlocked
before_destroy :validate_topic_is_unlocked
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")
module SearchMethods
def body_matches(body)
where("forum_posts.text_index @@ plainto_tsquery(?)", body)
end
def for_user(user_id)
where("forum_posts.creator_id = ?", user_id)
end
def creator_name(name)
where("forum_posts.creator_id = (select _.id from users _ where lower(_.name) = ?)", name)
end
def active
where("is_deleted = false")
end
def search(params)
q = scoped
return q if params.blank?
if params[:creator_id]
q = q.where("creator_id = ?", params[:creator_id].to_i)
end
if params[:topic_id]
q = q.where("topic_id = ?", params[:topic_id].to_i)
end
if params[:topic_title_matches]
q = q.joins(:topic).where("forum_topics.text_index @@ plainto_tsquery(?)", params[:topic_title_matches])
end
if params[:body_matches]
q = q.body_matches(params[:body_matches])
end
if params[:creator_name]
q = q.creator_name(params[:creator_name])
end
q
end
end
extend SearchMethods
def self.new_reply(params)
if params[:topic_id]