Sha256: f1da59d9728dee2b735caf47647f4fc32ae6b15b468708b8416b21f52149bcd2

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

require "awesome_nested_set"

module PandaCms
  class Post < ApplicationRecord
    self.table_name = "panda_cms_posts"

    has_paper_trail versions: {
      class_name: "PandaCms::PostVersion"
    }

    belongs_to :user, class_name: "PandaCms::User"

    validates :title, presence: true
    validates :slug, presence: true, uniqueness: true, format: {with: /\A[a-z0-9-]+\z/}

    scope :ordered, -> { order(published_at: :desc) }
    scope :with_user, -> { includes(:user) }

    has_rich_text :post_content

    belongs_to :tag, class_name: "PandaCms::PostTag", foreign_key: :post_tag_id

    enum :status, {
      active: "active",
      draft: "draft",
      hidden: "hidden",
      archived: "archived"
    }

    def to_param
      slug.to_s
    end

    def excerpt(length = 100, squish: true)
      excerpt = post_content.to_plain_text
      excerpt = excerpt.squish if squish
      excerpt.truncate(length).html_safe
    end

    def path
      "/" + PandaCms.posts[:prefix] + slug.to_s
    end

    def formatted_slug
      if params[:slug][0] != "/"
        "/#{params[:slug]}"
      else
        params[:slug]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
panda_cms-0.5.8 app/models/panda_cms/post.rb
panda_cms-0.5.7 app/models/panda_cms/post.rb
panda_cms-0.5.6 app/models/panda_cms/post.rb