Class: Muck::SearchController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/muck/search_controller.rb

Instance Method Summary collapse

Constructor Details

#initializeSearchController

Returns a new instance of SearchController.



5
6
7
8
# File 'app/controllers/muck/search_controller.rb', line 5

def initialize
  @limit = 50
  @no_index = true
end

Instance Method Details

#indexObject

Show search page if there is nothing specified to search for or if there are searchable params search for them



13
14
15
16
17
18
19
20
21
# File 'app/controllers/muck/search_controller.rb', line 13

def index
  if request.post?
    if params[:form_terms].nil? or params[:form_terms].empty?
      flash[:message] = 'Please enter something to search for.'
    else
      redirect_to '/search/results?terms=' + URI.escape(params[:form_terms])
    end
  end
end

#render_searchObject

all the results are rendered the same



74
75
76
77
78
79
80
81
82
# File 'app/controllers/muck/search_controller.rb', line 74

def render_search
  respond_to do |format|
    format.html { render :template => '/search/index' }
    format.xml  { render :xml => flash.to_xml }
    format.rss  { render :xml => flash.to_xml }
    format.atom  { render :xml => flash.to_xml }
    format.rdf  { render :xml => flash.to_xml }
  end
end

#resultsObject

search all fields for the given terms



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
# File 'app/controllers/muck/search_controller.rb', line 25

def results
  @page_title = "Search"
  @language = params[:locale] || 'en'
  @languages = Language.find(:all, :order => "name")
  @limit = params[:limit] ? params[:limit].to_i : 10
  @limit = 25 if @limit > 25
  @offset = params[:offset] ? params[:offset].to_i : 0
  @term_list = ''
  if !params[:terms].nil? && !params[:terms].empty?
    @term_list += URI.escape(params[:terms])
  end

  if request.post?
    if params[:form_terms].nil? or params[:form_terms].empty?
      flash[:message] = 'Please enter something to search for.'
      redirect_to '/'
    else
      redirect_to '/search/results?terms=' + URI.escape(params[:form_terms]) + (@language.nil? ? "" : "&locale=" + @language)
    end
  else
    @current_uri = '/search/results'
    if params[:terms]
      @url_terms = '?terms=' + @term_list
      results = Entry.search(URI.unescape(params[:terms]), @language, @limit, @offset)
      @results = results.records
      @hit_count = results.total
      @terms = URI.unescape(params[:terms])
    else
      @url_terms = ''
      @results = []
      @terms = '' 
    end

    respond_to do |format|
      format.html 
      format.xml  { render :layout=>false }
      format.rss  { render :layout=>false }
      format.pjs { 
        @json_results = ActiveSupport::JSON.encode(@results)
        render(:template => 'search/results.pjs.erb', :layout => false)  
      }
      format.atom { render :layout=>false }
      format.rdf  { render :layout=>false }
    end
  end
end