active_model_validators_ex 0.3.0 → 0.3.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: fb623b9155719e88023b83b4833516fa09a614a0
4
- data.tar.gz: d0a82d484adf8b88c4c2e0f07d2d3670fb963b33
3
+ metadata.gz: e7e4923568aea755dd748e40e079e60342b3cab5
4
+ data.tar.gz: cd6d41be058b4b2c7feaaf0f5739d3550bb80ad8
5
5
  SHA512:
6
- metadata.gz: 0673775f33792232fd1ffc390dad636a0879f0b86821728784728b0cca87d5d8c4073e4ee66d16356a81d4ee708b7a9bd64d1096690e14e632f162fcabf32361
7
- data.tar.gz: d0742f05524b44c5dffec0c6a951a102705a1965fa024458ced3374b6dd08241d1329eaa5cc65ebabfc670311568840463063944d507720f31fee9bfefa79491
6
+ metadata.gz: c8591102bc58f6d967bb6b68ecfff0ec5faf4385efed81956f188c7103751baca638228701b8c463c9ac00434a670531ce3fd7a1f303290ce13fba87d0a1d03d
7
+ data.tar.gz: 6ffdaf156137c21e301610bfba8c0d99519db34e84167d405d4230bc9984f82df8441f3c3199f0698c915874549ba84a77bf8f20c787f5e1b602ef7652916d4f
data/README.md CHANGED
@@ -97,7 +97,7 @@ validations:
97
97
  validates :time, time_format: {
98
98
  # value can be either a lambda or a time, and indicates that
99
99
  # attribute value must be after this value
100
- after: lambda { Time.new(2014) },
100
+ after: lambda { |model| Time.new(2014) },
101
101
 
102
102
  # can be either true or false, indicates if nil is accepted
103
103
  # defaults to false
@@ -1,9 +1,17 @@
1
1
  class TimeFormatValidator < ActiveModel::EachValidator
2
+ def initialize(options)
3
+ options[:allow_nil] ||= false
4
+
5
+ super(options)
6
+ end
7
+
2
8
  def validate_each(record, attribute, value)
3
9
  return if options[:allow_nil] && value.nil?
4
10
 
5
11
  parsed_time = value.is_a?(Time) ? value : Time.parse(value.to_s)
6
12
 
13
+ previous_time = calculate_previous_time(record)
14
+
7
15
  if !previous_time.nil? and parsed_time < previous_time
8
16
  record.errors[attribute] << "invalid value, #{value} must be after #{previous_time}"
9
17
  end
@@ -11,10 +19,10 @@ class TimeFormatValidator < ActiveModel::EachValidator
11
19
  record.errors[attribute] << "invalid value, #{value} not valid for #{attribute}"
12
20
  end
13
21
 
14
- def previous_time
22
+ def calculate_previous_time(record)
15
23
  case options[:after].class.name
16
24
  when 'Proc'
17
- options[:after].call
25
+ options[:after].call(record)
18
26
  when 'Time'
19
27
  options[:after]
20
28
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveModelValidatorsEx
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -103,7 +103,7 @@ describe TimeFormatValidator do
103
103
  end
104
104
 
105
105
  context 'and after is a Proc that returns Time' do
106
- let(:after) { lambda { Time.now } }
106
+ let(:after) { lambda { |a| Time.now } }
107
107
  let(:options) do
108
108
  { attributes: attribute, allow_nil: false, after: after }
109
109
  end
@@ -186,7 +186,7 @@ describe TimeFormatValidator do
186
186
  end
187
187
 
188
188
  context 'and after is a Proc that returns Time' do
189
- let(:after) { lambda { Time.now } }
189
+ let(:after) { lambda { |a| Time.now } }
190
190
  let(:options) do
191
191
  { attributes: attribute, allow_nil: true, after: after }
192
192
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_validators_ex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - junhanamaki