# Model for allpages that are not published
class AllPage < ApplicationRecord
  extend FriendlyId
  include FriendlyFinder
  friendly_id :title, use: :slugged
  validates :name, presence: true, uniqueness: true
  belongs_to :article, optional: true
  has_one :published_page
  before_destroy :b_destroy

  def should_generate_new_friendly_id?
    slug.blank? || will_save_change_to_attribute?(:title)
  end

  PAGE_TYPES = {
    1 => 'Strona zwykła',
    2 => 'Strona z listą wpisów',
    3 => 'Artykuł'
  }.freeze
  FORM_PAGE_TYPES = [
    ['Strona zwykła', '1'],
    ['Strona z listą wpisów', '2']
  ].freeze

  def b_destroy
    published_page && published_page.destroy
  end

  def created_name
    Admin.find(updated_by).description
  end

  def created_date
    updated_at.to_date
  end
end
