class Document < ActiveRecord::Base
  
  # Accessors
  attr_accessible :active, :description, :downloads, :kind, :year,
    :information_standard_id, :name, :institution_id, :document_category_id, :priority, :institution_dependence_id
  
  # Non database attributtes
  attr_accessible :datafile
  
  # Associations
  belongs_to :institution
  belongs_to :institution_dependence
  belongs_to :information_standard
  belongs_to :document_category, :touch => true
   
  # Paperclip
  has_attached_file :datafile

  # scope
  scope :active, where(active: true)
  scope :inactive, where(active: false)
  scope :without_standard, where(information_standard_id: nil)

  # Validations
  validates :institution_id, :information_standard_id, :name, presence: true
  validates_attachment_presence :datafile

  # delegates
  delegate :name, to: :information_standard, allow_nil: true, prefix: true

  # Paper trail, track changes
  has_paper_trail

  Kind = {
    0 => "Normal",
    1 => "Informe de Rendicion de Cuentas",
    2 => "Presentacion de Rendicion de Cuentas",
    3 => "Aportes"
  }

  # Will paginate uses this method
  # to know how many rows to show
  #
  def self.per_page
    50
  end

  # how many created documents on...
  #
  def self.created_on(date)
    where('DATE(created_at) = ?', date).count
  end


  # how many created documents on...
  #
  def self.updated_on(date)
    where('DATE(updated_at) = ?', date).count
  end

=begin
  def self.attributes_for_grid
    [
      :institution_id,
      :information_standard_id,
      :document_category_id,
      :active,
      :name,
      :description
    ]
  end
=end
end
