Sha256: 73908af90a6105dee0040c68a1f5f1dda27780e3156c0bea19fd70beb9c28fff

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

class Comfy::Blog::Post < ActiveRecord::Base

  self.table_name = "comfy_blog_posts"

  include Comfy::Cms::WithFragments
  include Comfy::Cms::WithCategories

  cms_has_revisions_for :fragments_attributes

  # -- Relationships -----------------------------------------------------------
  belongs_to :site,
    class_name: "Comfy::Cms::Site"

  # -- Validations -------------------------------------------------------------
  validates :title, :slug, :year, :month,
    presence: true
  validates :slug,
    uniqueness: { scope: %i[site_id year month] },
    format:     { with: %r{\A%*\w[a-z0-9_%-]*\z}i }

  # -- Scopes ------------------------------------------------------------------
  scope :published, -> { where(is_published: true) }
  scope :for_year,  ->(year) { where(year: year) }
  scope :for_month, ->(month) { where(month: month) }

  # -- Callbacks ---------------------------------------------------------------
  before_validation :set_slug,
                    :set_published_at,
                    :set_date

  # -- Instance Mathods --------------------------------------------------------
  def url(relative: false)
    public_blog_path = ComfyBlog.config.public_blog_path
    post_path = ["/", public_blog_path, year, month, slug].join("/").squeeze("/")
    [site.url(relative: relative), post_path].join
  end

protected

  def set_slug
    self.slug ||= title.to_s.parameterize
  end

  def set_date
    self.year   = published_at.year
    self.month  = published_at.month
  end

  def set_published_at
    self.published_at ||= Time.zone.now
  end

end

Version data entries

11 entries across 11 versions & 6 rubygems

Version Path
PixelForce_FyBlog-0.0.2 app/models/comfy/blog/post.rb
PixelForce_FyBlog-0.0.1 app/models/comfy/blog/post.rb
PixelForce_ComfyBlog-0.0.1 app/models/comfy/blog/post.rb
EtsComfyBlog-0.0.4 app/models/comfy/blog/post.rb
EtsComfyBlog-0.0.3 app/models/comfy/blog/post.rb
EtsComfyBlog-0.0.2 app/models/comfy/blog/post.rb
EtsComfyBlog-0.0.1 app/models/comfy/blog/post.rb
ets_comfy_blog-0.0.1 app/models/comfy/blog/post.rb
kcy_comfy_blog-2.0.7 app/models/comfy/blog/post.rb
comfy_blog-2.0.7 app/models/comfy/blog/post.rb
comfy_blog-2.0.6 app/models/comfy/blog/post.rb