Module: ZipSearch::ControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/zip_search/controller_helpers.rb
Instance Method Summary collapse
Instance Method Details
#apply_location_filtering(target) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/zip_search/controller_helpers.rb', line 41 def apply_location_filtering(target) return target unless params.key? :zs_location zl = Location.find_by(id: params[:zlid].to_i) q = params[:location_search][:q] zl_association = if has_zipsearch_locations? target :zipsearch_locations elsif has_zipsearch_location? target :zipsearch_location end return target if zl_association.nil? wq = case params[:by] when 'zip' then ['zipsearch_locations.zip = ?', q] when 'county' ['zipsearch_locations.county = ? AND zipsearch_locations.state = ?', zl.county, zl.state] when 'city' ['zipsearch_locations.city = ? AND zipsearch_locations.state = ?', zl.city, zl.state] when 'state' then ['zipsearch_locations.state = ?', q] end return target unless wq.present? target.joins(zl_association).where(*wq) end |
#do_zipcode_search(zip) ⇒ Object
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 |
# File 'lib/zip_search/controller_helpers.rb', line 12 def do_zipcode_search(zip) Geocoder.configure(lookup: :nominatim) n_results = Geocoder.search zip, bounds: US_BOUNDS Geocoder.configure(lookup: :google) g_results = Geocoder.search zip, bounds: US_BOUNDS response = {} if n_results.any? || g_results.any? if g_results.any? g_best_match = g_results.first response[:city] = g_best_match.city response[:state] = g_best_match.state end if n_results.any? n_best_match = if g_results.any? matching_g_results = n_results.select{|r| (r.city.present? && r.city == g_best_match.city) || (r.state.present? && r.state == g_best_match.state) } if matching_g_results.any? then matching_g_results.first else n_results.first end else n_results.first end response[:county] = n_best_match.county response[:city] ||= n_best_match.city response[:state] ||= n_best_match.state end end response end |