class User < ApplicationRecord
  include Discard::Model
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  validates :name, :email, presence: true
  validates :email, uniqueness: true

  validates :password, :password_confirmation, presence: true, on: :create
  validates :password, confirmation: true

  has_many :blogs, dependent: :destroy
  has_many :comments, dependent: :destroy
  belongs_to :deleted_by, class_name: "User", optional: true

  after_discard do
    blogs.discard_all
    comments.discard_all
  end

  after_undiscard do
    blogs.where(deleted_by: nil).undiscard_all
    comments.where(deleted_by: nil).undiscard_all
  end

  def first_name
    self.name.split.first
  end

  def last_name
    self.name.split.last
  end
end
