<commit_before># == USERS DOCUMENTATION
#
# The users table represents...  USERS!!!
#
# When a user signs up to the application they can be put into several states and have
# different requirements depending on how they signed up.
#

##

# == Schema Information
#
# Table name: users
#
#  id                :integer(4)      not null, primary key
#  first_name        :string(255)
#  last_name         :string(255)
#  email             :string(255)
#  state             :string(255)
#  account_id        :integer(4)
#  customer_cim_id   :string(255)
#  password_salt     :string(255)
#  crypted_password  :string(255)
#  perishable_token  :string(255)
#  persistence_token :string(255)
#  access_token      :string(255)
#  comments_count    :integer(4)      default(0)
#  created_at        :datetime
#  updated_at        :datetime
#

class User < ActiveRecord::Base
  include TransactionAccountable
  include UserCim

  acts_as_authentic do |config|
    config.validate_email_field
    config.validates_length_of_password_field_options( :minimum => 6, :on => :update, :if => :password_changed? )

    # So that Authlogic will not use the LOWER() function when checking login, allowing for benefit of column index.
    config.validates_uniqueness_of_login_field_options :case_sensitive => true
    config.validates_uniqueness_of_email_field_options :case_sensitive => true

    config.validate_login_field = true;
    config.validate_email_field = true;
  end

  before_validation :sanitize_data
  before_validation :before_validation_on_create, :on => :create
  before_create :start_store_credits, :subscribe_to_newsletters
  after_create  :set_referral_registered_at

  belongs_to :account

  has_many    :users_newsletters
  has_many    :newsletters, through: :users_newsletters

  has_many  :referrals, class_name: 'Referral', foreign_key: 'referring_user_id' # people you have tried to referred
  has_one   :referree,  class_name: 'Referral', foreign_key: 'referral_user_id' # person who referred you

  has_one     :store_credit
  has_many    :orders
  has_many    :comments
  has_many    :customer_service_comments, as:         :commentable,
                                          class_name: 'Comment'
  has_many    :shipments, :through => :orders
  has_many    :finished_orders,           -> { where(state: ['complete', 'paid']) },  class_name: 'Order'
  has_many    :completed_orders,          -> { where(state: 'complete') },            class_name: 'Order'

  has_many    :phones,          dependent: :destroy,       as: :phoneable
  has_one     :primary_phone, -> { where(primary: true) }, as: :phoneable, class_name: 'Phone'

  has_many    :addresses,       dependent: :destroy,       as: :addressable

  has_one     :default_billing_address,   -> { where(billing_default: true, active: true) },
                                          as:         :addressable,
                                          class_name: 'Address'

  has_many    :billing_addresses,         -> { where(active: true) },
                                          as:         :addressable,
                                          class_name: 'Address'

  has_one     :default_shipping_address,  -> { where(default: true, active: true) },
                                          as:         :addressable,
                                          class_name: 'Address'

  has_many     :shipping_addresses,       -> { where(active: true) },
                                          as:         :addressable,
                                          class_name: 'Address'

  has_many    :user_roles,                dependent: :destroy
  has_many    :roles,                     through: :user_roles

  has_many    :carts,                     dependent: :destroy

  has_many    :cart_items
  has_many    :shopping_cart_items, -> { where(active: true, item_type_id: ItemType::SHOPPING_CART_ID) }, class_name: 'CartItem'
  has_many    :wish_list_items,     -> { where(active: true, item_type_id: ItemType::WISH_LIST_ID) },     class_name: 'CartItem'
  has_many    :saved_cart_items,    -> { where(active: true, item_type_id: ItemType::SAVE_FOR_LATER) },   class_name: 'CartItem'
  has_many    :purchased_items,     -> { where(active: true, item_type_id: ItemType::PURCHASED_ID) },     class_name: 'CartItem'
  has_many    :deleted_cart_items,  -> { where( active: false) }, class_name: 'CartItem'
  has_many    :payment_profiles
  has_many    :transaction_ledgers, as: :accountable

  has_many    :return_authorizations
  has_many    :authored_return_authorizations, class_name: 'ReturnAuthorization', foreign_key: 'author_id'

  validates :first_name,  presence: true, if: :registered_user?,
                          format:   { with: CustomValidators::Names.name_validator },
                          length:   { maximum: 30 }
  validates :last_name,   :presence => true, :if => :registered_user?,
                          :format   => { :with => CustomValidators::Names.name_validator },
                          :length => { :maximum => 35 }
  validates :email,       :presence => true,
                          :uniqueness => true,##  This should be done at the DB this is too expensive in rails
                          :format   => { :with => CustomValidators::Emails.email_validator },
                          :length => { :maximum => 255 }
  #validates :password,    :presence => { :if => :password_required? }, :confirmation => true

  accepts_nested_attributes_for :addresses, :user_roles
  accepts_nested_attributes_for :phones, :reject_if => lambda { |t| ( t['display_number'].gsub(/\D+/, '').blank?) }
  accepts_nested_attributes_for :customer_service_comments, :reject_if => proc { |attributes| attributes['note'].strip.blank? }

  state_machine :state, :initial => :active do
    state :inactive
    state :active
    state :canceled

    event :activate do
      transition all => :active, :unless => :active?
    end

    event :cancel do
      transition :from => [:inactive, :active, :canceled], :to => :canceled
    end<commit_msg>remove commented code<commit_after># == USERS DOCUMENTATION
