class SpecificMapItem < ActiveRecord::Base
  belongs_to :specific_map
  
  validates :name, :presence => true
  
  has_many :specific_map_item_cavs, :dependent => :destroy
  
  has_many :cav_levels, :through => :specific_map_item_cavs
  
  before_create do
    self.geolocalise
  end
  
  def geolocalise
    self.g_text = self.address.to_s+", "+self.cp.to_s+" "+self.city.to_s+", France"
    
    
    @result = nil


    result = Geocoder.search(self.g_text).each do |result|
      if result.city and result.country 
    
        
        @result = result
        break
        
      end
    end
    if @result
      self.g_latitude = @result.latitude
      self.g_longitude = @result.longitude
    end
  end
  
  reverse_geocoded_by :g_latitude, :g_longitude
  
end
