active_model_validators_ex 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7e4923568aea755dd748e40e079e60342b3cab5
|
4
|
+
data.tar.gz: cd6d41be058b4b2c7feaaf0f5739d3550bb80ad8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
@@ -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
|