add search filter

This commit is contained in:
Sam 2014-05-16 00:31:45 +10:00
parent b2d6e2aa43
commit 90d7718103
3 changed files with 20 additions and 3 deletions

View file

@ -269,7 +269,8 @@ class ListController < ApplicationController
ascending: params[:ascending],
min_posts: params[:min_posts],
max_posts: params[:max_posts],
status: params[:status]
status: params[:status],
search: params[:search]
}
options[:no_subcategories] = true if params[:no_subcategories] == 'true'

View file

@ -232,10 +232,20 @@ class Search
posts.limit(limit)
end
def query_locale
def self.query_locale
@query_locale ||= Post.sanitize(Search.long_locale)
end
def query_locale
self.class.query_locale
end
def self.ts_query(term)
all_terms = term.gsub(/[:()&!'"]/,'').split
query = Post.sanitize(all_terms.map {|t| "#{PG::Connection.escape_string(t)}:*"}.join(" & "))
"TO_TSQUERY(#{query_locale}, #{query})"
end
def ts_query
@ts_query ||= begin
all_terms = @term.gsub(/[:()&!'"]/,'').split

View file

@ -22,7 +22,9 @@ class TopicQuery
ascending
no_subcategories
no_definitions
status).map(&:to_sym)
status
search
).map(&:to_sym)
# Maps `order` to a columns in `topics`
SORTABLE_MAPPING = {
@ -259,6 +261,10 @@ class TopicQuery
result = result.where('topics.id in (?)', options[:topic_ids]).references(:topics)
end
if search = options[:search]
result = result.where("topics.id in (select pp.topic_id from post_search_data pd join posts pp on pp.id = pd.post_id where pd.search_data @@ #{Search.ts_query(search.to_s)})")
end
if status = options[:status]
case status
when 'open'