lite-validators 1.7.0 → 1.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/docs/REFERENCE.md +2 -0
- data/lib/lite/validators/reference_validator.rb +11 -2
- data/lib/lite/validators/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cbc212144b10828145cc04c3117cd994c6b5b5ffd6539b8e9cb08d45e05b15b
|
4
|
+
data.tar.gz: 31deca154872899b7faa80838416c68c1830d97982c948301dc373238bc1a3b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da30d06d89209f263bd559b008db96618947228c0a4ca358df3301fdbcc09adcfe04e6e2b0b07d426ca15cff22baf8e804deefc21e80e08b94372d2bee912535
|
7
|
+
data.tar.gz: a6219df0e0563e79ded1216f7b81794bbf02f85aa4c68fa69b395b0a31a9a2286a3d35076dc9a1aadd67955f6badf63b76dd06f69b4437d633f92183d583d646
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [1.7.1] - 2022-03-17
|
10
|
+
### Changed
|
11
|
+
- Added polymorphic support to reference validator
|
12
|
+
|
9
13
|
## [1.7.0] - 2022-03-17
|
10
14
|
### Added
|
11
15
|
- Added Reference validator
|
data/Gemfile.lock
CHANGED
data/docs/REFERENCE.md
CHANGED
@@ -12,6 +12,7 @@
|
|
12
12
|
Option | Type | Available | Default
|
13
13
|
--- | --- | --- | ---
|
14
14
|
association | symbol | object method | attribute without `_id`
|
15
|
+
polymorphic | symbol | true, false | false
|
15
16
|
|
16
17
|
#### Usage
|
17
18
|
|
@@ -19,6 +20,7 @@ association | symbol | object method | attribute without `_id`
|
|
19
20
|
class User < ActiveRecord::Base
|
20
21
|
|
21
22
|
validates :input_id, reference: true
|
23
|
+
validates :output_id, reference: { polymorphic: true }
|
22
24
|
validates :output_id, reference: { association: :input }
|
23
25
|
|
24
26
|
end
|
@@ -7,7 +7,7 @@ class ReferenceValidator < BaseValidator
|
|
7
7
|
private
|
8
8
|
|
9
9
|
def valid?
|
10
|
-
valid_id? || valid_object?
|
10
|
+
(valid_id? && valid_type?) || valid_object?
|
11
11
|
end
|
12
12
|
|
13
13
|
def valid_id?
|
@@ -22,7 +22,16 @@ class ReferenceValidator < BaseValidator
|
|
22
22
|
|
23
23
|
def valid_object?
|
24
24
|
association_object = options[:association] || attribute.to_s.chomp('_id')
|
25
|
-
record.send(association_object).present?
|
25
|
+
return true if record.send(association_object).present?
|
26
|
+
|
27
|
+
record.errors.add(association_object, *error_message)
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_type?
|
31
|
+
return true unless options[:polymorphic]
|
32
|
+
|
33
|
+
association_type = "#{attribute.to_s.chomp('_id')}_type"
|
34
|
+
record.send(association_type).present?
|
26
35
|
end
|
27
36
|
|
28
37
|
end
|