<commit_before>class Location < ActiveRecord::Base
  belongs_to :person
  belongs_to :district
  belongs_to :state

  after_save :update_nation_builder

  def update_location(address: nil, city: nil, state: nil, zip: nil)
    if address
      if district = District.find_by_address(address: address,
                                             city:    city,
                                             state:   state,
                                             zip:     zip)
        self.address_1 = address
        self.city      = city
        self.district  = district
        self.state     = district.state
        self.zip_code  = zip if zip = ZipCode.valid_zip_5(zip)
      else
        return nil
      end
    elsif zip = ZipCode.valid_zip_5(zip) and zip != self.zip_code
      zip_code = ZipCode.find_by(zip_code: zip)
      self.address_1 = nil
      self.city      = city
      self.zip_code  = zip
      self.state     = zip_code.try(:state)
      self.district  = zip_code.try(:single_district)
    end
    self.save
  end

  def state_abbrev
    state ? state.abbrev : district.state.abbrev
  end

  private

  def serializable_hash(options)
    super(methods: [:state_abbrev],
             only: [:address_1, :address_2, :city, :zip_code])
  end

  def update_nation_builder
    if (changed - ["district_id", "created_at", "updated_at"]).any?
      nb_args = Integration::NationBuilder.location_params(person.email, self.as_json)<commit_msg>fix bug introduced in last commit<commit_after>class Location < ActiveRecord::Base
  belongs_to :person
  belongs_to :district
  belongs_to :state

  after_save :update_nation_builder

  def update_location(address: nil, city: nil, state: nil, zip: nil)
    if address
      if district = District.find_by_address(address: address,
                                             city:    city,
                                             state:   state,
                                             zip:     zip)
        self.address_1 = address
        self.city      = city
        self.district  = district
        self.state     = district.state
        self.zip_code  = zip if zip = ZipCode.valid_zip_5(zip)
      else
        return nil
      end
    elsif zip = ZipCode.valid_zip_5(zip) and zip != self.zip_code
      zip_code = ZipCode.find_by(zip_code: zip)
      self.address_1 = nil
      self.city      = city
      self.zip_code  = zip
      self.state     = zip_code.try(:state)
      self.district  = zip_code.try(:single_district)
    end
    self.save
  end

  def state_abbrev
    if state
      state.abbrev
    elsif district
      district.state.abbrev
    end
  end

  private

  def serializable_hash(options)
    super(methods: [:state_abbrev],
             only: [:address_1, :address_2, :city, :zip_code])
  end

  def update_nation_builder
    if (changed - ["district_id", "created_at", "updated_at"]).any?
      nb_args = Integration::NationBuilder.location_params(person.email, self.as_json)
{'fraction_non_alphanumeric': 0.08249496981891348, 'fraction_numerical': 0.004024144869215292, 'mean_word_length': 1.9652087475149105, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4515811', 'n_tokens_mistral': 915, 'n_tokens_neox': 879, 'n_words': 204}