class PortfolioItem
  include Mongoid::Document
  include ComparableByIndex

  embedded_in :portfolio, :inverse_of => :portfolio_items
  field :title
  field :slug, :type => String
  field :client, :type => String
  field :description
  field :date, :type => Date
  field :index, :type => Integer, :default => -1
  field :published, :type => Boolean, :default => true
  index :index
  embeds_many :images, :class_name => "Image"  do
    def sorted
      @target.sort!
    end
  end
  validates_presence_of :title
  before_create :generate_slug

  def generate_slug
    self.slug = "#{client} #{title}".parameterize
  end

  class << self
    def published
      where(:published => true)
    end
  end
end
