Sha256: 558cad452a9b1fba192c439924b71a8a9901e50f1a661583d7dcad0c84b28b85

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'noodall/articles/categories'

class Article < Noodall::Node
  extend Noodall::Articles::Categories

  delegate :list, :articles, :all_categories, :recent_articles, :archive, :to => :parent

  key :categories, Array, :index => true

  key :asset_id, ObjectId
  belongs_to :asset

  def category_list=(string)
    self.categories = string.to_s.split(',').map{ |t| t.strip.titlecase }.reject(&:blank?).compact.uniq
  end

  def category_list
    categories.join(',')
  end

  def related_articles
    @related_articles ||= begin
      siblings
        .published
        .order('published_date DESC')
        .where(:tags => /(#{tags.join('|')})/i)
        .limit(3)
    end
  end

  protected

  # A slug for creating the permalink with date
  def slug
    (published_at || current_time).strftime('%Y/%m/%d/') + super
  end

  private

  # Always put a new article at the top of a list
  def set_position
    write_attribute :position, 0 if self.position.blank?
  end

  # Parses body to find first image asset
  def set_asset

    # get all image assets
    asset_ids = body.to_s.scan(%r|<img.*id="asset-([^"]*)".*/>|i)
    if asset_ids.empty?
      self.asset = nil
    else
      # remove prefix and file extension
      self.asset = Asset.find_by_id(asset_ids.first.first.to_s)
    end
  end
  before_save :set_asset
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
noodall-articles-1.0.0 app/models/article.rb