Sha256: f047c3247bd2252b8ca3dcf6934efd065a1c9414c998e225e8479e2caf7aa42b

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require 'nokogiri'

class Post < ActiveRecord::Base
  has_one :header, class_name: Image

  store_accessor :properties, :labels

  scope :status, lambda {|status|
    if status.eql?("published")
      where "posts.published_at IS NOT NULL"
    else
      where "posts.published_at IS NULL"
    end
  }

  scope :published, lambda { status("published") }
  scope :drafted, lambda { status("drafted") }
  scope :slug, lambda { |slug| where("posts.slug is ?", slug) }
  scope :without, lambda { |post| where("posts.id != ?", post.id) }

  validates :title, presence: true, uniqueness: true
  validates :slug, presence: true, uniqueness: true

  before_validation :create_slug_if_nil

  def status=(new_status)
    if new_status.eql? "publish"
      publish!
    end
  end

  def publish!
    return unless published_at.nil?
    self.published_at = DateTime.now
    save!
  end

  def published?
    !draft?
  end

  def draft?
    published_at.nil?
  end

  def to_param
    if self.instance_of?(Admin::Post)
      id.to_s
    else
      slug
    end
  end

  def content
    (self.compiled_content || super || '').html_safe
  end

  def excerpt
    (self.compiled_excerpt || "").html_safe
  end


  def title=(new_title)
    super
    if self.draft?
      self.slug = nil
    end
  end

  def header?
    !self.header.nil? && !self.header.url.blank?
  end

  def labels
    ids = super || ''
    Label.where(id: ids.split(',')).to_a
  end

  def labels=(labels)
    super(labels.map(&:id).join(','))
  end

  protected

  def create_slug_if_nil
    return unless self.slug.blank?
    self.slug = self.title.parameterize
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ecrire-0.22.1 lib/ecrire/app/models/post.rb
ecrire-0.21.0 lib/ecrire/app/models/post.rb
ecrire-0.20.0 lib/ecrire/app/models/post.rb