Skip to content
Snippets Groups Projects
post.rb 2.05 KiB
Newer Older
Arnaud Levy's avatar
Arnaud Levy committed
# == Schema Information
#
# Table name: communication_website_posts
#
#  id                       :uuid             not null, primary key
#  description              :text
#  old_text                 :text
Arnaud Levy's avatar
Arnaud Levy committed
#  published                :boolean          default(FALSE)
#  published_at             :datetime
Arnaud Levy's avatar
Arnaud Levy committed
#  slug                     :text
Arnaud Levy's avatar
Arnaud Levy committed
#  title                    :string
#  created_at               :datetime         not null
#  updated_at               :datetime         not null
#  communication_website_id :uuid             not null
#  university_id            :uuid             not null
#
# Indexes
#
#  index_communication_website_posts_on_communication_website_id  (communication_website_id)
#  index_communication_website_posts_on_university_id             (university_id)
#
# Foreign Keys
#
#  fk_rails_...  (communication_website_id => communication_websites.id)
#  fk_rails_...  (university_id => universities.id)
#
class Communication::Website::Post < ApplicationRecord
pabois's avatar
pabois committed
  include WithSlug
Arnaud Levy's avatar
Arnaud Levy committed
  include Communication::Website::WithGithub
pabois's avatar
pabois committed

  has_rich_text :text

Arnaud Levy's avatar
Arnaud Levy committed
  belongs_to :university
  belongs_to :website,
             foreign_key: :communication_website_id
  has_one    :imported_post,
             class_name: 'Communication::Website::Imported::Post',
Arnaud Levy's avatar
Arnaud Levy committed
             foreign_key: :post_id,
pabois's avatar
pabois committed
             dependent: :destroy
Arnaud Levy's avatar
Arnaud Levy committed

  scope :ordered, -> { order(published_at: :desc, created_at: :desc) }
  scope :recent, -> { order(published_at: :desc).limit(5) }

  validates :title, presence: true

  def to_s
    "#{title}"
  end
Arnaud Levy's avatar
Arnaud Levy committed

  protected

  def github_file
    "#{published_at.year}/#{published_at.month}/#{published_at.strftime "%Y-%m-%d"}-#{id}.html"
  end

  def github_path
    "_posts/#{github_file}"
  end

  def publish_to_github
    github.publish  kind: :posts,
                    file: github_file,
                    title: to_s,
                    data: ApplicationController.render(
                      template: 'admin/communication/website/posts/jekyll',
                      layout: false,
                      assigns: { post: self }
                    )
  end
Arnaud Levy's avatar
Arnaud Levy committed
end