Sha256: 23b00b080c1f0bd4b045cd9288f05e4eec232c1276991c89bbc67d19c6061c25

Contents?: true

Size: 907 Bytes

Versions: 2

Compression:

Stored size: 907 Bytes

Contents

module Chive
  class Article < ActiveRecord::Base
    self.per_page = 10
    belongs_to :author, class_name: 'User'
    
    after_initialize :set_default_autosummary, :if => :new_record?
    before_save :set_published_at, :set_slug, :generate_summary
    
    def to_param
      slug
    end
    
    def byline
      custom_byline || author.email
    end
    
    private
    def set_default_autosummary
      self.autosummary = Chive.default_autosummary || false
    end
    def set_published_at
      self.published_at = DateTime.now if self.published_at.nil?
    end
    def set_slug
      # TODO Slugs need to be unique
      self.slug = self.title.parameterize if self.slug.nil?
    end
    def generate_summary
      if self.autosummary?
        html = Nokogiri::HTML(self.body)
        para = html.css('p').first
        puts para
        self.summary = para.inner_html
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chive-0.0.2 app/models/chive/article.rb
chive-0.0.1 app/models/chive/article.rb