From 80000057a6267989ce942b33e5a0122f4e869f2a Mon Sep 17 00:00:00 2001 From: Godwin Date: Wed, 14 Dec 2016 09:09:37 -0800 Subject: [PATCH] Update city_cache.rb --- app/models/city_cache.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/city_cache.rb b/app/models/city_cache.rb index e2f8f38..c763454 100644 --- a/app/models/city_cache.rb +++ b/app/models/city_cache.rb @@ -3,11 +3,19 @@ class CityCache < ActiveRecord::Base belongs_to :city + # look for a term to see if its already been searched for def self.search(str) - CityCache.find_by_search(str.downcase) + CityCache.find_by_search(normalize_string(str)) end + # cache this search term def self.cache(str, city_id) - CityCache.create(city_id: city_id, search: str.downcase) + CityCache.create(city_id: city_id, search: normalize_string(str)) end + + private + def normalize_string(str) + # remove accents, unnecessary whitespace, punctuation, and lowcase tje string + I18n.transliterate(str).gsub(/[^\w\s]/, '').gsub(/\s\s+/, ' ').strip.downcase + end end