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
40
41
|
# File 'lib/zip_search/acts_as_location.rb', line 13
def acts_as_location(title=:location, options={})
pg_search_scope :search_by_location,
against: [:zip, :county, :city, :state],
using: {tsearch: {prefix: true}}
pg_search_scope :search_by_state, against: :state,
using: {tsearch: {prefix: true}}
pg_search_scope :search_by_city, against: :city,
using: {tsearch: {prefix: true}}
pg_search_scope :search_by_county, :against => :county,
using: {tsearch: {prefix: true}}
pg_search_scope :search_by_zip, against: :zip
geocoded_by :to_sentence
reverse_geocoded_by :latitude, :longitude do |obj, results|
if geo = results.first
obj.street_address ||= obj.street_address
obj.zip ||= geo.postal_code
obj.county ||= geo.county if geo.respond_to? :county
obj.city ||= geo.city
obj.state ||= geo.state
end
end
after_validation :geocode, if: :should_geocode?
after_validation :reverse_geocode, if: :should_reverse_geocode?
validates_presence_of :zs_association_name
include ActsAsLocation::LocalInstanceMethods
end
|