Newer
Older
# == Schema Information
#
# Table name: communication_website_pages
#
# id :uuid not null, primary key
# about_type :string
# description :text
# path :text
# position :integer default(0), not null
# slug :string
# title :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid
# communication_website_id :uuid not null
# parent_id :uuid
# university_id :uuid not null
#
# Indexes
#
# index_communication_website_pages_on_about (about_type,about_id)
# index_communication_website_pages_on_communication_website_id (communication_website_id)
# index_communication_website_pages_on_parent_id (parent_id)
# index_communication_website_pages_on_related_category_id (related_category_id)
# index_communication_website_pages_on_university_id (university_id)
#
# Foreign Keys
#
# fk_rails_... (communication_website_id => communication_websites.id)
# fk_rails_... (parent_id => communication_website_pages.id)
# fk_rails_... (related_category_id => communication_website_categories.id)
# fk_rails_... (university_id => universities.id)
#
include WithSlug # We override slug_unavailable? method
belongs_to :website,
foreign_key: :communication_website_id
class_name: 'Communication::Website::Category',
optional: true
belongs_to :parent,
class_name: 'Communication::Website::Page',
optional: true
has_one :imported_page,
class_name: 'Communication::Website::Imported::Page',
dependent: :nullify
has_many :children,
class_name: 'Communication::Website::Page',
after_save :update_children_paths, if: :saved_change_to_path?
def best_featured_image(fallback: true)
return featured_image if featured_image.attached?
best_image = parent&.best_featured_image(fallback: false)
best_image ||= featured_image if fallback
best_image
end
def update_children_paths
children.each do |child|
child.update_column :path, child.generated_path
child.update_children_paths
end
end
self.class.unscoped
.where(communication_website_id: self.communication_website_id, slug: slug)
.where.not(id: self.id)
.exists?