Sha256: 48c391e20b61e8fecd00fb5e405bca7a79dbe158a0b14f2f8b7b5414fb1acd1f

Contents?: true

Size: 1016 Bytes

Versions: 4

Compression:

Stored size: 1016 Bytes

Contents

require 'kramdown'

module Admin
  class Post < ::Post

    has_one :header, class_name: Admin::Image
    has_many :titles, -> { order "titles.created_at DESC" }, class_name: Admin::Title

    before_save :compile!, prepend: true
    before_save :excerptize!

    def publish!(params = {})
      self.assign_attributes(params)
      self.published_at = DateTime.now
      self.save!
    end

    def unpublish!(params = {})
      self.assign_attributes(params)
      self.published_at = nil
      self.save!
    end

    def stylesheet
      super || ""
    end

    def javascript
      super || ""
    end

    def content
      read_attribute(:content) || ""
    end

    private

    def compile!
      self.compiled_content = Kramdown::Document.new(self.content).to_html
    end

    def excerptize!
      html = Nokogiri::HTML(self.compiled_content)
      html.xpath("//img").each do |img|
        img.remove
      end
      self.compiled_excerpt = html.xpath('//body').children[0..20].to_s
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ecrire-0.25.0 lib/ecrire/app/models/admin/post.rb
ecrire-0.24.1 lib/ecrire/app/models/admin/post.rb
ecrire-0.24.0 lib/ecrire/app/models/admin/post.rb
ecrire-0.23.0 lib/ecrire/app/models/admin/post.rb