validates_as_date_time 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/LICENSE +5 -0
- data/README +33 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/validates_as_date_time.rb +120 -0
- data/test/validates_as_date_test.rb +77 -0
- data/test/validates_as_date_time_test.rb +81 -0
- data/test/validates_as_time_test.rb +64 -0
- metadata +66 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
data/README
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
ValidatesAsDateTime
|
2
|
+
===================
|
3
|
+
This Ruby on Rails gem/plugin implements an ActiveRecord validation helper called
|
4
|
+
validates_as_date_time which validates U.S. dates, times, or date-times.
|
5
|
+
|
6
|
+
Installation:
|
7
|
+
=============
|
8
|
+
gem sources -a http://gems.github.com
|
9
|
+
|
10
|
+
Install the gem(s):
|
11
|
+
sudo gem install gbdev-validates_as_date_time
|
12
|
+
|
13
|
+
Add to environment.rb initializer block:
|
14
|
+
config.gem 'gbdev-validates_as_date_time', :version => '>=0.5.0', :lib => 'validates_as_date_time', :source => 'http://gems.github.com'
|
15
|
+
|
16
|
+
Usage:
|
17
|
+
======
|
18
|
+
In your model file do something like:
|
19
|
+
|
20
|
+
class MyClass < ActiveRecord::Base
|
21
|
+
validates_as_date_time :my_birth_date, :year_range => 1980..2005, :allow_nil => false
|
22
|
+
validates_as_date :her_birth_date, :allow_nil => false, :on => :create
|
23
|
+
validates_as_time :his_birth_date, :allow_nil => false, :on => :update
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
Tests:
|
28
|
+
======
|
29
|
+
Some tests have been added.
|
30
|
+
|
31
|
+
License:
|
32
|
+
========
|
33
|
+
See the LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the validates_as_date_time plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the validates_as_date_time plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ValidatesAsDateTime'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_as_date_time'
|
@@ -0,0 +1,120 @@
|
|
1
|
+
#
|
2
|
+
# Made this a plugin because there was not one out there yet. (yes I did an exhaustive search)
|
3
|
+
#
|
4
|
+
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
|
5
|
+
# http://creativecommons.org/licenses/by-sa/2.5/
|
6
|
+
#
|
7
|
+
|
8
|
+
# Validation helper for ActiveRecord derived objects that cleanly and simply
|
9
|
+
# allows the model to check if the given string is a valid US datetime
|
10
|
+
# 11/11/1111 12pm
|
11
|
+
# 11/11/1111 1pm
|
12
|
+
# 1/11/1111 10:10pm
|
13
|
+
# 11/1/1111 01pm
|
14
|
+
# 1/1/1111 01:11pm
|
15
|
+
# 1/1/1111 1:11pm
|
16
|
+
#
|
17
|
+
# 11/11/1111
|
18
|
+
# 11/11/1111
|
19
|
+
# 1/11/1111
|
20
|
+
# 11/1/1111
|
21
|
+
# 1/1/1111
|
22
|
+
# 1/1/1111
|
23
|
+
|
24
|
+
module ActiveRecord
|
25
|
+
module Validations
|
26
|
+
module ClassMethods
|
27
|
+
|
28
|
+
def validates_as_time(*attr_names)
|
29
|
+
regExpStr = '[0-9]{1,2}(:[0-9]{2})? *(am|pm)?'
|
30
|
+
configuration = {
|
31
|
+
:message => 'is an invalid time',
|
32
|
+
:with => /^#{regExpStr}$/,
|
33
|
+
:allow_nil => false,
|
34
|
+
:on => :save }
|
35
|
+
|
36
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
37
|
+
|
38
|
+
validates_each(attr_names,configuration) do |record, attr_name,value|
|
39
|
+
if !['Time', 'String'].include?(record.send(attr_name).class.to_s)
|
40
|
+
record.errors.add(attr_name, configuration[:message])
|
41
|
+
elsif record.send(attr_name).blank? and !configuration[:allow_nil]
|
42
|
+
record.errors.add(attr_name, configuration[:message])
|
43
|
+
elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
|
44
|
+
record.errors.add(attr_name, configuration[:message])
|
45
|
+
else
|
46
|
+
begin
|
47
|
+
Time.parse(value.to_s) # If time is not valid then it will throw an exception
|
48
|
+
rescue
|
49
|
+
record.errors.add(attr_name, configuration[:message])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
def validates_as_date(*attr_names)
|
57
|
+
regExpStr = '(([0-1]?[0-9])\/[0-3]?[0-9]\/[0-9]{4})'
|
58
|
+
configuration = {
|
59
|
+
:message => 'is an invalid date',
|
60
|
+
:with => /^#{regExpStr}$/,
|
61
|
+
:allow_nil => false,
|
62
|
+
:year_range => nil,
|
63
|
+
:on => :save }
|
64
|
+
|
65
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
66
|
+
|
67
|
+
validates_each(attr_names,configuration) do |record, attr_name,value|
|
68
|
+
if !['Date', 'String'].include?(record.send(attr_name).class.to_s)
|
69
|
+
record.errors.add(attr_name, configuration[:message])
|
70
|
+
elsif record.send(attr_name).blank? and !configuration[:allow_nil]
|
71
|
+
record.errors.add(attr_name, configuration[:message])
|
72
|
+
elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
|
73
|
+
record.errors.add(attr_name, configuration[:message])
|
74
|
+
else
|
75
|
+
begin
|
76
|
+
d = Date.parse(value.to_s) # If date is not valid then it will throw an exception
|
77
|
+
record.errors.add(attr_name, configuration[:message]) if !configuration[:year_range].nil? and
|
78
|
+
!configuration[:year_range].include?(d.year)
|
79
|
+
rescue
|
80
|
+
record.errors.add(attr_name, configuration[:message])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
def validates_as_date_time(*attr_names)
|
88
|
+
regExpStr = '(([0-1]?[0-9])\/[0-3]?[0-9]\/[0-9]{4}) *([0-9]{1,2}(:[0-9]{2})? *(am|pm))?'
|
89
|
+
configuration = {
|
90
|
+
:message => 'is an invalid datetime',
|
91
|
+
:with => /^#{regExpStr}$/,
|
92
|
+
:allow_nil => false,
|
93
|
+
:year_range => nil,
|
94
|
+
:on => :save }
|
95
|
+
|
96
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
97
|
+
|
98
|
+
validates_each(attr_names,configuration) do |record, attr_name,value|
|
99
|
+
if !['DateTime', 'String'].include?(record.send(attr_name).class.to_s)
|
100
|
+
record.errors.add(attr_name, configuration[:message])
|
101
|
+
elsif record.send(attr_name).blank? and !configuration[:allow_nil]
|
102
|
+
record.errors.add(attr_name, configuration[:message])
|
103
|
+
elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
|
104
|
+
record.errors.add(attr_name, configuration[:message])
|
105
|
+
else
|
106
|
+
begin
|
107
|
+
d = DateTime.parse(value.to_s) # If date time is not valid then it will throw an exception
|
108
|
+
record.errors.add(attr_name, configuration[:message]) if !configuration[:year_range].nil? and
|
109
|
+
!configuration[:year_range].include?(d.year)
|
110
|
+
rescue
|
111
|
+
record.errors.add(attr_name, configuration[:message])
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'validates_as_date_time'
|
7
|
+
rescue LoadError
|
8
|
+
require 'rubygems'
|
9
|
+
require 'activerecord'
|
10
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_date_time'
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestRecordVd < ActiveRecord::Base
|
14
|
+
def self.columns; []; end
|
15
|
+
attr_accessor :my_birth_date
|
16
|
+
validates_as_date :my_birth_date, :year_range => 1980..2005, :allow_nil => false # false is default
|
17
|
+
end
|
18
|
+
|
19
|
+
class TestRecordVd2 < ActiveRecord::Base
|
20
|
+
def self.columns; []; end
|
21
|
+
attr_accessor :my_birth_date
|
22
|
+
validates_as_date :my_birth_date, :year_range => 1980..2005, :allow_nil => true
|
23
|
+
end
|
24
|
+
|
25
|
+
class ValidatesAsDateTimeTest < Test::Unit::TestCase
|
26
|
+
|
27
|
+
def test_valid_date
|
28
|
+
dates = [ '11/11/1990',
|
29
|
+
'11/11/1990',
|
30
|
+
'1/11/1990',
|
31
|
+
'11/1/1990',
|
32
|
+
'1/1/1990',
|
33
|
+
'1/1/1990']
|
34
|
+
|
35
|
+
dates.each do |d|
|
36
|
+
tr = TestRecordVd.new(:my_birth_date => d)
|
37
|
+
assert tr.valid?, "#{d} should be legal."
|
38
|
+
assert_equal tr.errors.count, 0
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_invalid_date
|
43
|
+
dates = [ '11/11/11 12pm',
|
44
|
+
'11/11/11 1pm',
|
45
|
+
'1/11/11 10:10pm',
|
46
|
+
'11/1/11 01pm',
|
47
|
+
'1/1/11 01:11pm',
|
48
|
+
'1/1/11 1:11pm',
|
49
|
+
'1/1/1',
|
50
|
+
'2/29/2003 1:11pm',
|
51
|
+
'3/15/1970',
|
52
|
+
'',
|
53
|
+
nil,
|
54
|
+
DateTime.parse('1/1/2001'),
|
55
|
+
Date.parse('1/1/1')]
|
56
|
+
|
57
|
+
dates.each do |d|
|
58
|
+
tr = TestRecordVd.new(:my_birth_date => d)
|
59
|
+
assert !tr.valid?, "#{d} should be illegal."
|
60
|
+
assert_equal tr.errors.count, 1
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_blank_date_times_not_allowed
|
65
|
+
tr = TestRecordVd.new(:my_birth_date => nil)
|
66
|
+
assert !tr.valid?, "Blank date should be illegal."
|
67
|
+
assert_equal tr.errors.count, 1
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_blank_date_times_allowed
|
71
|
+
tr = TestRecordVd2.new(:my_birth_date => nil)
|
72
|
+
assert tr.valid?, "Blank date should be legal."
|
73
|
+
assert_equal tr.errors.count, 0
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'validates_as_date_time'
|
7
|
+
rescue LoadError
|
8
|
+
require 'rubygems'
|
9
|
+
require 'activerecord'
|
10
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_date_time'
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestRecordVdt < ActiveRecord::Base
|
14
|
+
def self.columns; []; end
|
15
|
+
attr_accessor :my_birth_date
|
16
|
+
validates_as_date_time :my_birth_date, :year_range => 1980..2005, :allow_nil => false # false is default
|
17
|
+
end
|
18
|
+
|
19
|
+
class TestRecordVdt2 < ActiveRecord::Base
|
20
|
+
def self.columns; []; end
|
21
|
+
attr_accessor :my_birth_date
|
22
|
+
validates_as_date_time :my_birth_date, :year_range => 1980..2005, :allow_nil => true
|
23
|
+
end
|
24
|
+
|
25
|
+
class ValidatesAsDateTimeTest < Test::Unit::TestCase
|
26
|
+
|
27
|
+
def test_valid_date_times
|
28
|
+
date_times = [ '11/11/1990 12pm',
|
29
|
+
'11/11/1990 1pm',
|
30
|
+
'1/11/1990 10:10pm',
|
31
|
+
'11/1/1990 01pm',
|
32
|
+
'1/1/1990 01:11pm',
|
33
|
+
'1/1/1990 1:11pm',
|
34
|
+
'11/11/1990',
|
35
|
+
'11/11/1990',
|
36
|
+
'1/11/1990',
|
37
|
+
'11/1/1990',
|
38
|
+
'1/1/1990',
|
39
|
+
'1/1/1990',
|
40
|
+
DateTime.parse('1/1/2001')]
|
41
|
+
date_times.each do |dt|
|
42
|
+
assert TestRecordVdt.new(:my_birth_date => dt).valid?, "#{dt} should be legal."
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_invalid_date_times
|
47
|
+
date_times = [ '11/11/11 12pm',
|
48
|
+
'11/11/11 1pm',
|
49
|
+
'1/11/11 10:10pm',
|
50
|
+
'11/1/11 01pm',
|
51
|
+
'1/1/11 01:11pm',
|
52
|
+
'1/1/11 1:11pm',
|
53
|
+
'11/11/11',
|
54
|
+
'11/11/11',
|
55
|
+
'1/11/11',
|
56
|
+
'11/1/11',
|
57
|
+
'1/1/11',
|
58
|
+
'1/1/11',
|
59
|
+
'2/30/2003 1:11pm',
|
60
|
+
'2/29/2003',
|
61
|
+
'3/15/1970',
|
62
|
+
'',
|
63
|
+
nil,
|
64
|
+
Date.parse('2/2/2002'),
|
65
|
+
DateTime.parse('1/1/1') ]
|
66
|
+
|
67
|
+
date_times.each do |dt|
|
68
|
+
assert !TestRecordVdt.new(:my_birth_date => dt).valid?, "#{dt} should be illegal."
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_blank_date_times_not_allowed
|
73
|
+
assert !TestRecordVdt.new(:my_birth_date => nil).valid?, "Blank datetime should be illegal."
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_blank_date_times_allowed
|
77
|
+
assert TestRecordVdt2.new(:my_birth_date => nil).valid?, "Blank datetime should be legal."
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'validates_as_date_time'
|
7
|
+
rescue LoadError
|
8
|
+
require 'rubygems'
|
9
|
+
require 'activerecord'
|
10
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_date_time'
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestRecordVt < ActiveRecord::Base
|
14
|
+
def self.columns; []; end
|
15
|
+
attr_accessor :my_birth_time
|
16
|
+
validates_as_time :my_birth_time, :allow_nil => false # false is default
|
17
|
+
end
|
18
|
+
|
19
|
+
class TestRecordVt2 < ActiveRecord::Base
|
20
|
+
def self.columns; []; end
|
21
|
+
attr_accessor :my_birth_time
|
22
|
+
validates_as_time :my_birth_time, :allow_nil => true
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
class ValidatesAsDateTimeTest < Test::Unit::TestCase
|
27
|
+
|
28
|
+
def test_valid_time
|
29
|
+
times = [ '12pm',
|
30
|
+
'1:00',
|
31
|
+
'14:00',
|
32
|
+
'10:24am',
|
33
|
+
'10:25 pm',
|
34
|
+
'09:12',
|
35
|
+
Time.parse('2/2/2002 11:00 pm')]
|
36
|
+
times.each do |t|
|
37
|
+
assert TestRecordVt.new(:my_birth_time => t).valid?, "#{t} should be legal."
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_invalid_time
|
42
|
+
times = [ 'asdf1:2::1asdf',
|
43
|
+
'1:002',
|
44
|
+
'14:80am',
|
45
|
+
'00:74pm',
|
46
|
+
'11:pm',
|
47
|
+
'asdf',
|
48
|
+
'',
|
49
|
+
nil]
|
50
|
+
times.each do |t|
|
51
|
+
assert !TestRecordVt.new(:my_birth_time => t).valid?, "#{t} should be illegal."
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_blank_times_not_allowed
|
56
|
+
assert !TestRecordVt.new(:my_birth_time => nil).valid?, "Blank date should be illegal."
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_blank_times_allowed
|
60
|
+
assert TestRecordVt2.new(:my_birth_time => nil).valid?, "Blank date should be legal."
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_as_date_time
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wes Hays
|
8
|
+
- John Dell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-09-08 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Rails gem/plugin that implements an ActiveRecord validation helper called validates_as_date_time which validates U.S. dates, times, and date-times
|
18
|
+
email: gems@gbdev.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- CHANGELOG
|
27
|
+
- LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- init.rb
|
31
|
+
- lib/validates_as_date_time.rb
|
32
|
+
- test/validates_as_date_test.rb
|
33
|
+
- test/validates_as_time_test.rb
|
34
|
+
- test/validates_as_date_time_test.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/gbdev/validates_as_date_time
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.5
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Rails gem/plugin to validate format of U.S. dates, times, and date-times
|
63
|
+
test_files:
|
64
|
+
- test/validates_as_date_test.rb
|
65
|
+
- test/validates_as_time_test.rb
|
66
|
+
- test/validates_as_date_time_test.rb
|