class Certificate < ActiveRecord::Base
  attr_accessible :description, :title, :images_attributes, :published
  has_many :images, :class_name => 'CertificateImage', :dependent => :destroy
  accepts_nested_attributes_for :images, :reject_if => lambda { |i| i[:uri].blank? && i[:uri_cache].blank? }, :allow_destroy => true

  validates_presence_of :description
  validates_length_of :description, :maximum => 1024

  validates_presence_of :title
  validates_length_of :title, :maximum => 255

  default_scope order('title ASC')
  scope :visible_to_user, lambda { where(:published => true) }

  define_index do
    indexes title, :sortable => true
    #indexes description
    has published
    #TODO set_property delta: true # 'delta:boolean' column should present
    #set_property field_weights: {
    #    title: 3,
    #    description: 1,
    #}
  end

  sphinx_scope(:sphinx_default_scope) {
    {order: 'title ASC', star: true}
  }

  default_sphinx_scope :sphinx_default_scope

end
