class Controladoria::CentroDeCustoDaRequisicao < ApplicationRecord
  has_paper_trail

  attr_accessor :marcado_para_cadastro

  belongs_to :centro_de_custo, class_name: 'Controladoria::CentroDeCusto'
  belongs_to :requisicao_de_material, class_name: "Administrativo::RequisicaoDeMaterial"

  validates_presence_of :centro_de_custo_id

  after_save :deleta_caso_nao_esteja_marcado_para_cadastro
  after_initialize :marca_como_marcado_para_cadastro

  def deleta_caso_nao_esteja_marcado_para_cadastro
    if self.marcado_para_cadastro == "0"
      self.destroy
    end
  end

  def marca_como_marcado_para_cadastro
    self.marcado_para_cadastro = "1" if self.persisted?
  end

end
