public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
insoshi / app / controllers / searches_controller.rb
100644 54 lines (45 sloc) 1.565 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
class SearchesController < ApplicationController
  include ApplicationHelper
 
  before_filter :login_required
 
  def index
    
    redirect_to(home_url) and return if params[:q].nil?
    
    query = params[:q].strip.inspect
    model = strip_admin(params[:model])
    page = params[:page] || 1
 
    unless %(Person Message ForumPost).include?(model)
      flash[:error] = "Invalid search"
      redirect_to home_url and return
    end
 
    if query.blank?
      @search = [].paginate
      @results = []
    else
      filters = {}
      if model == "Person" and current_person.admin?
        # Find all people, including deactivated and email unverified.
        model = "AllPerson"
      elsif model == "Message"
        filters['recipient_id'] = current_person.id
      end
      @search = Ultrasphinx::Search.new(:query => query,
                                        :filters => filters,
                                        :page => page,
                                        :class_names => model)
      @search.run
      @results = @search.results
      if model == "AllPerson"
        # Convert to people so that the routing works.
        @results.map!{ |person| Person.find(person) }
      end
    end
  rescue Ultrasphinx::UsageError
    flash[:error] = "Invalid search query"
    redirect_to searches_url(:q => "", :model => params[:model])
  end
  
  private
    
    # Strip off "Admin::" from the model name.
    # This is needed for, e.g., searches in the admin view
    def strip_admin(model)
      model.split("::").last
    end
end