Module: ValidatesDateTime::ClassMethods

Defined in:
lib/validates_date_time.rb

Instance Method Summary collapse

Instance Method Details

#validates_date(*args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/validates_date_time.rb', line 54

def validates_date(*args)
  options = temporal_validation_options({ :message => "is an invalid date" }, args)
  attr_names = args
  
  # We must remove this from the configuration that is passed to validates_each because
  # we want to have our own definition of nil that uses the before_type_cast value
  allow_nil = options.delete(:allow_nil)
  
  prepare_restrictions(options, :date)
  
  validates_each(attr_names, options) do |record, attr_name, value|
    raw_value = record.send("#{attr_name}_before_type_cast")
    
    # If value that is unable to be parsed, and a blank value where allow_nil is not set are both invalid
    if (!raw_value.blank? and !value) || (raw_value.blank? and !allow_nil)
      record.errors.add(attr_name, options[:message])
    elsif value
      validate_before_and_after_restrictions(record, attr_name, value, options)
    end
  end
end

#validates_date_time(*args) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/validates_date_time.rb', line 94

def validates_date_time(*args)
  options = temporal_validation_options({ :message => "is an invalid date time" }, args)
  attr_names = args
  
  allow_nil = options.delete(:allow_nil)
  prepare_restrictions(options, :time)
  
  validates_each(attr_names, options) do |record, attr_name, value|
    raw_value = record.send("#{attr_name}_before_type_cast")
    
    if (!raw_value.blank? and !value) || (raw_value.blank? and !allow_nil)
      record.errors.add(attr_name, options[:message])
    elsif value
      validate_before_and_after_restrictions(record, attr_name, value, options)
    end
  end
end

#validates_time(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/validates_date_time.rb', line 76

def validates_time(*args)
  options = temporal_validation_options({ :message => "is an invalid time" }, args)
  attr_names = args
  
  allow_nil = options.delete(:allow_nil)
  prepare_restrictions(options, :dummy_time)
  
  validates_each(attr_names, options) do |record, attr_name, value|
    raw_value = record.send("#{attr_name}_before_type_cast")
    
    if (!raw_value.blank? and !value) || (raw_value.blank? and !allow_nil)
      record.errors.add(attr_name, options[:message])
    elsif value
      validate_before_and_after_restrictions(record, attr_name, value, options)
    end
  end
end