<commit_before># == Schema Information
# Schema version: 20090216032013
#
# Table name: addresses
#
#  id             :integer(4)      not null, primary key
#  person_id      :integer(4)      
#  name           :string(50)      
#  address_line_1 :string(50)      
#  address_line_2 :string(50)      
#  address_line_3 :string(50)      
#  city           :string(50)      
#  county_id      :string(255)     
#  state_id       :integer(4)      
#  zipcode_plus_4 :string(10)      
#  latitude       :decimal(12, 8)  not null
#  longitude      :decimal(12, 8)  not null
#  created_at     :datetime        
#  updated_at     :datetime        
#

class Address < ActiveRecord::Base
  belongs_to :person, :inverse_of => :addresses
  belongs_to :state

  attr_accessible :address_line_1, :address_line_2, :address_line_3, :city, :state_id, :zipcode_plus_4, :address_privacy
  attr_accessible :primary
  attr_accessible *attribute_names, :as => :admin

  attr_accessible :person_attributes, :allow_destroy => true
  accepts_nested_attributes_for :person, :allow_destroy => true

  after_commit :geocode_address, :if => :persisted?
  acts_as_mappable :lat_column_name => 'latitude', :lng_column_name => 'longitude'
  
  def to_s
    Address.string_representation(address_line_1, address_line_2, address_line_3, city, state.try(:abbreviation), zipcode_plus_4)
  end


  def Address.string_representation(address_line_1, address_line_2, address_line_3, city,  state, zipcode_plus_4)
    # Don't include City and State unless address_line_1 is present
    if address_line_1.blank?
      zipcode_plus_4
    else
      [address_line_1, address_line_2, address_line_3, city, state, zipcode_plus_4].collect(&:presence).compact.join(", ")
    end
  end
 
  def perform
    geo = GeoKit::Geocoders::MultiGeocoder.geocode(self.to_s)
    if geo.success
      update_column(:latitude, geo.lat)
      update_column(:longitude, geo.lng)
      save!
    else
      Rails.logger.info "Address#perform fail"
    end    
  end
  
  private
  def geocode_address<commit_msg>make sure geocode_address is only called once. closes gh-596<commit_after># == Schema Information
# Schema version: 20090216032013
#
# Table name: addresses
#
#  id             :integer(4)      not null, primary key
#  person_id      :integer(4)      
#  name           :string(50)      
#  address_line_1 :string(50)      
#  address_line_2 :string(50)      
#  address_line_3 :string(50)      
#  city           :string(50)      
#  county_id      :string(255)     
#  state_id       :integer(4)      
#  zipcode_plus_4 :string(10)      
#  latitude       :decimal(12, 8)  not null
#  longitude      :decimal(12, 8)  not null
#  created_at     :datetime        
#  updated_at     :datetime        
#

class Address < ActiveRecord::Base
  belongs_to :person, :inverse_of => :addresses
  belongs_to :state

  attr_accessible :address_line_1, :address_line_2, :address_line_3, :city, :state_id, :zipcode_plus_4, :address_privacy
  attr_accessible :primary
  attr_accessible *attribute_names, :as => :admin

  attr_accessible :person_attributes, :allow_destroy => true
  accepts_nested_attributes_for :person, :allow_destroy => true

  after_commit :geocode_address, :if => :persisted?
  acts_as_mappable :lat_column_name => 'latitude', :lng_column_name => 'longitude'
  
  def to_s
    Address.string_representation(address_line_1, address_line_2, address_line_3, city, state.try(:abbreviation), zipcode_plus_4)
  end


  def Address.string_representation(address_line_1, address_line_2, address_line_3, city,  state, zipcode_plus_4)
    # Don't include City and State unless address_line_1 is present
    if address_line_1.blank?
      zipcode_plus_4
    else
      [address_line_1, address_line_2, address_line_3, city, state, zipcode_plus_4].collect(&:presence).compact.join(", ")
    end
  end
 
  def perform
    geo = GeoKit::Geocoders::MultiGeocoder.geocode(self.to_s)
    if geo.success
      update_column(:latitude, geo.lat)
      update_column(:longitude, geo.lng)
    else
      Rails.logger.info "Address#perform fail"
    end    
  end
  
  private
  def geocode_address
{'fraction_non_alphanumeric': 0.09465020576131687, 'fraction_numerical': 0.03025901718712176, 'mean_word_length': 3.3771186440677967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '7018613', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1409, 'n_words': 294}