1
2
3
4
5
6
7
8
9
10
11
12
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
42
43
class Entry < ActiveRecord::Base

  acts_as_mappable
  acts_as_taggable_on   :tags

  validates_presence_of :name, :tag_list
  validate              :required_info

  before_save           :geocode_paddress
  #validates_presence_of :tollfreephone or :phone or :phone2 or :mobile or :fax or :email or :website
  #acts_as_taggable_on :tags
  #acts_as_mappable :auto_geocode=>{:field=>:paddress, :error_message=>'Could not geocode physical address'}
  searchable_on         :name, :address, :phone, :phone2, :mobile, :fax, :email,
                        :website, :category, :description

private

  def required_info
    unless(phone or phone2 or tollfreephone or mobile or fax or email or website)
      errors.add_to_base "Please have at least one form of contact information."
    end
  end

  def geocode_paddress
    #if paddress is nil or empty set the old values to nil and return
    ((self.lat = self.lng = nil) or return) if paddress.empty?
    g=Geokit::Geocoders::MultiGeocoder.geocode(paddress)
      (errors.add(:paddress, "Could not Geocode address") or return) unless g.success
      self.lat, self.lng = g.lat,g.lng
  end

  #def geocode_paddress
  #  if paddress != nil
  #    geo = Geokit::Geocoders::MultiGeocoder.geocode (paddress)
  #    if geo.success
  #       self.lat, self.lng = geo.lat,geo.lng
  #    else
  #       errors.add(:paddress, "Could not Geocode address")
  #    end
  #  end
  #end

end