<commit_before>require 'digest/sha1'

class User < ActiveRecord::Base

  acts_as_authorized_user
  acts_as_authorizable

  # Virtual attribute for the unencrypted password
  attr_accessor :password

  has_many :ideas, :foreign_key => 'inventor_id' do
    def recent_visible(opts = {})
      Idea.populate_comment_counts(
        find :all, opts.reverse_merge(
          :conditions => { :hidden => false, 'users.state' => 'active' },
          :include => [:tags, :inventor],
          :order => 'ideas.created_at desc',
          :limit => 10))
    end
  end
  has_many :currents, :foreign_key => 'inventor_id'
  has_many :comments, :foreign_key => 'author_id' do
    def recent_visible(opts = {})
      find :all, opts.reverse_merge(
        :conditions => { 'comments.hidden' => false, 'ideas.hidden' => false, 'users.state' => 'active' },
        :include => [:idea, :author],
        :order => 'comments.created_at desc',
        :limit => 10)
    end
  end
  has_many :votes do
    def recent_visible(opts = {})
      find :all, opts.reverse_merge(
        :include => { :idea => :inventor },
        :conditions => { 'ideas.hidden' => false, 'users.state' => 'active' },
        :order => 'votes.id desc',
        :limit => 10)
    end
  end
  belongs_to :department
  has_and_belongs_to_many :life_cycle_steps, :join_table => 'life_cycle_steps_admins', :order => 'life_cycle_id, position'
  belongs_to :postal_code
  has_many :roles_users, :dependent => :delete_all
  has_many :roles, :through => :roles_users do
    def for_name(name)
      find :all, :conditions => { :name => name }
    end
  end
  has_and_belongs_to_many :subscribed_ideas,    :class_name => 'Idea',    :join_table => 'ideas_subscribers'
  has_and_belongs_to_many :subscribed_currents, :class_name => 'Current', :join_table => 'currents_subscribers'

  # OAuth support
  has_many :client_applications
  has_many :tokens, :class_name => 'OauthToken', :order => 'authorized_at desc', :include => [:client_application]
  has_many :relationships, :foreign_key => "follower_id",
                           :dependent => :destroy
  has_many :following, :through => :relationships, :source => :followed

  validates_presence_of     :name
  validates_presence_of     :email
  #validates_presence_of     :zip_code
  validates_presence_of     :department_id
  validates_presence_of     :password,                   :if => :password_required?
  validates_presence_of     :password_confirmation,      :if => :password_confirmation_required?
  validates_length_of       :password, :within => 4..40, :if => :password_required?
  validates_confirmation_of :password,                   :if => :password_required?
  validates_length_of       :name,     :within => 4..100
  validates_length_of       :email,    :within => 3..100
  validates_format_of       :email,    :with => /^[A-Z0-9._%+-]+@bdo\.dk$/i
  validates_uniqueness_of   :email, :case_sensitive => false
  validates_acceptance_of   :terms_of_service, :allow_nil => false, :if => 'new_record?'
  before_save :encrypt_password

  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :name, :email, :password, :password_confirmation, :zip_code, :terms_of_service, :tweet_ideas, :facebook_post_ideas, :notify_on_comments, :notify_on_state, :department_id, :phone

  unless !User.table_exists?
    acts_as_tsearch :fields => %w(name email zip_code )
  end

  acts_as_state_machine :initial => :pending
  state :passive
  state :pending, :enter => :registered
  state :active,  :enter => :do_activate
  state :suspended
  state :deleted, :enter => :do_delete

  event :register do
    transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) }
  end

  event :activate do
    transitions :from => :pending, :to => :active<commit_msg>changed the email validation code<commit_after>require 'digest/sha1'

class User < ActiveRecord::Base

  acts_as_authorized_user
  acts_as_authorizable

  # Virtual attribute for the unencrypted password
  attr_accessor :password

  has_many :ideas, :foreign_key => 'inventor_id' do
    def recent_visible(opts = {})
      Idea.populate_comment_counts(
        find :all, opts.reverse_merge(
          :conditions => { :hidden => false, 'users.state' => 'active' },
          :include => [:tags, :inventor],
          :order => 'ideas.created_at desc',
          :limit => 10))
    end
  end
  has_many :currents, :foreign_key => 'inventor_id'
  has_many :comments, :foreign_key => 'author_id' do
    def recent_visible(opts = {})
      find :all, opts.reverse_merge(
        :conditions => { 'comments.hidden' => false, 'ideas.hidden' => false, 'users.state' => 'active' },
        :include => [:idea, :author],
        :order => 'comments.created_at desc',
        :limit => 10)
    end
  end
  has_many :votes do
    def recent_visible(opts = {})
      find :all, opts.reverse_merge(
        :include => { :idea => :inventor },
        :conditions => { 'ideas.hidden' => false, 'users.state' => 'active' },
        :order => 'votes.id desc',
        :limit => 10)
    end
  end
  belongs_to :department
  has_and_belongs_to_many :life_cycle_steps, :join_table => 'life_cycle_steps_admins', :order => 'life_cycle_id, position'
  belongs_to :postal_code
  has_many :roles_users, :dependent => :delete_all
  has_many :roles, :through => :roles_users do
    def for_name(name)
      find :all, :conditions => { :name => name }
    end
  end
  has_and_belongs_to_many :subscribed_ideas,    :class_name => 'Idea',    :join_table => 'ideas_subscribers'
  has_and_belongs_to_many :subscribed_currents, :class_name => 'Current', :join_table => 'currents_subscribers'

  # OAuth support
  has_many :client_applications
  has_many :tokens, :class_name => 'OauthToken', :order => 'authorized_at desc', :include => [:client_application]
  has_many :relationships, :foreign_key => "follower_id",
                           :dependent => :destroy
  has_many :following, :through => :relationships, :source => :followed

  validates_presence_of     :name
  validates_presence_of     :email
  #validates_presence_of     :zip_code
  validates_presence_of     :department_id
  validates_presence_of     :password,                   :if => :password_required?
  validates_presence_of     :password_confirmation,      :if => :password_confirmation_required?
  validates_length_of       :password, :within => 4..40, :if => :password_required?
  validates_confirmation_of :password,                   :if => :password_required?
  validates_length_of       :name,     :within => 4..100
  validates_length_of       :email,    :within => 3..100
  #validates_format_of       :email,    :with => /^[A-Z0-9._%+-]+@bdo\.dk$/i
  validates_format_of       :email,    :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates_uniqueness_of   :email, :case_sensitive => false
  validates_acceptance_of   :terms_of_service, :allow_nil => false, :if => 'new_record?'
  before_save :encrypt_password

  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :name, :email, :password, :password_confirmation, :zip_code, :terms_of_service, :tweet_ideas, :facebook_post_ideas, :notify_on_comments, :notify_on_state, :department_id, :phone

  unless !User.table_exists?
    acts_as_tsearch :fields => %w(name email zip_code )
  end

  acts_as_state_machine :initial => :pending
  state :passive
  state :pending, :enter => :registered
  state :active,  :enter => :do_activate
  state :suspended
  state :deleted, :enter => :do_delete

  event :register do
    transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) }
  end

  event :activate do
    transitions :from => :pending, :to => :active
{'fraction_non_alphanumeric': 0.12677472044226662, 'fraction_numerical': 0.005779620555346149, 'mean_word_length': 3.6064814814814814, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 126, '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': '1411226', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2629, 'n_words': 599}