date_validator 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.rvmrc +1 -1
- data/Gemfile +2 -11
- data/Gemfile.lock +13 -13
- data/Rakefile +35 -52
- data/Readme.md +32 -0
- data/date_validator.gemspec +24 -67
- data/lib/active_model/validations/date_validator.rb +77 -0
- data/lib/date_validator.rb +11 -59
- data/lib/date_validator/version.rb +4 -0
- data/spec/date_validator_spec.rb +109 -96
- data/spec/spec_helper.rb +4 -5
- metadata +45 -51
- data/LICENSE +0 -20
- data/README.rdoc +0 -34
- data/VERSION +0 -1
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm --create use ruby-1.9.2@date_validator
|
1
|
+
rvm --create use ruby-1.9.2@date_validator
|
data/Gemfile
CHANGED
@@ -1,12 +1,3 @@
|
|
1
|
-
|
2
|
-
source :gemcutter
|
1
|
+
source "http://rubygems.org"
|
3
2
|
|
4
|
-
|
5
|
-
gem "rspec", "~> 2.5.0"
|
6
|
-
gem "simplecov"
|
7
|
-
gem "jeweler"
|
8
|
-
gem "activesupport", "~> 3.0.0"
|
9
|
-
gem "tzinfo"
|
10
|
-
end
|
11
|
-
|
12
|
-
gem "activemodel", "~> 3.0.0"
|
3
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
date_validator (0.6.1)
|
5
|
+
activemodel (~> 3.0.0)
|
6
|
+
|
1
7
|
GEM
|
2
8
|
remote: http://rubygems.org/
|
3
9
|
specs:
|
@@ -6,16 +12,10 @@ GEM
|
|
6
12
|
builder (~> 2.1.2)
|
7
13
|
i18n (~> 0.4.1)
|
8
14
|
activesupport (3.0.0)
|
15
|
+
bluecloth (2.0.11)
|
9
16
|
builder (2.1.2)
|
10
17
|
diff-lcs (1.1.2)
|
11
|
-
|
12
|
-
git (1.2.5)
|
13
|
-
i18n (0.4.1)
|
14
|
-
jeweler (1.4.0)
|
15
|
-
gemcutter (>= 0.1.0)
|
16
|
-
git (>= 1.2.5)
|
17
|
-
rubyforge (>= 2.0.0)
|
18
|
-
json_pure (1.4.6)
|
18
|
+
i18n (0.4.2)
|
19
19
|
rspec (2.5.0)
|
20
20
|
rspec-core (~> 2.5.0)
|
21
21
|
rspec-expectations (~> 2.5.0)
|
@@ -24,21 +24,21 @@ GEM
|
|
24
24
|
rspec-expectations (2.5.0)
|
25
25
|
diff-lcs (~> 1.1.2)
|
26
26
|
rspec-mocks (2.5.0)
|
27
|
-
rubyforge (2.0.4)
|
28
|
-
json_pure (>= 1.1.7)
|
29
27
|
simplecov (0.3.6)
|
30
28
|
simplecov-html (>= 0.3.7)
|
31
29
|
simplecov-html (0.3.8)
|
32
30
|
tzinfo (0.3.23)
|
31
|
+
yard (0.6.4)
|
33
32
|
|
34
33
|
PLATFORMS
|
35
34
|
java
|
36
35
|
ruby
|
37
36
|
|
38
37
|
DEPENDENCIES
|
39
|
-
activemodel (~> 3.0.0)
|
40
38
|
activesupport (~> 3.0.0)
|
41
|
-
|
39
|
+
bluecloth
|
40
|
+
date_validator!
|
42
41
|
rspec (~> 2.5.0)
|
43
42
|
simplecov
|
44
|
-
tzinfo
|
43
|
+
tzinfo (~> 0.3.0)
|
44
|
+
yard
|
data/Rakefile
CHANGED
@@ -1,60 +1,43 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'rake'
|
4
|
-
|
5
|
-
begin
|
6
|
-
require 'jeweler'
|
7
|
-
Jeweler::Tasks.new do |gem|
|
8
|
-
gem.name = "date_validator"
|
9
|
-
gem.summary = "A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel."
|
10
|
-
gem.description = "A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel. Currently supporting :after, :before, :after_or_equal_to and :before_or_equal_to options."
|
11
|
-
gem.email = "info@codegram.com"
|
12
|
-
gem.homepage = "http://github.com/codegram/date_validator"
|
13
|
-
gem.authors = ["Oriol Gual", "Josep M. Bach", "Josep Jaume Rey"]
|
14
|
-
|
15
|
-
gem.add_dependency 'activemodel', '>= 3.0.0'
|
16
|
-
|
17
|
-
gem.add_development_dependency "rspec", '>= 2.0.0.beta.20'
|
18
|
-
gem.add_development_dependency "activesupport", '>= 3.0.0'
|
19
|
-
gem.add_development_dependency "bundler", '>= 1.0.0'
|
20
|
-
gem.add_development_dependency "tzinfo"
|
21
|
-
end
|
22
|
-
Jeweler::GemcutterTasks.new
|
23
|
-
rescue LoadError
|
24
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
# Rake RSpec2 task stuff
|
29
|
-
gem 'rspec', '>= 2.0.0.beta.20'
|
30
|
-
gem 'rspec-expectations'
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
31
3
|
|
32
4
|
require 'rspec/core/rake_task'
|
5
|
+
desc "Run superrtext specs"
|
6
|
+
RSpec::Core::RakeTask.new
|
33
7
|
|
34
|
-
|
35
|
-
|
36
|
-
|
8
|
+
require 'yard'
|
9
|
+
YARD::Rake::YardocTask.new(:docs) do |t|
|
10
|
+
t.files = ['lib/**/*.rb']
|
11
|
+
t.options = ['-m', 'markdown', '--no-private', '-r', 'Readme.md', '--title', 'Date Validator documentation']
|
37
12
|
end
|
38
13
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
14
|
+
site = 'doc'
|
15
|
+
source_branch = 'master'
|
16
|
+
deploy_branch = 'gh-pages'
|
17
|
+
|
18
|
+
desc "generate and deploy documentation website to github pages"
|
19
|
+
multitask :pages do
|
20
|
+
puts ">>> Deploying #{deploy_branch} branch to Github Pages <<<"
|
21
|
+
require 'git'
|
22
|
+
repo = Git.open('.')
|
23
|
+
puts "\n>>> Checking out #{deploy_branch} branch <<<\n"
|
24
|
+
repo.branch("#{deploy_branch}").checkout
|
25
|
+
(Dir["*"] - [site]).each { |f| rm_rf(f) }
|
26
|
+
Dir["#{site}/*"].each {|f| mv(f, "./")}
|
27
|
+
rm_rf(site)
|
28
|
+
puts "\n>>> Moving generated site files <<<\n"
|
29
|
+
Dir["**/*"].each {|f| repo.add(f) }
|
30
|
+
repo.status.deleted.each {|f, s| repo.remove(f)}
|
31
|
+
puts "\n>>> Commiting: Site updated at #{Time.now.utc} <<<\n"
|
32
|
+
message = ENV["MESSAGE"] || "Site updated at #{Time.now.utc}"
|
33
|
+
repo.commit(message)
|
34
|
+
puts "\n>>> Pushing generated site to #{deploy_branch} branch <<<\n"
|
35
|
+
repo.push
|
36
|
+
puts "\n>>> Github Pages deploy complete <<<\n"
|
37
|
+
repo.branch("#{source_branch}").checkout
|
44
38
|
end
|
45
39
|
|
40
|
+
task :doc => [:docs]
|
46
41
|
|
47
|
-
|
48
|
-
|
49
|
-
if File.exist?('VERSION')
|
50
|
-
version = File.read('VERSION')
|
51
|
-
else
|
52
|
-
version = ""
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
rdoc.rdoc_dir = 'rdoc'
|
57
|
-
rdoc.title = "date_validator #{version}"
|
58
|
-
rdoc.rdoc_files.include('README*')
|
59
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
60
|
-
end
|
42
|
+
task :default => :spec
|
43
|
+
task :test => [:spec]
|
data/Readme.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# date_validator
|
2
|
+
|
3
|
+
A simple date validator for Rails 3. Compatible with Ruby 1.8.7, 1.9.2 and
|
4
|
+
Rubinius 1.2.2.
|
5
|
+
|
6
|
+
$ gem sources -a http://gemcutter.org/
|
7
|
+
$ gem install date_validator
|
8
|
+
|
9
|
+
And I mean simple. In your model:
|
10
|
+
|
11
|
+
validates :expiration_date,
|
12
|
+
:date => {:after => Proc.new { Time.now },
|
13
|
+
:before => Proc.new { Time.now + 1.year } }
|
14
|
+
# Using Proc.new prevents production cache issues
|
15
|
+
|
16
|
+
For now the available options you can use are `:after`, `:before`,
|
17
|
+
`:after_or_equal_to` and `:before_or_equal_to`.
|
18
|
+
|
19
|
+
Pretty much self-explanatory! :)
|
20
|
+
|
21
|
+
## Note on Patches/Pull Requests
|
22
|
+
|
23
|
+
* Fork the project.
|
24
|
+
* Make your feature addition or bug fix.
|
25
|
+
* Add tests for it. This is important so I don't break it in a
|
26
|
+
future version unintentionally.
|
27
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
28
|
+
* Send us a pull request. Bonus points for topic branches.
|
29
|
+
|
30
|
+
## Copyright
|
31
|
+
|
32
|
+
Copyright (c) 2011 Codegram. See LICENSE for details.
|
data/date_validator.gemspec
CHANGED
@@ -1,74 +1,31 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "date_validator/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |s|
|
7
|
-
s.name
|
8
|
-
s.version
|
9
|
-
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
6
|
+
s.name = "date_validator"
|
7
|
+
s.version = DateValidator::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Oriol Gual", "Josep M. Bach", "Josep Jaume Rey"]
|
10
|
+
s.email = ["info@codegram.com"]
|
11
|
+
s.homepage = "http://github.com/codegram/date_validator"
|
12
|
+
s.summary = %q{A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel.}
|
13
13
|
s.description = %q{A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel. Currently supporting :after, :before, :after_or_equal_to and :before_or_equal_to options.}
|
14
|
-
s.email = %q{info@codegram.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.rdoc"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".bundle/config",
|
21
|
-
".document",
|
22
|
-
".gitignore",
|
23
|
-
".rspec",
|
24
|
-
".rvmrc",
|
25
|
-
"Gemfile",
|
26
|
-
"Gemfile.lock",
|
27
|
-
"LICENSE",
|
28
|
-
"README.rdoc",
|
29
|
-
"Rakefile",
|
30
|
-
"VERSION",
|
31
|
-
"date_validator.gemspec",
|
32
|
-
"lib/date_validator.rb",
|
33
|
-
"locales/ca.yml",
|
34
|
-
"locales/en.yml",
|
35
|
-
"locales/es.yml",
|
36
|
-
"spec/date_validator_spec.rb",
|
37
|
-
"spec/spec_helper.rb"
|
38
|
-
]
|
39
|
-
s.homepage = %q{http://github.com/codegram/date_validator}
|
40
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
41
|
-
s.require_paths = ["lib"]
|
42
|
-
s.rubygems_version = %q{1.3.7}
|
43
|
-
s.summary = %q{A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel.}
|
44
|
-
s.test_files = [
|
45
|
-
"spec/date_validator_spec.rb",
|
46
|
-
"spec/spec_helper.rb"
|
47
|
-
]
|
48
14
|
|
49
|
-
|
50
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
|
-
s.specification_version = 3
|
15
|
+
s.rubyforge_project = "date_validator"
|
52
16
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
else
|
60
|
-
s.add_dependency(%q<activemodel>, [">= 3.0.0"])
|
61
|
-
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
|
62
|
-
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
63
|
-
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
64
|
-
s.add_dependency(%q<tzinfo>, [">= 0"])
|
65
|
-
end
|
66
|
-
else
|
67
|
-
s.add_dependency(%q<activemodel>, [">= 3.0.0"])
|
68
|
-
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
|
69
|
-
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
70
|
-
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
71
|
-
s.add_dependency(%q<tzinfo>, [">= 0"])
|
72
|
-
end
|
73
|
-
end
|
17
|
+
s.add_runtime_dependency 'activemodel', '~>3.0.0'
|
18
|
+
|
19
|
+
s.add_development_dependency 'rspec', '~> 2.5.0'
|
20
|
+
s.add_development_dependency 'simplecov'
|
21
|
+
s.add_development_dependency 'activesupport', '~> 3.0.0'
|
22
|
+
s.add_development_dependency 'tzinfo', '~> 0.3.0'
|
74
23
|
|
24
|
+
s.add_development_dependency 'yard'
|
25
|
+
s.add_development_dependency 'bluecloth'
|
26
|
+
|
27
|
+
s.files = `git ls-files`.split("\n")
|
28
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
29
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'active_model/validations'
|
2
|
+
|
3
|
+
# ActiveModel Rails module.
|
4
|
+
module ActiveModel
|
5
|
+
|
6
|
+
# ActiveModel::Validations Rails module. Contains all the default validators.
|
7
|
+
module Validations
|
8
|
+
|
9
|
+
# Date Validator. Inherits from ActiveModel::EachValidator.
|
10
|
+
#
|
11
|
+
# Responds to the regular validator API methods `#check_validity` and
|
12
|
+
# `#validate_each`.
|
13
|
+
class DateValidator < ActiveModel::EachValidator
|
14
|
+
|
15
|
+
# Implemented checks and their associated operators.
|
16
|
+
CHECKS = { :after => :>, :after_or_equal_to => :>=,
|
17
|
+
:before => :<, :before_or_equal_to => :<=}.freeze
|
18
|
+
|
19
|
+
# Call `#initialize` on the superclass, adding a default
|
20
|
+
# `:allow_nil => false` option.
|
21
|
+
def initialize(options)
|
22
|
+
super(options.reverse_merge(:allow_nil => false))
|
23
|
+
end
|
24
|
+
|
25
|
+
# Validates the arguments passed to the validator.
|
26
|
+
#
|
27
|
+
# They must be either any kind of <Time>, a <Proc>, or a <Symbol>.
|
28
|
+
def check_validity!
|
29
|
+
keys = CHECKS.keys
|
30
|
+
options.slice(*keys).each do |option, value|
|
31
|
+
next if is_time?(value) || value.is_a?(Proc) || value.is_a?(Symbol) || (defined?(ActiveSupport::TimeWithZone) and value.is_a? ActiveSupport::TimeWithZone)
|
32
|
+
raise ArgumentError, ":#{option} must be a time, a date, a time_with_zone, a symbol or a proc"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# The actual validator method. It is called when ActiveRecord iterates
|
37
|
+
# over all the validators.
|
38
|
+
def validate_each(record, attr_name, value)
|
39
|
+
return if options[:allow_nil] && value.nil?
|
40
|
+
|
41
|
+
unless value
|
42
|
+
record.errors.add(attr_name, :not_a_date, options)
|
43
|
+
return
|
44
|
+
end
|
45
|
+
|
46
|
+
options.slice(*CHECKS.keys).each do |option, option_value|
|
47
|
+
option_value = option_value.call(record) if option_value.is_a?(Proc)
|
48
|
+
option_value = record.send(option_value) if option_value.is_a?(Symbol)
|
49
|
+
|
50
|
+
original_value = value
|
51
|
+
original_option_value = option_value
|
52
|
+
|
53
|
+
# To enable to_i conversion, these types must be converted to Datetimes
|
54
|
+
if defined?(ActiveSupport::TimeWithZone)
|
55
|
+
option_value = option_value.to_datetime if option_value.is_a?(ActiveSupport::TimeWithZone)
|
56
|
+
value = value.to_datetime if value.is_a?(ActiveSupport::TimeWithZone)
|
57
|
+
end
|
58
|
+
|
59
|
+
if defined?(Date)
|
60
|
+
option_value = option_value.to_datetime if option_value.is_a?(Date)
|
61
|
+
value = value.to_datetime if value.is_a?(Date)
|
62
|
+
end
|
63
|
+
|
64
|
+
unless is_time?(option_value) && value.to_i.send(CHECKS[option], option_value.to_i)
|
65
|
+
record.errors.add(attr_name, option, options.merge(:value => original_value, :date => original_option_value))
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def is_time?(object)
|
73
|
+
object.is_a?(Time) || (defined?(Date) and object.is_a?(Date)) || (defined?(ActiveSupport::TimeWithZone) and object.is_a?(ActiveSupport::TimeWithZone))
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/date_validator.rb
CHANGED
@@ -1,63 +1,15 @@
|
|
1
|
-
require 'active_model'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
module Validations
|
5
|
-
class DateValidator < ActiveModel::EachValidator
|
6
|
-
CHECKS = { :after => :>, :after_or_equal_to => :>=,
|
7
|
-
:before => :<, :before_or_equal_to => :<=}.freeze
|
8
|
-
|
9
|
-
def initialize(options)
|
10
|
-
super(options.reverse_merge(:allow_nil => false))
|
11
|
-
end
|
12
|
-
|
13
|
-
def check_validity!
|
14
|
-
keys = CHECKS.keys
|
15
|
-
options.slice(*keys).each do |option, value|
|
16
|
-
next if is_time?(value) || value.is_a?(Proc) || value.is_a?(Symbol) || (defined?(ActiveSupport::TimeWithZone) and value.is_a? ActiveSupport::TimeWithZone)
|
17
|
-
raise ArgumentError, ":#{option} must be a time, a date, a time_with_zone, a symbol or a proc"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def validate_each(record, attr_name, value)
|
22
|
-
|
23
|
-
return if options[:allow_nil] && value.nil?
|
24
|
-
|
25
|
-
unless value
|
26
|
-
record.errors.add(attr_name, :not_a_date, options)
|
27
|
-
return
|
28
|
-
end
|
29
|
-
|
30
|
-
options.slice(*CHECKS.keys).each do |option, option_value|
|
31
|
-
option_value = option_value.call(record) if option_value.is_a?(Proc)
|
32
|
-
option_value = record.send(option_value) if option_value.is_a?(Symbol)
|
33
|
-
|
34
|
-
original_value = value
|
35
|
-
original_option_value = option_value
|
36
|
-
|
37
|
-
# To enable to_i conversion, these types must be converted to Datetimes
|
38
|
-
if defined?(ActiveSupport::TimeWithZone)
|
39
|
-
option_value = option_value.to_datetime if option_value.is_a?(ActiveSupport::TimeWithZone)
|
40
|
-
value = value.to_datetime if value.is_a?(ActiveSupport::TimeWithZone)
|
41
|
-
end
|
42
|
-
|
43
|
-
if defined?(Date)
|
44
|
-
option_value = option_value.to_datetime if option_value.is_a?(Date)
|
45
|
-
value = value.to_datetime if value.is_a?(Date)
|
46
|
-
end
|
47
|
-
|
48
|
-
unless is_time?(option_value) && value.to_i.send(CHECKS[option], option_value.to_i)
|
49
|
-
record.errors.add(attr_name, option, options.merge(:value => original_value, :date => original_option_value))
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def is_time?(object)
|
55
|
-
object.is_a?(Time) || (defined?(Date) and object.is_a?(Date)) || (defined?(ActiveSupport::TimeWithZone) and object.is_a?(ActiveSupport::TimeWithZone))
|
56
|
-
end
|
1
|
+
require 'active_model/validations/date_validator'
|
2
|
+
require 'active_support/i18n'
|
57
3
|
|
58
|
-
|
59
|
-
|
4
|
+
# A simple date validator for Rails 3.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# validates :expiration_date,
|
8
|
+
# :date => {:after => Proc.new { Time.now },
|
9
|
+
# :before => Proc.new { Time.now + 1.year } }
|
10
|
+
# # Using Proc.new prevents production cache issues
|
11
|
+
#
|
12
|
+
module DateValidator
|
60
13
|
end
|
61
14
|
|
62
|
-
require 'active_support/i18n'
|
63
15
|
I18n.load_path += Dir[File.expand_path(File.join(File.dirname(__FILE__), '../locales', '*.yml')).to_s]
|
data/spec/date_validator_spec.rb
CHANGED
@@ -1,125 +1,138 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
4
5
|
|
5
|
-
describe
|
6
|
+
describe DateValidator do
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
it "checks validity of the arguments" do
|
12
|
-
[3, "foo", 1..6].each do |wrong_argument|
|
13
|
-
expect {
|
14
|
-
TestRecord.validates :expiration_date, :date => {:before => wrong_argument}
|
15
|
-
}.to raise_error(ArgumentError, ":before must be a time, a date, a time_with_zone, a symbol or a proc")
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
it "complains if provided with no options" do
|
20
|
-
TestRecord.validates :expiration_date, :date => {:before => Time.now}
|
21
|
-
model = TestRecord.new(nil)
|
22
|
-
model.should_not be_valid
|
23
|
-
model.errors[:expiration_date].should == ["is not a date"]
|
24
|
-
end
|
25
|
-
|
26
|
-
[:valid,:invalid].each do |should_be|
|
27
|
-
|
28
|
-
_context = should_be == :valid ? 'when value validates correctly' : 'when value does not match validation requirements'
|
29
|
-
|
30
|
-
context _context do
|
8
|
+
before do
|
9
|
+
TestRecord.reset_callbacks(:validate)
|
10
|
+
end
|
31
11
|
|
32
|
-
|
12
|
+
it "checks validity of the arguments" do
|
13
|
+
[3, "foo", 1..6].each do |wrong_argument|
|
14
|
+
expect {
|
15
|
+
TestRecord.validates :expiration_date,
|
16
|
+
:date => {:before => wrong_argument}
|
17
|
+
}.to raise_error(ArgumentError, ":before must be a time, a date, a time_with_zone, a symbol or a proc")
|
18
|
+
end
|
19
|
+
end
|
33
20
|
|
34
|
-
|
21
|
+
it "complains when no options are provided" do
|
22
|
+
TestRecord.validates :expiration_date,
|
23
|
+
:date => {:before => Time.now}
|
35
24
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
when :before_or_equal_to then should_be == :valid ? now : now + 21000
|
41
|
-
end
|
25
|
+
model = TestRecord.new(nil)
|
26
|
+
model.should_not be_valid
|
27
|
+
model.errors[:expiration_date].should eq(["is not a date"])
|
28
|
+
end
|
42
29
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
30
|
+
[:valid,:invalid].each do |should_be|
|
31
|
+
_context = should_be == :valid ? 'when value validates correctly' : 'when value does not match validation requirements'
|
32
|
+
|
33
|
+
context _context do
|
34
|
+
[:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
|
35
|
+
now = Time.now.to_datetime
|
36
|
+
|
37
|
+
model_date = case check
|
38
|
+
when :after then should_be == :valid ? now + 21000 : now - 1
|
39
|
+
when :before then should_be == :valid ? now - 21000 : now + 1
|
40
|
+
when :after_or_equal_to then should_be == :valid ? now : now - 21000
|
41
|
+
when :before_or_equal_to then should_be == :valid ? now : now + 21000
|
42
|
+
end
|
43
|
+
|
44
|
+
it "ensures that an attribute is #{should_be} when #{should_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
|
45
|
+
TestRecord.validates :expiration_date,
|
46
|
+
:date => {:"#{check}" => Time.now}
|
47
|
+
|
48
|
+
model = TestRecord.new(model_date)
|
49
|
+
should_be == :valid ? model.should(be_valid, "an attribute should be valid when respecting the #{check} check")\
|
50
|
+
: model.should_not(be_valid, "an attribute should be invalidwhen offending the #{check} check")
|
51
|
+
end
|
52
|
+
|
53
|
+
if _context == 'when value does not match validation requirements'
|
54
|
+
it "yields a default error message indicating that value must be #{check} validation requirements" do
|
55
|
+
TestRecord.validates :expiration_date,
|
56
|
+
:date => {:"#{check}" => Time.now}
|
57
|
+
|
58
|
+
model = TestRecord.new(model_date)
|
59
|
+
model.should_not be_valid
|
60
|
+
model.errors[:expiration_date].should eq(["must be " + check.to_s.gsub('_',' ') + " #{Time.now}"])
|
61
|
+
end
|
62
|
+
end
|
47
63
|
end
|
48
64
|
|
49
65
|
if _context == 'when value does not match validation requirements'
|
66
|
+
it "allows for a custom validation message" do
|
67
|
+
TestRecord.validates :expiration_date,
|
68
|
+
:date => {:before_or_equal_to => Time.now,
|
69
|
+
:message => 'must be after Christmas'}
|
50
70
|
|
51
|
-
|
52
|
-
TestRecord.validates :expiration_date, :date => {:"#{check}" => Time.now}
|
53
|
-
model = TestRecord.new(model_date)
|
71
|
+
model = TestRecord.new(Time.now + 21000)
|
54
72
|
model.should_not be_valid
|
55
|
-
model.errors[:expiration_date].should
|
73
|
+
model.errors[:expiration_date].should eq(["must be after Christmas"])
|
56
74
|
end
|
57
|
-
|
58
|
-
end
|
59
75
|
|
60
|
-
|
76
|
+
it "allows custom validation message to be handled by I18n" do
|
77
|
+
custom_message = 'Custom Date Message'
|
78
|
+
I18n.backend.store_translations('en', {:errors => {:messages => {:not_a_date => custom_message}}})
|
61
79
|
|
62
|
-
|
80
|
+
TestRecord.validates :expiration_date, :date => true
|
63
81
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
82
|
+
model = TestRecord.new(nil)
|
83
|
+
model.should_not be_valid
|
84
|
+
model.errors[:expiration_date].should eq([custom_message])
|
85
|
+
end
|
86
|
+
end
|
70
87
|
|
71
|
-
it "allows custom validation message to be handled by I18n" do
|
72
|
-
custom_message = 'Custom Date Message'
|
73
|
-
I18n.backend.store_translations('en', {:errors => {:messages => {:not_a_date => custom_message}}})
|
74
|
-
TestRecord.validates :expiration_date, :date => true
|
75
|
-
model = TestRecord.new(nil)
|
76
|
-
model.should_not be_valid
|
77
|
-
model.errors[:expiration_date].should == [custom_message]
|
78
88
|
end
|
89
|
+
end
|
79
90
|
|
91
|
+
extra_types = [:proc, :symbol]
|
92
|
+
extra_types.push(:date) if defined?(Date) and defined?(DateTime)
|
93
|
+
extra_types.push(:time_with_zone) if defined?(ActiveSupport::TimeWithZone)
|
94
|
+
|
95
|
+
extra_types.each do |type|
|
96
|
+
it "accepts a #{type} as an argument to a check" do
|
97
|
+
case type
|
98
|
+
when :proc then
|
99
|
+
expect {
|
100
|
+
TestRecord.validates :expiration_date,
|
101
|
+
:date => {:after => Proc.new{Time.now + 21000}}
|
102
|
+
}.to_not raise_error
|
103
|
+
when :symbol then
|
104
|
+
expect {
|
105
|
+
TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
|
106
|
+
TestRecord.validates :expiration_date, :date => {:after => :min_date}
|
107
|
+
}.to_not raise_error
|
108
|
+
when :date then
|
109
|
+
expect {
|
110
|
+
TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date}
|
111
|
+
}.to_not raise_error
|
112
|
+
when :time_with_zone then
|
113
|
+
expect {
|
114
|
+
Time.zone = "Hawaii"
|
115
|
+
TestRecord.validates :expiration_date, :date => {:before => Time.zone.parse((Time.now + 21000).to_s)}
|
116
|
+
}.to_not raise_error
|
117
|
+
end
|
118
|
+
end
|
80
119
|
end
|
81
120
|
|
82
|
-
|
121
|
+
it "gracefully handles an unexpected result from a proc argument evaluation" do
|
122
|
+
TestRecord.validates :expiration_date,
|
123
|
+
:date => {:after => Proc.new{ nil }}
|
83
124
|
|
84
|
-
|
125
|
+
TestRecord.new(Time.now).should_not be_valid
|
126
|
+
end
|
85
127
|
|
86
|
-
|
87
|
-
|
88
|
-
|
128
|
+
it "gracefully handles an unexpected result from a symbol argument evaluation" do
|
129
|
+
TestRecord.send(:define_method, :min_date, lambda { nil })
|
130
|
+
TestRecord.validates :expiration_date,
|
131
|
+
:date => {:after => :min_date}
|
89
132
|
|
90
|
-
|
91
|
-
it "accepts a #{type} as an argument to a check" do
|
92
|
-
case type
|
93
|
-
when :proc then
|
94
|
-
expect {
|
95
|
-
TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}}
|
96
|
-
}.to_not raise_error
|
97
|
-
when :symbol then
|
98
|
-
expect {
|
99
|
-
TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
|
100
|
-
TestRecord.validates :expiration_date, :date => {:after => :min_date}
|
101
|
-
}.to_not raise_error
|
102
|
-
when :date then
|
103
|
-
expect {
|
104
|
-
TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date}
|
105
|
-
}.to_not raise_error
|
106
|
-
when :time_with_zone then
|
107
|
-
expect {
|
108
|
-
Time.zone = "Hawaii"
|
109
|
-
TestRecord.validates :expiration_date, :date => {:before => Time.zone.parse((Time.now + 21000).to_s)}
|
110
|
-
}.to_not raise_error
|
133
|
+
TestRecord.new(Time.now).should_not be_valid
|
111
134
|
end
|
112
135
|
end
|
113
|
-
end
|
114
|
-
|
115
|
-
it "gracefully handles an unexpected result from a proc argument evaluation" do
|
116
|
-
TestRecord.validates :expiration_date, :date => {:after => Proc.new{ nil }}
|
117
|
-
TestRecord.new(Time.now).should_not be_valid
|
118
|
-
end
|
119
136
|
|
120
|
-
it "gracefully handles an unexpected result from a symbol argument evaluation" do
|
121
|
-
TestRecord.send(:define_method, :min_date, lambda { nil })
|
122
|
-
TestRecord.validates :expiration_date, :date => {:after => :min_date}
|
123
|
-
TestRecord.new(Time.now).should_not be_valid
|
124
137
|
end
|
125
138
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
1
|
require 'simplecov'
|
4
2
|
SimpleCov.start do
|
5
3
|
add_group "Lib", "lib"
|
6
4
|
end
|
7
5
|
|
8
|
-
require '
|
9
|
-
|
6
|
+
require 'rspec'
|
7
|
+
|
8
|
+
require 'active_support/time' # For testing Date and TimeWithZone objects
|
10
9
|
|
10
|
+
require 'active_model'
|
11
11
|
require 'date_validator'
|
12
|
-
require 'rspec'
|
13
12
|
|
14
13
|
class TestRecord
|
15
14
|
include ActiveModel::Validations
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 6
|
8
|
-
- 0
|
9
|
-
version: 0.6.0
|
4
|
+
prerelease:
|
5
|
+
version: 0.6.1
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Oriol Gual
|
@@ -16,7 +12,7 @@ autorequire:
|
|
16
12
|
bindir: bin
|
17
13
|
cert_chain: []
|
18
14
|
|
19
|
-
date: 2011-
|
15
|
+
date: 2011-03-04 00:00:00 +01:00
|
20
16
|
default_executable:
|
21
17
|
dependencies:
|
22
18
|
- !ruby/object:Gem::Dependency
|
@@ -25,12 +21,8 @@ dependencies:
|
|
25
21
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
22
|
none: false
|
27
23
|
requirements:
|
28
|
-
- -
|
24
|
+
- - ~>
|
29
25
|
- !ruby/object:Gem::Version
|
30
|
-
segments:
|
31
|
-
- 3
|
32
|
-
- 0
|
33
|
-
- 0
|
34
26
|
version: 3.0.0
|
35
27
|
type: :runtime
|
36
28
|
version_requirements: *id001
|
@@ -40,69 +32,75 @@ dependencies:
|
|
40
32
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
33
|
none: false
|
42
34
|
requirements:
|
43
|
-
- -
|
35
|
+
- - ~>
|
44
36
|
- !ruby/object:Gem::Version
|
45
|
-
|
46
|
-
- 2
|
47
|
-
- 0
|
48
|
-
- 0
|
49
|
-
- beta
|
50
|
-
- 20
|
51
|
-
version: 2.0.0.beta.20
|
37
|
+
version: 2.5.0
|
52
38
|
type: :development
|
53
39
|
version_requirements: *id002
|
54
40
|
- !ruby/object:Gem::Dependency
|
55
|
-
name:
|
41
|
+
name: simplecov
|
56
42
|
prerelease: false
|
57
43
|
requirement: &id003 !ruby/object:Gem::Requirement
|
58
44
|
none: false
|
59
45
|
requirements:
|
60
46
|
- - ">="
|
61
47
|
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
- 3
|
64
|
-
- 0
|
65
|
-
- 0
|
66
|
-
version: 3.0.0
|
48
|
+
version: "0"
|
67
49
|
type: :development
|
68
50
|
version_requirements: *id003
|
69
51
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
52
|
+
name: activesupport
|
71
53
|
prerelease: false
|
72
54
|
requirement: &id004 !ruby/object:Gem::Requirement
|
73
55
|
none: false
|
74
56
|
requirements:
|
75
|
-
- -
|
57
|
+
- - ~>
|
76
58
|
- !ruby/object:Gem::Version
|
77
|
-
|
78
|
-
- 1
|
79
|
-
- 0
|
80
|
-
- 0
|
81
|
-
version: 1.0.0
|
59
|
+
version: 3.0.0
|
82
60
|
type: :development
|
83
61
|
version_requirements: *id004
|
84
62
|
- !ruby/object:Gem::Dependency
|
85
63
|
name: tzinfo
|
86
64
|
prerelease: false
|
87
65
|
requirement: &id005 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.3.0
|
71
|
+
type: :development
|
72
|
+
version_requirements: *id005
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: yard
|
75
|
+
prerelease: false
|
76
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
88
77
|
none: false
|
89
78
|
requirements:
|
90
79
|
- - ">="
|
91
80
|
- !ruby/object:Gem::Version
|
92
|
-
segments:
|
93
|
-
- 0
|
94
81
|
version: "0"
|
95
82
|
type: :development
|
96
|
-
version_requirements: *
|
83
|
+
version_requirements: *id006
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: bluecloth
|
86
|
+
prerelease: false
|
87
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: "0"
|
93
|
+
type: :development
|
94
|
+
version_requirements: *id007
|
97
95
|
description: A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel. Currently supporting :after, :before, :after_or_equal_to and :before_or_equal_to options.
|
98
|
-
email:
|
96
|
+
email:
|
97
|
+
- info@codegram.com
|
99
98
|
executables: []
|
100
99
|
|
101
100
|
extensions: []
|
102
101
|
|
103
|
-
extra_rdoc_files:
|
104
|
-
|
105
|
-
- README.rdoc
|
102
|
+
extra_rdoc_files: []
|
103
|
+
|
106
104
|
files:
|
107
105
|
- .bundle/config
|
108
106
|
- .document
|
@@ -111,12 +109,12 @@ files:
|
|
111
109
|
- .rvmrc
|
112
110
|
- Gemfile
|
113
111
|
- Gemfile.lock
|
114
|
-
- LICENSE
|
115
|
-
- README.rdoc
|
116
112
|
- Rakefile
|
117
|
-
-
|
113
|
+
- Readme.md
|
118
114
|
- date_validator.gemspec
|
115
|
+
- lib/active_model/validations/date_validator.rb
|
119
116
|
- lib/date_validator.rb
|
117
|
+
- lib/date_validator/version.rb
|
120
118
|
- locales/ca.yml
|
121
119
|
- locales/en.yml
|
122
120
|
- locales/es.yml
|
@@ -127,8 +125,8 @@ homepage: http://github.com/codegram/date_validator
|
|
127
125
|
licenses: []
|
128
126
|
|
129
127
|
post_install_message:
|
130
|
-
rdoc_options:
|
131
|
-
|
128
|
+
rdoc_options: []
|
129
|
+
|
132
130
|
require_paths:
|
133
131
|
- lib
|
134
132
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -136,21 +134,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
136
134
|
requirements:
|
137
135
|
- - ">="
|
138
136
|
- !ruby/object:Gem::Version
|
139
|
-
segments:
|
140
|
-
- 0
|
141
137
|
version: "0"
|
142
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
139
|
none: false
|
144
140
|
requirements:
|
145
141
|
- - ">="
|
146
142
|
- !ruby/object:Gem::Version
|
147
|
-
segments:
|
148
|
-
- 0
|
149
143
|
version: "0"
|
150
144
|
requirements: []
|
151
145
|
|
152
|
-
rubyforge_project:
|
153
|
-
rubygems_version: 1.
|
146
|
+
rubyforge_project: date_validator
|
147
|
+
rubygems_version: 1.5.2
|
154
148
|
signing_key:
|
155
149
|
specification_version: 3
|
156
150
|
summary: A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based on ActiveModel.
|
data/LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2009 Josep Mª Bach
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
= date_validator
|
2
|
-
|
3
|
-
A simple date validator for Rails 3. Compatible with Ruby 1.8, 1.9, JRuby 1.5.3
|
4
|
-
and Rubinius 1.1.
|
5
|
-
|
6
|
-
gem sources -a http://gemcutter.org/
|
7
|
-
gem install date_validator
|
8
|
-
|
9
|
-
And I mean simple. In your model:
|
10
|
-
|
11
|
-
validates :expiration_date, :date => {:after => Proc.new { Time.now } :before => Proc.new { Time.now + 1.year } }
|
12
|
-
# Using Proc.new prevents production cache issues
|
13
|
-
|
14
|
-
For now these are the available options you can use:
|
15
|
-
|
16
|
-
:after
|
17
|
-
:before
|
18
|
-
:after_or_equal_to
|
19
|
-
:before_or_equal_to
|
20
|
-
|
21
|
-
Pretty much self-explanatory! :)
|
22
|
-
|
23
|
-
== Note on Patches/Pull Requests
|
24
|
-
|
25
|
-
* Fork the project.
|
26
|
-
* Make your feature addition or bug fix.
|
27
|
-
* Add tests for it. This is important so I don't break it in a
|
28
|
-
future version unintentionally.
|
29
|
-
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
30
|
-
* Send me a pull request. Bonus points for topic branches.
|
31
|
-
|
32
|
-
== Copyright
|
33
|
-
|
34
|
-
Copyright (c) 2010 Codegram. See LICENSE for details.
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.6.0
|