Project

General

Profile

Defect #12359 » version_validate_effective_date.diff

Proposed patch. - Róbert Ágoston, 2012-11-14 17:23

View differences:

test/unit/version_test.rb (working copy)
def test_invalid_effective_date_validation
v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '99999-01-01')
assert !v.save
assert !v.valid?
v.effective_date = '2012-11-33'
assert !v.valid?
v.effective_date = '2012-31-11'
assert !v.valid?
v.effective_date = 'ABC'
assert !v.valid?
assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
v.errors[:effective_date]
end
app/models/version.rb (working copy)
validates_presence_of :name
validates_uniqueness_of :name, :scope => [:project_id]
validates_length_of :name, :maximum => 60
validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true
validate :validate_effective_date
validates_inclusion_of :status, :in => VERSION_STATUSES
validates_inclusion_of :sharing, :in => VERSION_SHARINGS
......
progress
end
end
def validate_effective_date
unless effective_date_before_type_cast.empty? ||
((effective_date_before_type_cast =~ /^\d{4}-\d{2}-\d{2}$/) &&
(begin; effective_date_before_type_cast.to_date; rescue; false end))
errors.add(:effective_date, :not_a_date)
end
end
end
    (1-1/1)