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

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