<commit_before>include GoingPostal
class Signature < ActiveRecord::Base
  include ApplicationHelper
  belongs_to :user
  belongs_to :petition

  before_validation :format_zipcode
  before_save :sanitize_input
  validates_presence_of :first_name, :last_name, :country_code, :petition_id,
    message: "This can't be blank."

  validates :email, email: true
  validates :zipcode, length: { maximum: 12 }
  validate :country_code, :arbitrary_opinion_of_country_string_validity

  include ActionView::Helpers::DateHelper

  def arbitrary_opinion_of_country_string_validity
    begin
      IsoCountryCodes.find(country_code)
    rescue IsoCountryCodes::UnknownCodeError
      errors.add(:country_code, "Country Code might come from a spam bot.")
    end

  end

  def name
    [first_name, last_name].join(' ')
  end

  def location
    if anonymous
      country_code
    else
      [city, state, country_code].select(&:present?).join(', ')
    end
  end

  def time_ago
    time_ago_in_words(created_at)
  end

  def to_petition_csv_line
    [name, email, city, state_symbol, full_country_name]
  end

  def state_symbol
    us_states_hash[state]
  end

  def full_country_name
    IsoCountryCodes.find(country_code).name
  end

  private
  def format_zipcode
    zipcode = GoingPostal.format_zipcode(zipcode, country_code) || zipcode
  end

  def sanitize_input
    self.first_name = Sanitize.clean(first_name)
    self.last_name = Sanitize.clean(last_name)
    self.street_address = Sanitize.clean(street_address)
    self.email = Sanitize.clean(email)
    self.city = Sanitize.clean(city)
    self.state = Sanitize.clean(state)
    self.country_code = Sanitize.clean(country_code)
    self.zipcode = Sanitize.clean(zipcode)
  end

  def validate_zipcode
    unless GoingPostal.valid_zipcode?(zipcode, country_code)
      errors.add(:zipcode, "Invalid zip/postal code for country")
    end
  end
end
<commit_msg>Fix error where old petitions with invalid countries will not download<commit_after>include GoingPostal
class Signature < ActiveRecord::Base
  include ApplicationHelper
  belongs_to :user
  belongs_to :petition

  before_validation :format_zipcode
  before_save :sanitize_input
  validates_presence_of :first_name, :last_name, :country_code, :petition_id,
    message: "This can't be blank."

  validates :email, email: true
  validates :zipcode, length: { maximum: 12 }
  validate :country_code, :arbitrary_opinion_of_country_string_validity

  include ActionView::Helpers::DateHelper

  def arbitrary_opinion_of_country_string_validity
    if full_country_name.nil?
      errors.add(:country_code, "Country Code might come from a spam bot.")
    end
  end

  def name
    [first_name, last_name].join(' ')
  end

  def location
    if anonymous
      country_code
    else
      [city, state, country_code].select(&:present?).join(', ')
    end
  end

  def time_ago
    time_ago_in_words(created_at)
  end

  def to_petition_csv_line
    [name, email, city, state_symbol, full_country_name]
  end

  def state_symbol
    us_states_hash[state]
  end

  def full_country_name
    begin
      IsoCountryCodes.find(country_code).name
    rescue IsoCountryCodes::UnknownCodeError
      nil
    end
  end

  private
  def format_zipcode
    zipcode = GoingPostal.format_zipcode(zipcode, country_code) || zipcode
  end

  def sanitize_input
    self.first_name = Sanitize.clean(first_name)
    self.last_name = Sanitize.clean(last_name)
    self.street_address = Sanitize.clean(street_address)
    self.email = Sanitize.clean(email)
    self.city = Sanitize.clean(city)
    self.state = Sanitize.clean(state)
    self.country_code = Sanitize.clean(country_code)
    self.zipcode = Sanitize.clean(zipcode)
  end

  def validate_zipcode
    unless GoingPostal.valid_zipcode?(zipcode, country_code)
      errors.add(:zipcode, "Invalid zip/postal code for country")
    end
  end
end

{'fraction_non_alphanumeric': 0.07692307692307693, 'fraction_numerical': 0.0010290712631849757, 'mean_word_length': 4.029754204398448, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6778631', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1265, 'n_words': 279}