##// END OF EJS Templates
Adds a custom validator for dates (#12736)....
Jean-Philippe Lang -
r10894:3e14c3017c03
parent child
Show More
@@ -68,6 +68,8 class Issue < ActiveRecord::Base
68 validates_length_of :subject, :maximum => 255
68 validates_length_of :subject, :maximum => 255
69 validates_inclusion_of :done_ratio, :in => 0..100
69 validates_inclusion_of :done_ratio, :in => 0..100
70 validates_numericality_of :estimated_hours, :allow_nil => true
70 validates_numericality_of :estimated_hours, :allow_nil => true
71 validates :start_date, :date => true
72 validates :due_date, :date => true
71 validate :validate_issue, :validate_required_fields
73 validate :validate_issue, :validate_required_fields
72
74
73 scope :visible, lambda {|*args|
75 scope :visible, lambda {|*args|
@@ -532,14 +534,6 class Issue < ActiveRecord::Base
532 end
534 end
533
535
534 def validate_issue
536 def validate_issue
535 if due_date.nil? && @attributes['due_date'].present?
536 errors.add :due_date, :not_a_date
537 end
538
539 if start_date.nil? && @attributes['start_date'].present?
540 errors.add :start_date, :not_a_date
541 end
542
543 if due_date && start_date && due_date < start_date
537 if due_date && start_date && due_date < start_date
544 errors.add :due_date, :greater_than_start_date
538 errors.add :due_date, :greater_than_start_date
545 end
539 end
@@ -30,10 +30,9 class Version < ActiveRecord::Base
30 validates_presence_of :name
30 validates_presence_of :name
31 validates_uniqueness_of :name, :scope => [:project_id]
31 validates_uniqueness_of :name, :scope => [:project_id]
32 validates_length_of :name, :maximum => 60
32 validates_length_of :name, :maximum => 60
33 validates_format_of :effective_date, :with => /\A\d{4}-\d{2}-\d{2}\z/, :message => :not_a_date, :allow_nil => true
33 validates :effective_date, :date => 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
37
36
38 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
37 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
39 scope :open, lambda { where(:status => 'open') }
38 scope :open, lambda { where(:status => 'open') }
@@ -287,10 +286,4 class Version < ActiveRecord::Base
287 progress
286 progress
288 end
287 end
289 end
288 end
290
291 def validate_version
292 if effective_date.nil? && @attributes['effective_date'].present?
293 errors.add :effective_date, :not_a_date
294 end
295 end
296 end
289 end
@@ -38,3 +38,14 module ActiveRecord
38 end
38 end
39 end
39 end
40 end
40 end
41
42 class DateValidator < ActiveModel::EachValidator
43 def validate_each(record, attribute, value)
44 before_type_cast = record.attributes_before_type_cast[attribute.to_s]
45 if before_type_cast.is_a?(String) && before_type_cast.present?
46 unless before_type_cast =~ /\A\d{4}-\d{2}-\d{2}\z/ && value
47 record.errors.add attribute, :not_a_date
48 end
49 end
50 end
51 end
@@ -37,6 +37,8 class VersionTest < ActiveSupport::TestCase
37 assert !v.valid?
37 assert !v.valid?
38 v.effective_date = '2012-31-11'
38 v.effective_date = '2012-31-11'
39 assert !v.valid?
39 assert !v.valid?
40 v.effective_date = '-2012-31-11'
41 assert !v.valid?
40 v.effective_date = 'ABC'
42 v.effective_date = 'ABC'
41 assert !v.valid?
43 assert !v.valid?
42 assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
44 assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
General Comments 0
You need to be logged in to leave comments. Login now