<commit_before>require "sentient_user/sentient_user"

# Someone who either appears in race results or who is added as a member of a racing association
#
# Names are _not_ unique. In fact, there are many business rules about names. See Aliases and Names.
class Person < ActiveRecord::Base
  include Comparable
  include Export::People
  include Names::Nameable
  include People::Aliases
  include People::Ages
  include People::Authorization
  include People::Export
  include People::Membership
  include People::Merge
  include People::Names
  include People::Numbers
  include RacingOnRails::VestalVersions::Versioned
  include SentientUser

  acts_as_authentic do |config|
    config.crypto_provider Authlogic::CryptoProviders::Sha512
    config.validates_length_of_login_field_options within: 3..100, allow_nil: true, allow_blank: true
    config.validates_format_of_login_field_options with: Authlogic::Regex.login,
                                                   message: I18n.t('error_messages.login_invalid',
                                                   default: "should use only letters, numbers, spaces, and .-_@ please."),
                                                   allow_nil: true,
                                                   allow_blank: true

    config.validates_uniqueness_of_login_field_options allow_blank: true, allow_nil: true, case_sensitive: false
    config.validates_confirmation_of_password_field_options unless: Proc.new { |user| user.password.blank? }
    config.validates_length_of_password_field_options  minimum: 4, allow_nil: true, allow_blank: true
    config.validates_length_of_password_confirmation_field_options  minimum: 4, allow_nil: true, allow_blank: true
    config.validate_email_field false
    config.disable_perishable_token_maintenance true
    config.maintain_sessions false
  end

  before_validation :find_associated_records
  before_validation :set_membership_dates
  validate :membership_dates
  before_destroy :ensure_no_results

  has_many :events, foreign_key: "promoter_id"
  has_and_belongs_to_many :editable_events, class_name: "Event", foreign_key: "editor_id", join_table: "editors_events"
  has_many :results
  belongs_to :team

  attr_accessor :year

  CATEGORY_FIELDS = [ :bmx_category, :ccx_category, :dh_category, :mtb_category, :road_category, :track_category ]

  def self.where_name_or_number_like(name)
    return Person.none if name.blank?

    Person.
      where(
        "people.name like :name_like or aliases.name like :name_like or race_numbers.value = :name",
        name_like: "%#{name.strip}%", name: name.strip
      ).
      includes(:aliases).
      includes(:race_numbers).
      includes(:team).
      references(:aliases).
      references(:race_numbers).
      order(:last_name, :first_name)
  end

  def self.find_by_info(name, email = nil, home_phone = nil)
    if name.present?
      Person.find_by_name(name)
    else
      Person.where(
        "(email = ? and email <> '' and email is not null) or (home_phone = ? and home_phone <> '' and home_phone is not null)",
        email, home_phone
      ).first
    end
  end

  # interprets dates returned in sql above for member export
  def self.lic_check(lic, lic_date)
    if lic_date && lic.to_i > 0
      case lic_date
      when Date, Time, DateTime
        (lic_date > Time.zone.today) ? "current" : "CHECK LIC!"
      else
        (Date.strptime(lic_date, "%m/%d/%Y") > Time.zone.today) ? "current" : "CHECK LIC!"
      end
    else
      "NOT ON FILE"
    end
  end

  # Find Person with most recent. If no results, select the most recently updated Person.
  def self.select_by_recent_activity(people)
    results = people.to_a.inject([]) { |r, person| r + person.results }<commit_msg>Use map and flatten rather than goofy inject when combining results<commit_after>require "sentient_user/sentient_user"

# Someone who either appears in race results or who is added as a member of a racing association
#
# Names are _not_ unique. In fact, there are many business rules about names. See Aliases and Names.
class Person < ActiveRecord::Base
  include Comparable
  include Export::People
  include Names::Nameable
  include People::Aliases
  include People::Ages
  include People::Authorization
  include People::Export
  include People::Membership
  include People::Merge
  include People::Names
  include People::Numbers
  include RacingOnRails::VestalVersions::Versioned
  include SentientUser

  acts_as_authentic do |config|
    config.crypto_provider Authlogic::CryptoProviders::Sha512
    config.validates_length_of_login_field_options within: 3..100, allow_nil: true, allow_blank: true
    config.validates_format_of_login_field_options with: Authlogic::Regex.login,
                                                   message: I18n.t('error_messages.login_invalid',
                                                   default: "should use only letters, numbers, spaces, and .-_@ please."),
                                                   allow_nil: true,
                                                   allow_blank: true

    config.validates_uniqueness_of_login_field_options allow_blank: true, allow_nil: true, case_sensitive: false
    config.validates_confirmation_of_password_field_options unless: Proc.new { |user| user.password.blank? }
    config.validates_length_of_password_field_options  minimum: 4, allow_nil: true, allow_blank: true
    config.validates_length_of_password_confirmation_field_options  minimum: 4, allow_nil: true, allow_blank: true
    config.validate_email_field false
    config.disable_perishable_token_maintenance true
    config.maintain_sessions false
  end

  before_validation :find_associated_records
  before_validation :set_membership_dates
  validate :membership_dates
  before_destroy :ensure_no_results

  has_many :events, foreign_key: "promoter_id"
  has_and_belongs_to_many :editable_events, class_name: "Event", foreign_key: "editor_id", join_table: "editors_events"
  has_many :results
  belongs_to :team

  attr_accessor :year

  CATEGORY_FIELDS = [ :bmx_category, :ccx_category, :dh_category, :mtb_category, :road_category, :track_category ]

  def self.where_name_or_number_like(name)
    return Person.none if name.blank?

    Person.
      where(
        "people.name like :name_like or aliases.name like :name_like or race_numbers.value = :name",
        name_like: "%#{name.strip}%", name: name.strip
      ).
      includes(:aliases).
      includes(:race_numbers).
      includes(:team).
      references(:aliases).
      references(:race_numbers).
      order(:last_name, :first_name)
  end

  def self.find_by_info(name, email = nil, home_phone = nil)
    if name.present?
      Person.find_by_name(name)
    else
      Person.where(
        "(email = ? and email <> '' and email is not null) or (home_phone = ? and home_phone <> '' and home_phone is not null)",
        email, home_phone
      ).first
    end
  end

  # interprets dates returned in sql above for member export
  def self.lic_check(lic, lic_date)
    if lic_date && lic.to_i > 0
      case lic_date
      when Date, Time, DateTime
        (lic_date > Time.zone.today) ? "current" : "CHECK LIC!"
      else
        (Date.strptime(lic_date, "%m/%d/%Y") > Time.zone.today) ? "current" : "CHECK LIC!"
      end
    else
      "NOT ON FILE"
    end
  end

  # Find Person with most recent. If no results, select the most recently updated Person.
  def self.select_by_recent_activity(people)
    results = people.map(&:results).flatten
{'fraction_non_alphanumeric': 0.08266985138004246, 'fraction_numerical': 0.0031847133757961785, 'mean_word_length': 3.231892195395845, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1028913', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2197, 'n_words': 582}