date_validator 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e95b0ebaffd070517405785c04fa35a692194777
4
- data.tar.gz: d902fa5d58b1c911b001a507b11ac0539a2190d7
3
+ metadata.gz: 10530abca992905ef4966abc59bde16277badc80
4
+ data.tar.gz: ec91b26e1d5c4ec2f2a021da33b785e4c60d5371
5
5
  SHA512:
6
- metadata.gz: fb9ea1549680bef07e681e695d01e645c481ee1c6fa4611d41a42b918fc1a42a18b9711bd4620f8e32bfa5e85a820557a0d5c371ea59ac0febab72e5f2e7c926
7
- data.tar.gz: 6b3f9f4eea74c0a52d82ef399121b7a0ea386590f5a3772cb3dfbb3de006414973186efafbbfcd40e740333f35baec7f49985501bce59bc6acc1cbf0046fd189
6
+ metadata.gz: 33d51b09b3101e09ceb24e72fdab255c32be4e7ee766610163bbaaac793b7ab5d64c12a27a693642238e290c233e673d34a34c60a83603d75ffbadd70035c589
7
+ data.tar.gz: 0bb7e42f9e6bd99894952e29c5e5b1cffa388bb643cbe896b069097a36b74d3022bfe51855d372509b0ee32022da3851d7f71bf6f7e5eb3d0b852fcdd4d06ff0
@@ -1,8 +1,10 @@
1
1
  # http://about.travis-ci.org/docs/user/build-configuration/
2
2
  langauge: ruby
3
+ env:
4
+ - ACTIVE_MODEL_VERSION=3.2
5
+ - ACTIVE_MODEL_VERSION=4.0
3
6
  rvm:
4
- - 1.9.2
5
7
  - 1.9.3
6
8
  - 2.0.0
7
- - rbx-19mode
9
+ - rbx
8
10
  - jruby-19mode
data/Gemfile CHANGED
@@ -1,3 +1,21 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ active_model_version = ENV['ACTIVE_MODEL_VERSION'] || 'default'
6
+
7
+ active_model_opts =
8
+ case active_model_version
9
+ when 'master'
10
+ { github: 'rails/rails' }
11
+ when 'default'
12
+ '~> 3'
13
+ else
14
+ "~> #{active_model_version}"
15
+ end
16
+
17
+ gem 'activemodel', active_model_opts
18
+
19
+ platforms :rbx do
20
+ gem 'rubysl', '~> 2.0'
21
+ end
data/Rakefile CHANGED
@@ -41,8 +41,8 @@ begin
41
41
  repo.branch("#{source_branch}").checkout
42
42
  end
43
43
 
44
- task :doc => [:docs]
44
+ task doc: [:docs]
45
45
  rescue LoadError
46
46
  end
47
47
 
48
- task :default => :test
48
+ task default: :test
data/Readme.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # date_validator [![Build Status](https://travis-ci.org/codegram/date_validator.png?branch=master)](https://travis-ci.org/codegram/date_validator)
2
2
 
3
3
 
4
- A simple date validator for Rails 3. Should be compatible with all latest Rubies.
4
+ A simple date validator for Rails. Should be compatible with all latest Rubies.
5
5
 
6
6
 
7
7
  ```shell
@@ -10,10 +10,16 @@ $ gem install date_validator
10
10
 
11
11
  And I mean simple. In your model:
12
12
 
13
+ ```ruby
14
+ validates :expiration_date, date: true
15
+ ```
16
+
17
+ or with some options, such as:
18
+
13
19
  ```ruby
14
20
  validates :expiration_date,
15
- :date => {:after => Proc.new { Time.now },
16
- :before => Proc.new { Time.now + 1.year } }
21
+ date: { after: Proc.new { Time.now },
22
+ before: Proc.new { Time.now + 1.year } }
17
23
  # Using Proc.new prevents production cache issues
18
24
  ```
19
25
 
@@ -23,30 +29,30 @@ a Symbol instead of a block:
23
29
  ```ruby
24
30
  # Ensure the expiration date is after the packaging date
25
31
  validates :expiration_date,
