Module: ZipSearch::ActsAsLocation::ClassMethods

Defined in:
lib/zip_search/acts_as_location.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_location(title = :location, options = {}) ⇒ Object



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

#location_fieldsObject



43
# File 'lib/zip_search/acts_as_location.rb', line 43

def location_fields; i( street_address zip county city state ) end

#nested_params(extra_params = []) ⇒ Object



50
51
52
# File 'lib/zip_search/acts_as_location.rb', line 50

def nested_params(extra_params=[])
  [:id, :_destroy] + params(extra_params)
end

#params(extra_params = []) ⇒ Object



45
46
47
48
# File 'lib/zip_search/acts_as_location.rb', line 45

def params(extra_params=[])
  extra_params + [ :zs_association_name, :title, :latitude, :longitude,
                   :street_address, :zip, :county, :city, :state ]
end