dirtiness_validator 0.1.0 → 0.2.0
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 +4 -4
- data/README.md +10 -0
- data/lib/active_model/validations/dirtiness_validator.rb +12 -3
- data/lib/dirtiness_validator/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd48235c7968efdf36ec6cf61ec7ae1e5accca88
|
4
|
+
data.tar.gz: aecd29545ef37e9f1a9d7166fb0d19e6b7bb56ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdee454bad16955eb612373bf914470360a4fbbb5bb8c7c61968d5a3d88b25dee6ca2e5c7111b875cddcb96e3ab7a151533072d17af37d3d5053a3050b9fbad8
|
7
|
+
data.tar.gz: 74509a2fbbbd7df9d192a030990545db644ed379955039caf1f9fcb5ff11056c368256ecddb0d6739dee2572d4c4ef2366fec1b07c360609cd6bb85d6045ce75
|
data/README.md
CHANGED
@@ -29,6 +29,16 @@ class Vote < ActiveRecord::Base
|
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
32
|
+
Configuration options other than :message can be supplied with a symbol or a proc.
|
33
|
+
|
34
|
+
```
|
35
|
+
# This validates that last_voted_at.to_date is greater than last_voted_at_was.to_date.
|
36
|
+
validates :last_voted_at, dirtiness: { greater: :to_date }
|
37
|
+
|
38
|
+
# This does the same.
|
39
|
+
validates :last_voted_at, dirtiness: { greater: -> (value) { value.to_date } }
|
40
|
+
```
|
41
|
+
|
32
42
|
Note that Dirtiness Validator skips validation if the record is not persisted.
|
33
43
|
|
34
44
|
To use Dirtiness Validator with ActiveModel, you must include ActiveModell:Dirty and call its methods as instructed in your model.
|
@@ -6,15 +6,24 @@ module ActiveModel
|
|
6
6
|
CHECKS = { greater: :>, greater_or_equal: :>=, less: :<, less_or_equal: :<=,
|
7
7
|
equal: :==, not_equal: :!= }.freeze
|
8
8
|
|
9
|
-
def validate_each(record, attr_name,
|
9
|
+
def validate_each(record, attr_name, current_value)
|
10
10
|
return unless record.persisted?
|
11
11
|
|
12
12
|
# check before_type_cast && allow_xxx option
|
13
13
|
|
14
|
-
options.slice(*CHECKS.keys).each do |option,
|
14
|
+
options.slice(*CHECKS.keys).each do |option, option_value|
|
15
15
|
previous_value = record.__send__("#{attr_name}_was")
|
16
16
|
|
17
|
-
|
17
|
+
case option_value
|
18
|
+
when Symbol
|
19
|
+
current_value = current_value.__send__(option_value)
|
20
|
+
previous_value = previous_value.__send__(option_value)
|
21
|
+
when Proc
|
22
|
+
current_value = option_value.call(current_value)
|
23
|
+
previous_value = option_value.call(previous_value)
|
24
|
+
end
|
25
|
+
|
26
|
+
unless current_value.__send__(CHECKS[option], previous_value)
|
18
27
|
record.errors.add(attr_name, option, filtered_options(previous_value))
|
19
28
|
end
|
20
29
|
end
|