Newer
Older
# == Schema Information
#
# Table name: communication_website_posts
#
# id :uuid not null, primary key
# description :text
# title :string
# created_at :datetime not null
# updated_at :datetime not null
# author_id :uuid indexed
# communication_website_id :uuid not null, indexed
# university_id :uuid not null, indexed
# index_communication_website_posts_on_author_id (author_id)
# index_communication_website_posts_on_communication_website_id (communication_website_id)
# index_communication_website_posts_on_university_id (university_id)
#
# Foreign Keys
#
# fk_rails_1e0d058a25 (university_id => universities.id)
# fk_rails_d1c1a10946 (communication_website_id => communication_websites.id)
# fk_rails_e0eec447b0 (author_id => university_people.id)
include WithFeaturedImage
include WithBlobs
has_one :imported_post,
class_name: 'Communication::Website::Imported::Post',
dependent: :destroy
has_and_belongs_to_many :categories,
class_name: 'Communication::Website::Category',
join_table: 'communication_website_categories_posts',
foreign_key: 'communication_website_post_id',
association_foreign_key: 'communication_website_category_id'
before_validation :set_published_at, if: :published_changed?
scope :ordered, -> { order(published_at: :desc, created_at: :desc) }
scope :recent, -> { order(published_at: :desc).limit(5) }
"content/posts/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" if published && published_at
dependencies += categories
dependencies += active_storage_blobs
if author.present?
dependencies += [author, author.author]
dependencies += author.active_storage_blobs
end
dependencies
[self] + explicit_active_storage_blobs
def url
return unless published
return if website.url.blank?
"#{website.url}#{website.index_for(:communication_posts).path}#{path}"
protected
def slug_unavailable?(slug)
self.class.unscoped
.where(communication_website_id: self.communication_website_id, slug: slug)
.where.not(id: self.id)
.exists?
self.published_at = Time.zone.now if published? && published_at.nil?
end
def inherited_blob_ids
[best_featured_image&.blob_id]
end