<commit_before>class DwpCheck < ActiveRecord::Base

  include CommonScopes

  belongs_to :created_by, class_name: 'User'
  belongs_to :office

  before_create :generate_unique_number
  after_create :generate_api_token

  validates :last_name, :dob, :ni_number, :office_id, presence: true
  validates :last_name, length: { minimum: 2 }, allow_blank: true

  validate :date_to_check_must_be_valid
  validate :date_of_birth_must_be_valid

  validates :ni_number, format: {
    with: /\A(?!BG|GB|NK|KN|TN|NT|ZZ)[ABCEGHJ-PRSTW-Z][ABCEGHJ-NPRSTW-Z]\d{6}[A-D]\z/
  }, allow_blank: true

  scope :by_office, lambda { |office_id|
    joins('left outer join users on dwp_checks.created_by_id = users.id').
      where('dwp_checks.office_id = ?', office_id)
  }
  scope :by_office_grouped_by_type, lambda { |office_id|
    joins('left outer join users on dwp_checks.created_by_id = users.id').
      where('dwp_checks.office_id = ?', office_id).
      group(:dwp_result).
      order('length(dwp_result)')
  }

  def date_to_check_must_be_valid
    if date_to_check.present? && (
      date_to_check > Date.today ||
      date_to_check < Date.today - 3.months
    )
      errors.add(:date_to_check, 'must be in the last 3 months')
    end
  end

  def date_of_birth_must_be_valid
    if dob.present? && dob >= Date.today
      errors.add(:dob, 'must be before today')
    end
  end

  def ni_number=(val)
    if val.nil?
      self[:ni_number] = nil
    else
      self[:ni_number] = val.upcase if val.present?
    end
  end

private

  def generate_unique_number
    new_uid = ''
    loop do
      new_uid = SecureRandom.hex(4).scan(/.{1,4}/).join('-')
      break if DwpCheck.find_by(unique_number: new_uid).nil?
    end
    self.unique_number = new_uid
  end

  def generate_api_token
    short_name = created_by.name.gsub(' ', '').downcase.truncate(27)
    self.our_api_token = "#{short_name}@#{created_at.strftime('%y%m%d%H%M%S')}.#{unique_number}"
  end
end
<commit_msg>Save model after generating token<commit_after>class DwpCheck < ActiveRecord::Base

  include CommonScopes

  belongs_to :created_by, class_name: 'User'
  belongs_to :office

  before_create :generate_unique_number
  after_create :generate_api_token

  validates :last_name, :dob, :ni_number, :office_id, presence: true
  validates :last_name, length: { minimum: 2 }, allow_blank: true

  validate :date_to_check_must_be_valid
  validate :date_of_birth_must_be_valid

  validates :ni_number, format: {
    with: /\A(?!BG|GB|NK|KN|TN|NT|ZZ)[ABCEGHJ-PRSTW-Z][ABCEGHJ-NPRSTW-Z]\d{6}[A-D]\z/
  }, allow_blank: true

  scope :by_office, lambda { |office_id|
    joins('left outer join users on dwp_checks.created_by_id = users.id').
      where('dwp_checks.office_id = ?', office_id)
  }
  scope :by_office_grouped_by_type, lambda { |office_id|
    joins('left outer join users on dwp_checks.created_by_id = users.id').
      where('dwp_checks.office_id = ?', office_id).
      group(:dwp_result).
      order('length(dwp_result)')
  }

  def date_to_check_must_be_valid
    if date_to_check.present? && (
      date_to_check > Date.today ||
      date_to_check < Date.today - 3.months
    )
      errors.add(:date_to_check, 'must be in the last 3 months')
    end
  end

  def date_of_birth_must_be_valid
    if dob.present? && dob >= Date.today
      errors.add(:dob, 'must be before today')
    end
  end

  def ni_number=(val)
    if val.nil?
      self[:ni_number] = nil
    else
      self[:ni_number] = val.upcase if val.present?
    end
  end

private

  def generate_unique_number
    new_uid = ''
    loop do
      new_uid = SecureRandom.hex(4).scan(/.{1,4}/).join('-')
      break if DwpCheck.find_by(unique_number: new_uid).nil?
    end
    self.unique_number = new_uid
  end

  def generate_api_token
    short_name = created_by.name.gsub(' ', '').downcase.truncate(27)
    self.our_api_token = "#{short_<PASSWORD>}@#{created_at.strftime('%y%m%d%H%M%S')}.#{unique_number}"
    self.save!
  end
end

{'fraction_non_alphanumeric': 0.12440191387559808, 'fraction_numerical': 0.004532863258625032, 'mean_word_length': 3.634772462077013, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2936391', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1485, 'n_words': 281}