# All published pages
class PublishedPage < ApplicationRecord
  extend FriendlyId
  include FriendlyFinder
  friendly_id :title, use: :slugged
  belongs_to :all_page
  belongs_to :article, optional: true

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

  def published
    true
  end

  def created_name
    admin = Admin.find(created_by)
    admin.blank? ? 'nieznany' : admin.description
  end

  def created_date
    created_at.to_date
  end

  def article_friendly
    ret = ''
    if type_of == 3 && article
      pp = article.published_pages.where('type_of = 2').first
      ret = pp.friendly_id unless pp.blank?
    end
    ret
  end
end
