# == Schema Information
#
# Table name: published_pages
#
#  id               :integer          not null, primary key
#  name             :string(40)       not null
#  title            :string(255)      not null
#  slug             :string(255)      not null
#  meta_description :string(255)
#  all_page_id      :integer          not null
#  nofollow         :boolean          default(FALSE), not null
#  number_of_views  :integer          default(0), not null
#  type_of          :integer          not null
#  article_id       :integer
#  priority         :integer          default(0), not null
#  small_text       :text(65535)
#  full_text        :text(16777215)
#  created_at       :datetime
#  created_by       :bigint(8)
#  updated_at       :datetime
#  updated_by       :bigint(8)
#

# 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