#
# The users table represents...  USERS!!!
#
# When a user signs up to the application they can be put into several states and have
# different requirements depending on how they signed up.
#

##

# == Schema Information
#
# Table name: users
#
#  id                :integer(4)      not null, primary key
#  first_name        :string(255)
#  last_name         :string(255)
#  email             :string(255)
#  state             :string(255)
#  account_id        :integer(4)
#  customer_cim_id   :string(255)
#  password_salt     :string(255)
#  crypted_password  :string(255)
#  perishable_token  :string(255)
#  persistence_token :string(255)
#  access_token      :string(255)
#  comments_count    :integer(4)      default(0)
#  created_at        :datetime
#  updated_at        :datetime
#

class User < ActiveRecord::Base
  include TransactionAccountable
  include UserCim

  acts_as_authentic do |config|
    config.validate_email_field
    config.validates_length_of_password_field_options( :minimum => 6, :on => :update, :if => :password_changed? )

    # So that Authlogic will not use the LOWER() function when checking login, allowing for benefit of column index.
    config.validates_uniqueness_of_login_field_options :case_sensitive => true
    config.validates_uniqueness_of_email_field_options :case_sensitive => true

    config.validate_login_field = true;
    config.validate_email_field = true;
  end

  before_validation :sanitize_data
  before_validation :before_validation_on_create, :on => :create
  before_create :start_store_credits, :subscribe_to_newsletters
  after_create  :set_referral_registered_at

  belongs_to :account

  has_many    :users_newsletters
  has_many    :newsletters, through: :users_newsletters

  has_many  :referrals, class_name: 'Referral', foreign_key: 'referring_user_id' # people you have tried to referred
  has_one   :referree,  class_name: 'Referral', foreign_key: 'referral_user_id' # person who referred you

  has_one     :store_credit
  has_many    :orders
  has_many    :comments
  has_many    :customer_service_comments, as:         :commentable,
                                          class_name: 'Comment'
  has_many    :shipments, :through => :orders
  has_many    :finished_orders,           -> { where(state: ['complete', 'paid']) },  class_name: 'Order'
  has_many    :completed_orders,          -> { where(state: 'complete') },            class_name: 'Order'

  has_many    :phones,          dependent: :destroy,       as: :phoneable
  has_one     :primary_phone, -> { where(primary: true) }, as: :phoneable, class_name: 'Phone'

  has_many    :addresses,       dependent: :destroy,       as: :addressable

  has_one     :default_billing_address,   -> { where(billing_default: true, active: true) },
                                          as:         :addressable,
                                          class_name: 'Address'

  has_many    :billing_addresses,         -> { where(active: true) },
                                          as:         :addressable,
                                          class_name: 'Address'

  has_one     :default_shipping_address,  -> { where(default: true, active: true) },
                                          as:         :addressable,
                                          class_name: 'Address'

  has_many     :shipping_addresses,       -> { where(active: true) },
                                          as:         :addressable,
                                          class_name: 'Address'

  has_many    :user_roles,                dependent: :destroy
  has_many    :roles,                     through: :user_roles

  has_many    :carts,                     dependent: :destroy

  has_many    :cart_items
  has_many    :shopping_cart_items, -> { where(active: true, item_type_id: ItemType::SHOPPING_CART_ID) }, class_name: 'CartItem'
  has_many    :wish_list_items,     -> { where(active: true, item_type_id: ItemType::WISH_LIST_ID) },     class_name: 'CartItem'
  has_many    :saved_cart_items,    -> { where(active: true, item_type_id: ItemType::SAVE_FOR_LATER) },   class_name: 'CartItem'
  has_many    :purchased_items,     -> { where(active: true, item_type_id: ItemType::PURCHASED_ID) },     class_name: 'CartItem'
  has_many    :deleted_cart_items,  -> { where( active: false) }, class_name: 'CartItem'
  has_many    :payment_profiles
  has_many    :transaction_ledgers, as: :accountable

  has_many    :return_authorizations
  has_many    :authored_return_authorizations, class_name: 'ReturnAuthorization', foreign_key: 'author_id'

  validates :first_name,  presence: true, if: :registered_user?,
                          format:   { with: CustomValidators::Names.name_validator },
                          length:   { maximum: 30 }
  validates :last_name,   :presence => true, :if => :registered_user?,
                          :format   => { :with => CustomValidators::Names.name_validator },
                          :length => { :maximum => 35 }
  validates :email,       :presence => true,
                          :uniqueness => true,##  This should be done at the DB this is too expensive in rails
                          :format   => { :with => CustomValidators::Emails.email_validator },
                          :length => { :maximum => 255 }

  accepts_nested_attributes_for :addresses, :user_roles
  accepts_nested_attributes_for :phones, :reject_if => lambda { |t| ( t['display_number'].gsub(/\D+/, '').blank?) }
  accepts_nested_attributes_for :customer_service_comments, :reject_if => proc { |attributes| attributes['note'].strip.blank? }

  state_machine :state, :initial => :active do
    state :inactive
    state :active
    state :canceled

    event :activate do
      transition all => :active, :unless => :active?
    end

    event :cancel do
      transition :from => [:inactive, :active, :canceled], :to => :canceled
    end
{'fraction_non_alphanumeric': 0.09980023306142834, 'fraction_numerical': 0.006991842850008324, 'mean_word_length': 2.4565592635212887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5171945', 'n_tokens_mistral': 3623, 'n_tokens_neox': 3494, 'n_words': 826}