26
- :date => {:after => :packaging_date}
32
+ date: { after: :packaging_date }
27
33
  ```
28
34
 
29
35
  For now the available options you can use are `:after`, `:before`,
30
- `:after_or_equal_to` and `:before_or_equal_to`.
36
+ `:after_or_equal_to`, `:before_or_equal_to` and `:equal_to`.
31
37
 
32
38
  If you want to specify a custom message, you can do so in the options hash:
33
39
 
34
40
  ```ruby
35
41
  validates :start_date,
36
- :date => {:after => Date.today, :message => 'must be after today'},
37
- :on => :create
42
+ date: { after: Proc.new { Date.today }, message: 'must be after today' },
43
+ on: :create
38
44
  ```
39
45
 
40
- Pretty much self-explanatory! :)
46
+ Pretty much self-explanatory! :)
41
47
 
42
48
  If you want to make sure an attribute is before/after another attribute, use:
43
49
 
44
50
  ```ruby
45
- validates :start_date, :date => {:before => :end_date }
51
+ validates :start_date, date: { before: :end_date }
46
52
  ```
47
53
 
48
54
  ## Note on Patches/Pull Requests
49
-
55
+
50
56
  * Fork the project.
51
57
  * Make your feature addition or bug fix.
52
58
  * Add tests for it. This is important so I don't break it in a
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.rubyforge_project = "date_validator"
16
16
 
17
- s.add_runtime_dependency 'activemodel', '>= 3'
17
+ s.add_runtime_dependency 'activemodel'
18
18
 
19
19
  s.add_development_dependency 'minitest'
20
20
  s.add_development_dependency 'rake'
@@ -13,13 +13,15 @@ module ActiveModel
13
13
  class DateValidator < ActiveModel::EachValidator
14
14
 
15
15
  # Implemented checks and their associated operators.
16
- CHECKS = { :after => :>, :after_or_equal_to => :>=,
17
- :before => :<, :before_or_equal_to => :<=}.freeze
16
+ CHECKS = { after: :>, after_or_equal_to: :>=,
17
+ before: :<, before_or_equal_to: :<=,
18
+ equal_to: :==
19
+ }.freeze
18
20
 
19
21
  # Call `#initialize` on the superclass, adding a default
20
- # `:allow_nil => false` option.
22
+ # `allow_nil: false` option.
21
23
  def initialize(options)
22
- super(options.reverse_merge(:allow_nil => false))
24
+ super(options.reverse_merge(allow_nil: false))
23
25
  end
24
26
 
25
27
  # Validates the arguments passed to the validator.
@@ -65,29 +67,29 @@ module ActiveModel
65
67
  record.errors.add(attr_name, :not_a_date, options)
66
68
  return
67
69
  end
68
-
70
+
69
71
  options.slice(*CHECKS.keys).each do |option, option_value|
70
72
  option_value = option_value.call(record) if option_value.is_a?(Proc)
71
73
  option_value = record.send(option_value) if option_value.is_a?(Symbol)
72
-
74
+
73
75
  original_value = value
74
76
  original_option_value = option_value
75
77
 
76
78
  # To enable to_i conversion, these types must be converted to Datetimes
77
79
  if defined?(ActiveSupport::TimeWithZone)
78
- option_value = option_value.to_datetime if option_value.is_a?(ActiveSupport::TimeWithZone)
79
- value = value.to_datetime if value.is_a?(ActiveSupport::TimeWithZone)
80
+ option_value = option_value.to_datetime if option_value.is_a?(ActiveSupport::TimeWithZone)
81
+ value = value.to_datetime if value.is_a?(ActiveSupport::TimeWithZone)
80
82
  end
81
83
 
82
84
  if defined?(Date)
83
- option_value = option_value.to_datetime if option_value.is_a?(Date)
84
- value = value.to_datetime if value.is_a?(Date)
85
+ option_value = option_value.to_datetime if option_value.is_a?(Date)
86
+ value = value.to_datetime if value.is_a?(Date)
85
87
  end
86
-
88
+
87
89
  unless is_time?(option_value) && value.to_i.send(CHECKS[option], option_value.to_i)
88
90
  record.errors.add(attr_name, option, options.merge(
89
- :value => original_value,
90
- :date => (I18n.localize(original_option_value) rescue original_option_value)
91
+ value: original_value,
92
+ date: (I18n.localize(original_option_value) rescue original_option_value)
91
93
  ))
