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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91fc49dd77f67eb59f3c031853c292ae12a53d40
4
- data.tar.gz: 80c5280eb99c9ded250f9906eb25457cafc74ac3
3
+ metadata.gz: cd48235c7968efdf36ec6cf61ec7ae1e5accca88
4
+ data.tar.gz: aecd29545ef37e9f1a9d7166fb0d19e6b7bb56ab
5
5
  SHA512:
6
- metadata.gz: 40096c45d791d360010a7213667a5ad1f85ed11997b3c9e624e809cef5bada75dd0677073e27301fe5ce69cc475342c016e5c6724611491be59f71af22ca4047
7
- data.tar.gz: 6882fdb9f5e7894a1a5238915809cd41868bba8055fbf5d123dbf8700dbacb1c040f0febafd532b75ff0564cda0b67d570df1355d472a12d54fb8a23760eec1a
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, value)
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
- unless value.__send__(CHECKS[option], previous_value)
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
@@ -1,3 +1,3 @@
1
1
  module DirtinessValidator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dirtiness_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tyamagu2