class User < ActiveRecord::Base
  extend FriendlyId

  has_many :comments
  has_many :posts

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :nick,
            presence: true,
            uniqueness: true,
            length: {minimum: 2, maximum: 25},
            format: { with: /\A[a-zA-Z]+\z/, message: 'only allows letters' }
            # dodać potem możliwość cyfr i podkreślników

  validates :email,
            presence: true,
            length: {minimum: 4},
            uniqueness: true,
            format: { with: VALID_EMAIL_REGEX }

  validates :password,
            confirmation: true,
            length: {minimum: 4, maximum: 25}

  validates :password_confirmation,
            presence: true

  validate  :picture_size

  has_secure_password

  friendly_id :nick, use: :slugged

  mount_uploader :avatar, AvatarUploader

  def admin?
    self.admin
  end

  private

    def picture_size
      if avatar.size > 5.megabytes
        errors.add(:avatar, 'should be less than 5MB')
      end
    end

end