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
|
# File 'app/controllers/zip_search/locations_controller.rb', line 6
def location_search
q = params[:q]; by = params[:by]
results = if by.present?
case by
when 'state'
US_STATES.map{|s| { name: s[0], id: s[1], param: by } }
when 'city'
search = Location.search_by_city q
search.to_a.uniq{|r| [ r[:city], r[:state] ] }.map{|r|
{ id: r[:id], name: r[:city], param: by,
desc: "City in #{r[:state]}" } }
when 'county'
search = Location.search_by_county q
search.to_a.uniq{|r| [ r[:county], r[:state] ] }.map{|r|
{ id: r[:id], name: r[:county], param: by,
desc: "County in #{r[:city]}, #{r[:state]}" } }
when 'zip'
search = Location.search_by_zip q
search.to_a.uniq(&:zip).map{|r|
{ id: r[:id], name: r[:zip], param: by,
desc: "#{r[:county]}, #{r[:city]}, #{r[:state]}" } }
end
else [] end
render json: results
end
|