- require 'xml/libxml'
- require 'digest/md5'
-
- has_many :traces
- has_many :diary_entries, :order => 'created_at DESC'
- has_many :messages, :foreign_key => :to_user_id
- has_many :new_messages, :class_name => "Message", :foreign_key => :to_user_id, :conditions => "message_read = 0"
- has_many :friends
-
- validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password'
- validates_uniqueness_of :display_name, :allow_nil => true
- validates_uniqueness_of :email
- validates_length_of :pass_crypt, :minimum => 8
- validates_length_of :display_name, :minimum => 3, :allow_nil => true
- validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
- validates_format_of :display_name, :with => /^[^\/;.,?]*$/
-
+ require "xml/libxml"
+
+ has_many :traces, -> { where(:visible => true) }
+ has_many :diary_entries, -> { order(:created_at => :desc) }
+ has_many :diary_comments, -> { order(:created_at => :desc) }
+ has_many :messages, -> { where(:to_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :foreign_key => :to_user_id
+ has_many :new_messages, -> { where(:to_user_visible => true, :message_read => false).order(:sent_on => :desc) }, :class_name => "Message", :foreign_key => :to_user_id
+ has_many :sent_messages, -> { where(:from_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :class_name => "Message", :foreign_key => :from_user_id
+ has_many :friends, -> { joins(:befriendee).where(:users => { :status => %w(active confirmed) }) }
+ has_many :friend_users, :through => :friends, :source => :befriendee
+ has_many :tokens, :class_name => "UserToken"
+ has_many :preferences, :class_name => "UserPreference"
+ has_many :changesets, -> { order(:created_at => :desc) }
+ has_many :changeset_comments, :foreign_key => :author_id
+ has_and_belongs_to_many :changeset_subscriptions, :class_name => "Changeset", :join_table => "changesets_subscribers", :foreign_key => "subscriber_id"
+ has_many :note_comments, :foreign_key => :author_id
+ has_many :notes, :through => :note_comments
+
+ has_many :client_applications
+ has_many :oauth_tokens, -> { order(:authorized_at => :desc).preload(:client_application) }, :class_name => "OauthToken"
+
+ has_many :blocks, :class_name => "UserBlock"
+ has_many :blocks_created, :class_name => "UserBlock", :foreign_key => :creator_id
+ has_many :blocks_revoked, :class_name => "UserBlock", :foreign_key => :revoker_id
+
+ has_many :roles, :class_name => "UserRole"
+
+ scope :visible, -> { where(:status => %w(pending active confirmed)) }
+ scope :active, -> { where(:status => %w(active confirmed)) }
+ scope :identifiable, -> { where(:data_public => true) }
+
+ has_attached_file :image,
+ :default_url => "/assets/:class/:attachment/:style.png",
+ :styles => { :large => "100x100>", :small => "50x50>" }
+
+ validates :display_name, :presence => true, :allow_nil => true, :length => 3..255,
+ :exclusion => %w(new terms save confirm confirm-email go_public reset-password forgot-password suspended)
+ validates :display_name, :if => proc { |u| u.display_name_changed? },
+ :uniqueness => { :case_sensitive => false }
+ validates :display_name, :if => proc { |u| u.display_name_changed? },
+ :format => { :with => %r{\A[^\x00-\x1f\x7f\ufffe\uffff/;.,?%#]*\z} }
+ validates :display_name, :if => proc { |u| u.display_name_changed? },
+ :format => { :with => /\A\S/, :message => "has leading whitespace" }
+ validates :display_name, :if => proc { |u| u.display_name_changed? },
+ :format => { :with => /\S\z/, :message => "has trailing whitespace" }
+ validates :email, :presence => true, :confirmation => true
+ validates :email, :if => proc { |u| u.email_changed? },
+ :uniqueness => { :case_sensitive => false }
+ validates :pass_crypt, :confirmation => true, :length => 8..255
+ validates :home_lat, :home_lon, :allow_nil => true, :numericality => true
+ validates :home_zoom, :allow_nil => true, :numericality => { :only_integer => true }
+ validates :preferred_editor, :inclusion => Editors::ALL_EDITORS, :allow_nil => true
+ validates :image, :attachment_content_type => { :content_type => %r{\Aimage/.*\Z} }
+
+ validates_email_format_of :email, :if => proc { |u| u.email_changed? }
+ validates_email_format_of :new_email, :allow_blank => true, :if => proc { |u| u.new_email_changed? }
+
+ after_initialize :set_defaults