@@ -33,6 +33,7 class Version < ActiveRecord::Base | |||||
33 | validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true |
|
33 | validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true | |
34 | validates_inclusion_of :status, :in => VERSION_STATUSES |
|
34 | validates_inclusion_of :status, :in => VERSION_STATUSES | |
35 | validates_inclusion_of :sharing, :in => VERSION_SHARINGS |
|
35 | validates_inclusion_of :sharing, :in => VERSION_SHARINGS | |
|
36 | validate :validate_version | |||
36 |
|
37 | |||
37 | scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} |
|
38 | scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} | |
38 | scope :open, :conditions => {:status => 'open'} |
|
39 | scope :open, :conditions => {:status => 'open'} | |
@@ -275,4 +276,10 class Version < ActiveRecord::Base | |||||
275 | progress |
|
276 | progress | |
276 | end |
|
277 | end | |
277 | end |
|
278 | end | |
|
279 | ||||
|
280 | def validate_version | |||
|
281 | if effective_date.nil? && @attributes['effective_date'].present? | |||
|
282 | errors.add :effective_date, :not_a_date | |||
|
283 | end | |||
|
284 | end | |||
278 | end |
|
285 | end |
@@ -32,7 +32,13 class VersionTest < ActiveSupport::TestCase | |||||
32 |
|
32 | |||
33 | def test_invalid_effective_date_validation |
|
33 | def test_invalid_effective_date_validation | |
34 | v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '99999-01-01') |
|
34 | v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '99999-01-01') | |
35 |
assert !v. |
|
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 | assert_include I18n.translate('activerecord.errors.messages.not_a_date'), |
|
42 | assert_include I18n.translate('activerecord.errors.messages.not_a_date'), | |
37 | v.errors[:effective_date] |
|
43 | v.errors[:effective_date] | |
38 | end |
|
44 | end |
General Comments 0
You need to be logged in to leave comments.
Login now