~datacommons/datacommons/trunk

« back to all changes in this revision

Viewing changes to app/controllers/people_controller.rb

  • Committer: Paul Fitzpatrick
  • Date: 2013-04-04 15:23:15 UTC
  • Revision ID: paulfitz@alum.mit.edu-20130404152315-fdta8ed1yzh1qslj
triage people select problem - use autocomplete

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
  # GET /people
12
12
  # GET /people.xml
13
13
  def index
14
 
    @people = Person.find(:all)
 
14
    if params[:q]
 
15
      name = params[:q]
 
16
      limit = 50
 
17
      conditions = nil
 
18
      joinSQL = nil
 
19
      if name.length>=2
 
20
        first, last = name.split(/ /)
 
21
        unless last.nil?
 
22
          last = nil if last == ""
 
23
        end
 
24
        joinSQL, condSQLs, condParams = Organization.all_join(session,{ :entity => "Person"})
 
25
        joinSQL = "INNER JOIN organizations_people ON organizations_people.person_id = people.id INNER JOIN organizations ON organizations_people.organization_id = organizations.id #{joinSQL}"
 
26
        joinSQL = nil if condSQLs.empty?
 
27
        unless last.nil?
 
28
          condSQLs << "people.lastname LIKE ? AND people.firstname LIKE ?"
 
29
          condParams << (last + "%")
 
30
          condParams << (first + "%")
 
31
        else
 
32
          condSQLs << "people.lastname LIKE ? OR people.firstname LIKE ?"
 
33
          condParams << (first + "%")
 
34
          condParams << (first + "%")
 
35
        end
 
36
        # people = Person.find(:all, :conditions => ["lastname LIKE ? OR firstname LIKE ?",first + "%",first+"%"], :limit => limit)
 
37
        conditions = []
 
38
        conditions = [condSQLs.collect{|c| "(#{c})"}.join(' AND ')] + condParams unless condSQLs.empty?
 
39
      else
 
40
        conditions = ["people.lastname IS NOT NULL AND people.lastname <> ''"]
 
41
      end
 
42
      @people = Person.find(:all, :conditions => conditions, :joins => joinSQL, :limit => limit, :order => "people.lastname ASC, people.firstname ASC")
 
43
    else
 
44
      @people = Person.find(:all)
 
45
    end
15
46
 
16
47
    respond_to do |format|
17
48
      format.html # index.html.erb
18
49
      format.xml  { render :xml => @people }
 
50
      format.json  { render :json => @people }
19
51
    end
20
52
  end
21
53