search_controller.rb
1.32 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class SearchController < ApplicationController
helper TagsHelper
protected
def search(finder, query)
finder.find_by_contents(query).sort_by do |hit|
-(relevance_for(hit))
end
end
public
include SearchHelper
######################################################
class Finder
attr_reader :environment
def initialize(env)
@environment = env
end
def articles
environment.articles
end
def comments
environment.comments
end
end
def index
@query = params[:query] || ''
@filtered_query = remove_stop_words(@query)
@finder ||= SearchController::Finder.new(@environment)
@results = { :articles => search(@finder.articles, @query),
:comments => search(@finder.comments, @query) }
end
before_filter :load_category, :only => :filter
def filter
@finder = @category
index
render :action => 'index'
end
#######################################################
def tags
@tags = Tag.find(:all).inject({}) do |memo,tag|
memo[tag.name] = tag.taggings.count
memo
end
end
def tag
@tag = Tag.find_by_name(params[:tag])
@tagged = @tag.taggings.map(&:taggable)
end
#######################################################
def popup
render :action => 'popup', :layout => false
end
end