search_controller.rb
836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class SearchController < ApplicationController
SEARCHES = []
def self.search(&block)
SEARCHES << block
end
protected
#############################################
# XXX add yours searches here
#############################################
search do |query|
Article.find_tagged_with(query)
end
search do |query|
Article.find_by_contents(query)
end
search do |query|
Profile.find_by_contents(query)
end
# auxiliary method to search in all defined searches and collect the results
def search(query)
SEARCHES.inject([]) do |acc,finder|
acc += finder.call(query)
end.sort_by do |hit|
(hit.respond_to? :ferret_score) ? (1.0 - hit.ferret_score) : (-1.0)
end
end
public
def index
@query = params[:query] || ''
@results = search(@query)
end
end