[#4177] Added validation for Contract to check that end date is greater than start date.

This commit is contained in:
Eric Davis
2010-06-17 12:34:27 -07:00
parent 8c7d865a60
commit 56e39ba3ca
2 changed files with 13 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ class Contract < ActiveRecord::Base
validates_presence_of :end_date
validates_presence_of :executed
validates_inclusion_of :discount_type, :in => %w($ %), :allow_blank => true, :allow_nil => true
validate :start_and_end_date_are_valid
# Accessors
attr_accessible :name
@@ -28,4 +29,9 @@ class Contract < ActiveRecord::Base
attr_accessible :po_number
attr_accessible :details
def start_and_end_date_are_valid
if start_date && end_date && end_date < start_date
errors.add :end_date, :greater_than_start_date
end
end
end

View File

@@ -16,8 +16,13 @@ class ContractTest < ActiveSupport::TestCase
should_allow_values_for :discount_type, "$", "%", nil, ''
should_not_allow_values_for :discount_type, ["amount", "percent", "bar"]
context "start_date" do
should "be before end_date"
context "end_date" do
should "be after start_date" do
@contract = Contract.new(:start_date => Date.today, :end_date => Date.yesterday)
assert @contract.invalid?
assert_equal "must be greater than start date", @contract.errors.on(:end_date)
end
end
should "QUESTION: name be unique"