<commit_before>class User < ActiveRecord::Base

  mount_uploader :photo, PhotoUploader

  rolify role_join_table_name: 'roles_users'

  include Authority::UserAbilities
  include Authority::Abilities

  self.authorizer_name = "UserAuthorizer"

  #  For recent updates to consent/privacy policy/etc
  RECENT_UPDATE_DATE = "2015-04-15"

  # Include default devise modules. Others available are:
  # :confirmable, :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :timeoutable, :lockable

  # Callbacks
  after_create :set_forum_name, :send_welcome_email

  # Mappings
  TYPE = [['Diagnosed With Sleep Apnea', 'adult_diagnosed'],
          ['Concern That I May Have Sleep Apnea', 'adult_at_risk'],
          ['Family Member of an Adult with Sleep Apnea', 'caregiver_adult'],
          ['Family Member of a Child with Sleep Apnea', 'caregiver_child'],
          ['Provider', 'provider'],
          ['Researcher', 'researcher']]

  DEFAULT_SURVEYS = {
    adult_diagnosed: ['about-me', 'additional-information-about-me', 'about-my-family', 'my-health-conditions', 'my-sleep-pattern', 'my-sleep-quality', 'my-sleep-apnea', 'my-sleep-apnea-treatment', 'my-quality-of-life', 'my-interest-in-research'],
    adult_at_risk: ['about-me', 'additional-information-about-me', 'about-my-family', 'my-health-conditions', 'my-sleep-pattern', 'my-sleep-quality', 'my-risk-profile', 'my-quality-of-life', 'my-interest-in-research'],
    caregiver_adult: ['about-me', 'additional-information-about-me', 'about-my-family', 'my-interest-in-research'],
    caregiver_child: ['about-me', 'additional-information-about-me', 'about-my-family', 'my-interest-in-research']
  }

  # Concerns
  include CommonDataModel, Deletable

  attr_accessor :user_is_updating

  # Named Scopes
  scope :search_by_email, ->(terms) { where("LOWER(#{self.table_name}.email) LIKE ?", terms.to_s.downcase.gsub(/^| |$/, '%')) }
  scope :search, lambda { |arg| where( 'LOWER(first_name) LIKE ? or LOWER(last_name) LIKE ? or LOWER(email) LIKE ?', arg.to_s.downcase.gsub(/^| |$/, '%'), arg.to_s.downcase.gsub(/^| |$/, '%'), arg.to_s.downcase.gsub(/^| |$/, '%') ) }
  scope :providers, -> { current.where(provider: true) }
  scope :include_in_exports_and_reports, -> { where(include_in_exports: true) }

  # Model Validation
  validates_presence_of :first_name, :last_name

  validates :forum_name, allow_blank: true, uniqueness: true, format: { with: /\A[a-zA-Z0-9]*\Z/i }, unless: :update_by_user?
  validates :forum_name, allow_blank: false, uniqueness: true, format: { with: /\A[a-zA-Z0-9]+\Z/i }, if: :update_by_user?

  with_options unless: :is_provider? do |user|
    user.validates :over_eighteen, inclusion: { in: [true], message: "You must be over 18 years of age to sign up" }, allow_nil: true
    #user.validates :year_of_birth, presence: true, numericality: {only_integer: true, allow_nil: false, less_than_or_equal_to: -> (user){ Date.today.year - 18 }, greater_than_or_equal_to: -> (user){ 1900 }}
  end

  with_options if: :is_provider? do |user|
    user.validates :provider_name, allow_blank: true, uniqueness: true
    user.validates :slug, allow_blank: true, uniqueness: true, format: { with: /\A[a-z][a-z0-9\-]*[a-z0-9]\Z/i }
  end

  # Model Relationships
  belongs_to :my_provider, class_name: "User", foreign_key: 'provider_id'
  has_many :answer_sessions, -> { where deleted: false }
  has_many :answers, -> { where deleted: false }
  has_many :votes
  has_one :social_profile, -> { where deleted: false }
  has_many :notifications, -> { where deleted: false }
  has_many :research_topics, -> { where deleted: false }
  has_many :forums, -> { where deleted: false }
  has_many :topics, -> { where deleted: false }<commit_msg>User.votes will only get current votes<commit_after>class User < ActiveRecord::Base

  mount_uploader :photo, PhotoUploader

  rolify role_join_table_name: 'roles_users'

  include Authority::UserAbilities
  include Authority::Abilities

  self.authorizer_name = "UserAuthorizer"

  #  For recent updates to consent/privacy policy/etc
  RECENT_UPDATE_DATE = "2015-04-15"

  # Include default devise modules. Others available are:
  # :confirmable, :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :timeoutable, :lockable

  # Callbacks
  after_create :set_forum_name, :send_welcome_email

  # Mappings
  TYPE = [['Diagnosed With Sleep Apnea', 'adult_diagnosed'],
          ['Concern That I May Have Sleep Apnea', 'adult_at_risk'],
          ['Family Member of an Adult with Sleep Apnea', 'caregiver_adult'],
          ['Family Member of a Child with Sleep Apnea', 'caregiver_child'],
          ['Provider', 'provider'],
          ['Researcher', 'researcher']]

  DEFAULT_SURVEYS = {
    adult_diagnosed: ['about-me', 'additional-information-about-me', 'about-my-family', 'my-health-conditions', 'my-sleep-pattern', 'my-sleep-quality', 'my-sleep-apnea', 'my-sleep-apnea-treatment', 'my-quality-of-life', 'my-interest-in-research'],
    adult_at_risk: ['about-me', 'additional-information-about-me', 'about-my-family', 'my-health-conditions', 'my-sleep-pattern', 'my-sleep-quality', 'my-risk-profile', 'my-quality-of-life', 'my-interest-in-research'],
    caregiver_adult: ['about-me', 'additional-information-about-me', 'about-my-family', 'my-interest-in-research'],
    caregiver_child: ['about-me', 'additional-information-about-me', 'about-my-family', 'my-interest-in-research']
  }

  # Concerns
  include CommonDataModel, Deletable

  attr_accessor :user_is_updating

  # Named Scopes
  scope :search_by_email, ->(terms) { where("LOWER(#{self.table_name}.email) LIKE ?", terms.to_s.downcase.gsub(/^| |$/, '%')) }
  scope :search, lambda { |arg| where( 'LOWER(first_name) LIKE ? or LOWER(last_name) LIKE ? or LOWER(email) LIKE ?', arg.to_s.downcase.gsub(/^| |$/, '%'), arg.to_s.downcase.gsub(/^| |$/, '%'), arg.to_s.downcase.gsub(/^| |$/, '%') ) }
  scope :providers, -> { current.where(provider: true) }
  scope :include_in_exports_and_reports, -> { where(include_in_exports: true) }

  # Model Validation
  validates_presence_of :first_name, :last_name

  validates :forum_name, allow_blank: true, uniqueness: true, format: { with: /\A[a-zA-Z0-9]*\Z/i }, unless: :update_by_user?
  validates :forum_name, allow_blank: false, uniqueness: true, format: { with: /\A[a-zA-Z0-9]+\Z/i }, if: :update_by_user?

  with_options unless: :is_provider? do |user|
    user.validates :over_eighteen, inclusion: { in: [true], message: "You must be over 18 years of age to sign up" }, allow_nil: true
    #user.validates :year_of_birth, presence: true, numericality: {only_integer: true, allow_nil: false, less_than_or_equal_to: -> (user){ Date.today.year - 18 }, greater_than_or_equal_to: -> (user){ 1900 }}
  end

  with_options if: :is_provider? do |user|
    user.validates :provider_name, allow_blank: true, uniqueness: true
    user.validates :slug, allow_blank: true, uniqueness: true, format: { with: /\A[a-z][a-z0-9\-]*[a-z0-9]\Z/i }
  end

  # Model Relationships
  belongs_to :my_provider, class_name: "User", foreign_key: 'provider_id'
  has_many :answer_sessions, -> { where deleted: false }
  has_many :answers, -> { where deleted: false }
  has_many :votes, -> { where deleted: false }
  has_one :social_profile, -> { where deleted: false }
  has_many :notifications, -> { where deleted: false }
  has_many :research_topics, -> { where deleted: false }
  has_many :forums, -> { where deleted: false }
  has_many :topics, -> { where deleted: false }
{'fraction_non_alphanumeric': 0.1557161629434954, 'fraction_numerical': 0.006307490144546649, 'mean_word_length': 4.499277456647399, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '463903', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2584, 'n_words': 580}