Sha256: 91399867c6eca8868548b28d7afe99e7c2e264f7b98f79b4556a239b266f2e50

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

module Homesteading
  class Post < ActiveRecord::Base

    default_scope { order("published_at desc") }

    validates :published_at, :year, :month, :day, :hour, :minute, :second, :slug,
              presence:true, unless: :new_record?

    before_create :set_published_at_attrs, :set_slug
    before_update :set_published_at_attrs, :set_slug

    self.abstract_class = true


    def path
      base = ["/" + post_type]
      base += [year.to_s.rjust(2, "0"), month.rjust(2, "0"), day.rjust(2, "0"), slug] if published_at
      base.compact.join("/")
    end

    def post_type
      @post_type ||= Setting.where(name: "Post Type").first.content.downcase.pluralize
    end

    def params
      { year: year, month: month, day: day, slug: slug }
    end

    def public?
      !self.private?
    end

    def name
      [title, subtitle].compact.join(" : ")
    end

    def published_at
      read_attribute(:published_at) || self.published_at = Time.zone.now
    end

    private

    def set_published_at_attrs
      self.year   = published_at.year
      self.month  = published_at.month.to_s.rjust(2, "0")
      self.day    = published_at.day.to_s.rjust(  2, "0")
      self.hour   = published_at.hour.to_s.rjust( 2, "0")
      self.minute = published_at.min.to_s.rjust(  2, "0")
      self.second = published_at.sec.to_s.rjust(  2, "0")
    end

    def set_slug
      blank       = ""
      separator   = "-"
      self.slug ||= name.present? ? name : content
      self.slug   = slug.downcase.
        gsub(/\(|\)|\[|\]\./, blank).
        gsub(/&amp;/,         blank).
        gsub(/\W+/,           separator).
        gsub(/_+/,            separator).
        gsub(/ +/,            separator).
        gsub(/-+/,            separator).
        gsub(/^-+/,           blank).
        gsub(/-+$/,           blank)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
homesteading_publisher-0.2.4 app/models/homesteading/post.rb
homesteading_publisher-0.2.3 app/models/homesteading/post.rb
homesteading_publisher-0.2.2 app/models/homesteading/post.rb
homesteading_publisher-0.2.1 app/models/homesteading/post.rb
homesteading_publisher-0.2.0 app/models/homesteading/post.rb