You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
593 B
21 lines
593 B
class CityCache < ActiveRecord::Base
|
|
self.table_name = :city_cache
|
|
|
|
belongs_to :city
|
|
|
|
# look for a term to see if its already been searched for
|
|
def self.search(str)
|
|
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: normalize_string(str))
|
|
end
|
|
|
|
private
|
|
def self.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
|
|
|