Module: ActiveRecord::Validations::ClassMethods

Defined in:
lib/validates_as_date_time.rb

Instance Method Summary collapse

Instance Method Details

#validates_as_date(*attr_names) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/validates_as_date_time.rb', line 56

def validates_as_date(*attr_names)
  regExpStr = '(([0-1]?[0-9])\/[0-3]?[0-9]\/[0-9]{4})'
  configuration = {
    :message    => 'is an invalid date',
    :with       => /^#{regExpStr}$/,
    :allow_nil  => false,
    :year_range => nil,
    :on         => :save }   
     
  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)

  validates_each(attr_names,configuration) do |record, attr_name,value|
    if !['Date', 'String'].include?(record.send(attr_name).class.to_s)
      record.errors.add(attr_name, configuration[:message])
    elsif record.send(attr_name).blank? and !configuration[:allow_nil]
      record.errors.add(attr_name, configuration[:message])
    elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
      record.errors.add(attr_name, configuration[:message]) 
    else
      begin
        d = Date.parse(value.to_s) # If date is not valid then it will throw an exception
        record.errors.add(attr_name, configuration[:message]) if !configuration[:year_range].nil? and 
                                                                 !configuration[:year_range].include?(d.year)
      rescue
        record.errors.add(attr_name, configuration[:message])
      end
    end        
  end   
end

#validates_as_date_time(*attr_names) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/validates_as_date_time.rb', line 87

def validates_as_date_time(*attr_names)
  regExpStr = '(([0-1]?[0-9])\/[0-3]?[0-9]\/[0-9]{4}) *([0-9]{1,2}(:[0-9]{2})? *(am|pm))?'
  configuration = {
    :message    => 'is an invalid datetime',
    :with       => /^#{regExpStr}$/,
    :allow_nil  => false,
    :year_range => nil,
    :on         => :save }
    
  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)

  validates_each(attr_names,configuration) do |record, attr_name,value|
    if !['DateTime', 'String'].include?(record.send(attr_name).class.to_s)
      record.errors.add(attr_name, configuration[:message])
    elsif record.send(attr_name).blank? and !configuration[:allow_nil]
      record.errors.add(attr_name, configuration[:message])
    elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
      record.errors.add(attr_name, configuration[:message]) 
    else
      begin
        d = DateTime.parse(value.to_s) # If date time is not valid then it will throw an exception
        record.errors.add(attr_name, configuration[:message]) if !configuration[:year_range].nil? and 
                                                                 !configuration[:year_range].include?(d.year)
      rescue
        record.errors.add(attr_name, configuration[:message])
      end
    end            
  end    
end

#validates_as_time(*attr_names) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/validates_as_date_time.rb', line 28

def validates_as_time(*attr_names)
  regExpStr = '[0-9]{1,2}(:[0-9]{2})? *(am|pm)?'
  configuration = {
    :message   => 'is an invalid time',
    :with      => /^#{regExpStr}$/,
    :allow_nil => false,
    :on        => :save }
    
  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)

  validates_each(attr_names,configuration) do |record, attr_name,value|
    if !['Time', 'String'].include?(record.send(attr_name).class.to_s)
      record.errors.add(attr_name, configuration[:message])
    elsif record.send(attr_name).blank? and !configuration[:allow_nil]
      record.errors.add(attr_name, configuration[:message])
    elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
      record.errors.add(attr_name, configuration[:message]) 
    else
      begin
        Time.parse(value.to_s) # If time is not valid then it will throw an exception
      rescue
        record.errors.add(attr_name, configuration[:message])
      end
    end          
  end
end