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)
32 32

  
33 33
  def test_invalid_effective_date_validation
34 34
    v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '99999-01-01')
35
    assert !v.save
35
    assert !v.valid?
36
    v.effective_date = '2012-11-33'
37
    assert !v.valid?
38
    v.effective_date = '2012-31-11'
39
    assert !v.valid?
40
    v.effective_date = 'ABC'
41
    assert !v.valid?
36 42
    assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
37 43
                   v.errors[:effective_date]
38 44
  end
app/models/version.rb (working copy)
30 30
  validates_presence_of :name
31 31
  validates_uniqueness_of :name, :scope => [:project_id]
32 32
  validates_length_of :name, :maximum => 60
33
  validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true
33
  validate :validate_effective_date
34 34
  validates_inclusion_of :status, :in => VERSION_STATUSES
35 35
  validates_inclusion_of :sharing, :in => VERSION_SHARINGS
36 36

  
......
275 275
      progress
276 276
    end
277 277
  end
278

  
279
  def validate_effective_date
280
    unless effective_date_before_type_cast.empty? ||
281
      ((effective_date_before_type_cast =~ /^\d{4}-\d{2}-\d{2}$/) &&
282
        (begin; effective_date_before_type_cast.to_date; rescue; false end))
283
      errors.add(:effective_date, :not_a_date)
284
    end
285
  end
278 286
end
    (1-1/1)