module ZipSearch::ControllerHelpers
Public Instance Methods
apply_location_filtering(target)
click to toggle source
# 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)
click to toggle source
# 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
Protected Instance Methods
has_zipsearch_location?(klass, name=:zipsearch_location)
click to toggle source
# File lib/zip_search/controller_helpers.rb, line 70 def has_zipsearch_location?(klass, name=:zipsearch_location) klass.reflect_on_all_associations(:has_one) .any?{|a| a.name == name } || klass.reflect_on_all_associations(:belongs_to) .any?{|a| a.name == name } end
has_zipsearch_locations?(klass, name=:zipsearch_locations)
click to toggle source
# File lib/zip_search/controller_helpers.rb, line 66 def has_zipsearch_locations?(klass, name=:zipsearch_locations) klass.reflect_on_all_associations(:has_many) .any?{|a| a.name == name } end