Sha256: d84d3769ad8b8ec84a5fb7ddfaaa97ccd5b10bdb315282a75ab37f192b57343f

Contents?: true

Size: 1.69 KB

Versions: 6

Compression:

Stored size: 1.69 KB

Contents

require 'nokogiri'

class Post < ActiveRecord::Base
  has_one :header, class_name: Image
  has_many :titles, -> { order "titles.created_at DESC" }

  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 :without, lambda { |post| where("posts.id != ?", post.id) }

  validates :titles, length: {minimum: 1}

  before_save :update_tags

  attr_writer :tags

  def title=(new_title)
    if self.published?
      self.titles.new(name: new_title)
    else
      title = self.titles.first || self.titles.new
      title.post = self
      title.name = new_title
    end
  end

  def title
    (self.titles.first || self.titles.new).name
  end

  def year
    published_at.year
  end

  def month
    published_at.month
  end

  def slug
    self.titles.first.slug
  end

  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 content
    (self.compiled_content || super || '').html_safe
  end

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

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

  def tags
    @tags ||= Tag.where("tags.id in (?)", super || [])
  end

  protected

  def update_tags
    ids = tags.map(&:id)
    unless ids == read_attribute(:tags)
      write_attribute(:tags, ids)
    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ecrire-0.26.2 lib/ecrire/app/models/post.rb
ecrire-0.26.1 lib/ecrire/app/models/post.rb
ecrire-0.26.0 lib/ecrire/app/models/post.rb
ecrire-0.25.2 lib/ecrire/app/models/post.rb
ecrire-0.25.1 lib/ecrire/app/models/post.rb
ecrire-0.25.0 lib/ecrire/app/models/post.rb