class ForumUser < ActiveRecord::Base
  #attr_accessible :bio, :email, :firstname, :name, :password, :password_confirmation, :avatar, :moderator
  
  has_many :topic_vieweds, :through => :forum_user_topic_vieweds, :source => :forum_topic
  
  has_many :forum_user_topic_vieweds
  
  
  belongs_to :mail_profile
  
  has_many :followed_topics
  has_many :forum_topics, :through => :followed_topics do
    def enabled
      where(":followed_topic.enabled = ?", true)
    end
  end
  
  has_secure_password
  validates_presence_of :password, :on => :create
  validates :email, :presence => true, :uniqueness => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
  validates :name, :presence => true
  validates :firstname, :presence => true
  
  #validates :mail_profile_id, :uniqueness => true
  
  mount_uploader :avatar, AvatarUploader
  
  has_many :messages, :class_name => "ForumMessage"
  has_many :topics, :class_name => "ForumTopic"
  
  has_many :images, :class_name => "ForumUserImage"
  
  before_create { generate_token(:auth_token) }
  attr_accessor :skip_sheet_validation

  def avatar_url
    self.avatar? ? self.avatar.square.url : "/default_avatar.png"
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while ForumUser.exists?(column => self[column])
  end
  
  
end