92
94
  end
93
95
  end
@@ -104,8 +106,8 @@ module ActiveModel
104
106
  # Validates whether the value of the specified attribute is a validate Date
105
107
  #
106
108
  # class Person < ActiveRecord::Base
107
- # validates_date_of :payment_date, :after => :packaging_date
108
- # validates_date_of :expiration_date, :before => Proc.new { Time.now }
109
+ # validates_date_of :payment_date, after: :packaging_date
110
+ # validates_date_of :expiration_date, before: Proc.new { Time.now }
109
111
  # end
110
112
  #
111
113
  # Configuration options:
@@ -113,6 +115,7 @@ module ActiveModel
113
115
  # * <tt>:before</tt> - check that a Date is before the specified one.
114
116
  # * <tt>:after_or_equal_to</tt> - check that a Date is after or equal to the specified one.
115
117
  # * <tt>:before_or_equal_to</tt> - check that a Date is before or equal to the specified one.
118
+ # * <tt>:equal_to</tt> - check that a Date is equal to the specified one.
116
119
  def validates_date_of(*attr_names)
117
120
  validates_with DateValidator, _merge_attributes(attr_names)
118
121
  end
@@ -5,8 +5,8 @@ require 'active_support/i18n'
5
5
  #
6
6
  # @example
7
7
  # validates :expiration_date,
8
- # :date => {:after => Proc.new { Time.now },
9
- # :before => Proc.new { Time.now + 1.year } }
8
+ # date: { after: Proc.new { Time.now },
9
+ # before: Proc.new { Time.now + 1.year } }
10
10
  # # Using Proc.new prevents production cache issues
11
11
  #
12
12
  module DateValidator
@@ -1,4 +1,4 @@
1
1
  module DateValidator
2
2
  # The version number.
3
- VERSION = "0.7.0"
3
+ VERSION = "0.7.1"
4
4
  end
@@ -6,3 +6,4 @@ en:
6
6
  after_or_equal_to: "must be after or equal to %{date}"
7
7
  before: "must be before %{date}"
8
8
  before_or_equal_to: "must be before or equal to %{date}"
9
+ equal_to: "must be equal to %{date}"
@@ -0,0 +1,8 @@
1
+ it:
2
+ errors:
3
+ messages:
4
+ not_a_date: "non è una data"
5
+ after: "deve essere successiva a %{date}"
6
+ after_or_equal_to: "deve essere successiva o uguale a %{date}"
7
+ before: "deve essere antecedente a %{date}"
8
+ before_or_equal_to: "deve essere antecedente o uguale a %{date}"
@@ -0,0 +1,8 @@
1
+ pt-BR:
2
+ errors:
3
+ messages:
4
+ not_a_date: "não é uma data válida"
5
+ after: "deve ser após %{date}"
6
+ after_or_equal_to: "deve ser após ou igual a %{date}"
7
+ before: "deve ser antes de %{date}"
8
+ before_or_equal_to: "deve ser antes ou igual a %{date}"
@@ -1,7 +1,7 @@
1
1
  ru:
2
2
  errors:
3
3
  messages:
4
- not_a_date: "неверный формат"
4
+ not_a_date: "не является датой"
5
5
  after: "должна быть позднее чем %{date}"
6
6
  after_or_equal_to: "должна равняться или быть позднее чем %{date}"
7
7
  before: "должна быть ранее чем %{date}"
@@ -12,7 +12,7 @@ module ActiveModel
12
12
  it "checks validity of the arguments" do
13
13
  [3, "foo", 1..6].each do |wrong_argument|
14
14
  proc {
15
- TestRecord.validates(:expiration_date, :date => {:before => wrong_argument})
15
+ TestRecord.validates(:expiration_date, date: { before: wrong_argument })
16
16
  }.must_raise(ArgumentError, ":before must be a time, a date, a time_with_zone, a symbol or a proc")
17
17
  end
18
18
  end
@@ -20,7 +20,7 @@ module ActiveModel
20
20
  it "complains when no options are provided" do
21
21
  I18n.backend.reload!
22
22
  TestRecord.validates :expiration_date,
23
- :date => {:before => Time.now}
23
+ date: { before: Time.now }
24
24
 
