Created
February 15, 2013 18:04
-
-
Save rodrigoulisses/4962169 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PrecatoryParcel < Compras::Model | |
attr_accessible :expiration_date, :value, :situation, :payment_date | |
attr_accessible :amount_paid, :observation | |
has_enumeration_for :situation, :with => PrecatoryParcelSituation | |
belongs_to :precatory | |
delegate :pledges, :to => :precatory, :prefix => true, :allow_nil => true | |
validates :expiration_date, :value, :situation, :presence => true | |
before_destroy :check_destroy | |
def check_destroy | |
if precatory_pledges.any? | |
errors.add(:base, :cannot_be_changed_if_precatory_have_pledge) | |
end | |
end | |
end | |
# encoding: utf-8 | |
require 'model_helper' | |
require 'app/models/precatory_parcel' | |
require 'app/enumerations/precatory_parcel_situation' | |
describe PrecatoryParcel do | |
it { should validate_presence_of :expiration_date } | |
it { should validate_presence_of :value } | |
it { should validate_presence_of :situation } | |
it { should belong_to :precatory } | |
context "destroy precatory_parcel" do | |
before do | |
subject.expiration_date = Date.new(2013, 01, 31) | |
subject.value = 100 | |
subject.situation = PrecatoryParcelSituation::PAID | |
subject.stub(:precatory).and_return(double("Precatorio").stub(:pledges).and_return(double("Emprenho"), :any? => true)) | |
end | |
it "when precatory has pledge" do | |
subject.destroy | |
expect(subject.errors[:base]).to include 'não pode ser alterada. Este Precatório já possui Empenho' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
faz assim:
Caso inválido:
Caso válido:
Não esquece de retornar
false
depois de incluir o erro, senão ele vai excluir o registro do mesmo jeito.