<commit_before>class Geocode < ActiveRecord::Base
  include Comparable

  has_many :geocodings, :dependent => :destroy
  has_many :geocoder_queries, :dependent => :destroy
  
  cattr_accessor :geocoder
  
  def distance_to(destination, units = :miles, formula = :haversine)
    if destination && destination.latitude && destination.longitude
      Graticule::Distance.const_get(formula.to_s.camelize).distance(self, destination, units)
    end
  end

  def geocoded?
    !latitude.blank? && !longitude.blank?
  end
  
  def self.find_or_create_by_query(query)
    find_by_query(query) || create_by_query(query)
  end
  
  def self.create_by_query(query)
    create geocoder.locate(query).attributes.merge(:query => query)
  end
  
  def self.find_or_create_by_location(location)
    find_by_query(location.to_s) || create_from_location(location)
  end
  
  def self.create_from_location(location)
    create geocoder.locate(location).attributes.merge(:query => location.to_s)
  rescue
    nil
  end
  
  def geocoded
    @geocoded ||= geocodings.collect { |geocoding| geocoding.geocodable }
  end
  
  def on(geocodable)
    geocodings.create :geocodable => geocodable
  end
  
  def <=>(comparison_object)
    self.to_s <=> comparison_object.to_s
  end

  # Note this may return values to the opposite way you were expecting!
  def coordinates
    "#{longitude},#{latitude}"
  end

  def to_s<commit_msg>create_ method can now handle geocoder_queries relation<commit_after>class Geocode < ActiveRecord::Base
  include Comparable

  has_many :geocodings, :dependent => :destroy
  has_many :geocoder_queries, :dependent => :destroy
  
  cattr_accessor :geocoder
  
  def distance_to(destination, units = :miles, formula = :haversine)
    if destination && destination.latitude && destination.longitude
      Graticule::Distance.const_get(formula.to_s.camelize).distance(self, destination, units)
    end
  end

  def geocoded?
    !latitude.blank? && !longitude.blank?
  end
  
  def self.find_or_create_by_query(query)
    find_by_query(query) || create_by_query(query)
  end
  
  def self.create_by_query(query)
    location = geocoder.locate(query)
    # Find geocodes with this location that already exist
    geocodes = find(:all, :conditions => {:latitude => location.latitude, :longitude => location.longitude, 
      :street => location.street, :locality => location.locality, :region => location.region,
      :postal_code => location.postal_code, :country => location.country})
    geocoder_query = GeocoderQuery.new(:query => query)
    if geocodes.size == 0
      # No existing geocodes
      create location.attributes.merge(:geocoder_queries => [geocoder_query])
    elsif geocodes.size != 1
      # Too many geocodes with this location, should only be one geocode record per location.
      raise RuntimeError.new("There is more than one geocode with the same location: #{location}.")
    else
      # A single existing geocode at this location, create the geocoder query and associate with the geocode.
      geocodes[0].geocoder_queries << geocoder_query
    end
  end
  
  def self.find_or_create_by_location(location)
    find_by_query(location.to_s) || create_from_location(location)
  end
  
  def self.create_from_location(location)
    create geocoder.locate(location).attributes.merge(:query => location.to_s)
  rescue
    nil
  end
  
  def geocoded
    @geocoded ||= geocodings.collect { |geocoding| geocoding.geocodable }
  end
  
  def on(geocodable)
    geocodings.create :geocodable => geocodable
  end
  
  def <=>(comparison_object)
    self.to_s <=> comparison_object.to_s
  end

  # Note this may return values to the opposite way you were expecting!
  def coordinates
    "#{longitude},#{latitude}"
  end

  def to_s
{'fraction_non_alphanumeric': 0.0952, 'fraction_numerical': 0.0008, 'mean_word_length': 3.9682119205298014, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6908951', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1240, 'n_words': 280}