Sha256: d1515c64114b873af15877dae42defad090d2c3252f8cecf5b2a9fa11e998b35

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 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/,
        message: "must start with a forward slash and contain only lowercase letters, numbers, and hyphens"
      }

    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
      formatted_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.config.posts[:prefix] + slug.to_s
    end

    def formatted_slug
      if slug[0] == "/"
        slug[1, slug.length].to_s
      else
        slug
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
panda_cms-0.6.3 app/models/panda_cms/post.rb
panda_cms-0.6.2 app/models/panda_cms/post.rb
panda_cms-0.6.1 app/models/panda_cms/post.rb
panda_cms-0.6.0 app/models/panda_cms/post.rb