25
25
  model = TestRecord.new(nil)
26
26
  model.valid?.must_equal false
@@ -29,7 +29,7 @@ module ActiveModel
29
29
 
30
30
  it "works with helper methods" do
31
31
  time = Time.now
32
- TestRecord.validates_date_of :expiration_date, :before => time
32
+ TestRecord.validates_date_of :expiration_date, before: time
33
33
  model = TestRecord.new(time + 20000)
34
34
  model.valid?.must_equal false
35
35
  end
@@ -38,19 +38,20 @@ module ActiveModel
38
38
  _context = must_be == :valid ? 'when value validates correctly' : 'when value does not match validation requirements'
39
39
 
40
40
  describe _context do
41
- [:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
41
+ [:after, :before, :after_or_equal_to, :before_or_equal_to, :equal_to].each do |check|
42
42
  now = Time.now.to_datetime
43
43
 
44
44
  model_date = case check
45
- when :after then must_be == :valid ? now + 21000 : now - 1
46
- when :before then must_be == :valid ? now - 21000 : now + 1
47
- when :after_or_equal_to then must_be == :valid ? now : now - 21000
45
+ when :after then must_be == :valid ? now + 21000 : now - 1
46
+ when :before then must_be == :valid ? now - 21000 : now + 1
47
+ when :after_or_equal_to then must_be == :valid ? now : now - 21000
48
48
  when :before_or_equal_to then must_be == :valid ? now : now + 21000
49
+ when :equal_to then must_be == :valid ? now : now + 21000
49
50
  end
50
51
 
51
52
  it "ensures that an attribute is #{must_be} when #{must_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
52
53
  TestRecord.validates :expiration_date,
53
- :date => {:"#{check}" => now}
54
+ date: {:"#{check}" => now}
54
55
 
55
56
  model = TestRecord.new(model_date)
56
57
  must_be == :valid ? model.valid?.must_equal(true) : model.valid?.must_equal(false)
@@ -59,7 +60,7 @@ module ActiveModel
59
60
  if _context == 'when value does not match validation requirements'
60
61
  it "yields a default error message indicating that value must be #{check} validation requirements" do
61
62
  TestRecord.validates :expiration_date,
62
- :date => {:"#{check}" => now}
63
+ date: {:"#{check}" => now}
63
64
 
64
65
  model = TestRecord.new(model_date)
65
66
  model.valid?.must_equal false
@@ -73,8 +74,8 @@ module ActiveModel
73
74
 
74
75
  it "allows for a custom validation message" do
75
76
  TestRecord.validates :expiration_date,
76
- :date => {:before_or_equal_to => now,
77
- :message => 'must be after Christmas'}
77
+ date: { before_or_equal_to: now,
78
+ message: 'must be after Christmas' }
78
79
 
79
80
  model = TestRecord.new(now + 21000)
80
81
  model.valid?.must_equal false
@@ -83,9 +84,9 @@ module ActiveModel
83
84
 
84
85
  it "allows custom validation message to be handled by I18n" do
85
86
  custom_message = 'Custom Date Message'
86
- I18n.backend.store_translations('en', {:errors => {:messages => {:not_a_date => custom_message}}})
87
+ I18n.backend.store_translations('en', { errors: { messages: { not_a_date: custom_message }}})
87
88
 
88
- TestRecord.validates :expiration_date, :date => true
89
+ TestRecord.validates :expiration_date, date: true
89
90
 
90
91
  model = TestRecord.new(nil)
91
92
  model.valid?.must_equal false
@@ -104,22 +105,22 @@ module ActiveModel
104
105
  it "accepts a #{type} as an argument to a check" do
105
106
  case type
106
107
  when :proc then
107
- TestRecord.validates(:expiration_date, :date => {:after => Proc.new{Time.now + 21000}}).must_be_kind_of Hash
108
+ TestRecord.validates(:expiration_date, date: { after: Proc.new {Time.now + 21000} }).must_be_kind_of Hash
108
109
  when :symbol then
109
110
  TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
110
- TestRecord.validates(:expiration_date, :date => {:after => :min_date}).must_be_kind_of Hash
111
+ TestRecord.validates(:expiration_date, date: { after: :min_date }).must_be_kind_of Hash
111
112
  when :date then
112
- TestRecord.validates(:expiration_date, :date => {:after => Time.now.to_date}).must_be_kind_of Hash
113
+ TestRecord.validates(:expiration_date, date: { after: Time.now.to_date }).must_be_kind_of Hash
113
114
  when :time_with_zone then
114
115
  Time.zone = "Hawaii"
115
- TestRecord.validates(:expiration_date, :date => {:before => Time.zone.parse((Time.now + 21000).to_s)}).must_be_kind_of Hash
116
+ TestRecord.validates(:expiration_date, date: { before: Time.zone.parse((Time.now + 21000).to_s) }).must_be_kind_of Hash
116
117
  end
117
118
  end
118
119
  end
119
120
 
120
121
  it "gracefully handles an unexpected result from a proc argument evaluation" do
121
122
  TestRecord.validates :expiration_date,
122
- :date => {:after => Proc.new{ nil }}
123
+ date: { after: Proc.new { nil } }
123
124
 
124
125
  TestRecord.new(Time.now).valid?.must_equal false
125
126
  end
@@ -127,7 +128,7 @@ module ActiveModel
127
128
  it "gracefully handles an unexpected result from a symbol argument evaluation" do
128
129
  TestRecord.send(:define_method, :min_date, lambda { nil })
129
130
  TestRecord.validates :expiration_date,
130
- :date => {:after => :min_date}
131
+ date: { after: :min_date }
131
132
 
132
133
  TestRecord.new(Time.now).valid?.must_equal false
133
134
  end
@@ -138,12 +139,12 @@ module ActiveModel
138
139
  end
139
140
 
140
141
  it "should detect invalid date expressions when nil is allowed" do
141
- TestRecord.validates(:expiration_date, :date => true, :allow_nil => true)
142
+ TestRecord.validates(:expiration_date, date: true, allow_nil: true)
142
143
  TestRecord.new(nil).valid?.must_equal false
143
144
  end
144
145
 
145
146
  it "should detect invalid date expressions when blank is allowed" do
146
- TestRecord.validates(:expiration_date, :date => true, :allow_blank => true)
147
+ TestRecord.validates(:expiration_date, date: true, allow_blank: true)
147
148
  TestRecord.new(nil).valid?.must_equal false
148
149
  end
149
150
  end
@@ -1,5 +1,5 @@
1
1
  begin
2
- require 'simplecov';
2
+ require 'simplecov'
3
3
  SimpleCov.start do
4
4
  add_group "Lib", 'lib'
5
5
  end
@@ -8,9 +8,10 @@ end
8
8
 
9
9
  begin; require 'turn'; rescue LoadError; end
10
10
 
11
- require 'minitest/spec'
11
+ gem 'minitest'
12
12
  require 'minitest/autorun'
13
13
 
14
+ require 'active_support/core_ext'
14
15
  require 'active_support/time' # For testing Date and TimeWithZone objects
15
16
 
16
17
  require 'active_model'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oriol Gual
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-05-04 00:00:00.000000000 Z
13
+ date: 2014-10-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '3'
21
+ version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - '>='
27
27
  - !ruby/object:Gem::Version
28
- version: '3'
28
+ version: '0'
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: minitest
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -110,8 +110,10 @@ files:
110
110
  - locales/en.yml
111
111
  - locales/es.yml
112
112
  - locales/fr.yml
113
+ - locales/it.yml
113
114
  - locales/nl.yml
114
115
  - locales/pl.yml
116
+ - locales/pt-BR.yml
115
117
  - locales/ru.yml
116
118
  - test/date_validator_test.rb
117
119
  - test/test_helper.rb
@@ -134,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
136
  version: '0'
135
137
  requirements: []
136
138
  rubyforge_project: date_validator
137
- rubygems_version: 2.0.0
139
+ rubygems_version: 2.0.14
138
140
  signing_key:
139
141
  specification_version: 4
140
142
  summary: A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3, based
@@ -142,3 +144,4 @@ summary: A simple, ORM agnostic, Ruby 1.9 compatible date validator for Rails 3,
142
144
  test_files:
143
145
  - test/date_validator_test.rb
144
146
  - test/test_helper.rb
147
+ has_rdoc: