soulless 1.1.1 → 1.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/lib/soulless/model.rb +4 -0
- data/lib/soulless/validations/using_validator.rb +45 -0
- data/lib/soulless/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1989bab6516b12bd424b892e317bf99c12df4ee
|
4
|
+
data.tar.gz: 33a9ae2c3165ee947351ba3a68ce539926bebb4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bbdfa08258b566f77da8fb3a772db5485bd920fa0f5ea6fb05513bfc686f3ade294ea8bf8c97eddac419c6aafb5fb20ea7fa57879584e7f0d9d68c82990868a
|
7
|
+
data.tar.gz: 8758ca6e635e814fe7542ae675525452dd7c7c3a9221b8b660890c0fa382dbabf9139ec858e3a97b283c84310c397351fb6f8cb0d91c5dccf6746b512f848cad
|
data/lib/soulless/model.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Soulless
|
2
|
+
module Validations
|
3
|
+
class UsingValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
check_options
|
6
|
+
return if should_not_continue?(value)
|
7
|
+
|
8
|
+
if value.is_a?(Array)
|
9
|
+
value.each { |v| perform(record, attribute, v) }
|
10
|
+
else
|
11
|
+
perform(record, attribute, value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def check_options
|
18
|
+
fail(ArgumentError, 'The `:model` option is required for the `using` validator') unless options[:model]
|
19
|
+
fail(ArgumentError, 'The `:model` option should contain a class that responds to `valid?` for the `using` validator') unless options[:model].respond_to?(:new) && options[:model].method_defined?(:valid?)
|
20
|
+
end
|
21
|
+
|
22
|
+
def should_not_continue?(value)
|
23
|
+
(options[:allow_nil] && value.nil?) ||
|
24
|
+
(options[:allow_empty] && value.empty?)
|
25
|
+
end
|
26
|
+
|
27
|
+
def perform(record, attribute, value)
|
28
|
+
model = options[:model].new(value)
|
29
|
+
|
30
|
+
return if model.valid?
|
31
|
+
|
32
|
+
reference_value = model[options[:reference]] if options[:reference]
|
33
|
+
|
34
|
+
if !reference_value.blank?
|
35
|
+
errors = {}
|
36
|
+
errors[reference_value] = model.errors
|
37
|
+
|
38
|
+
record.errors[attribute] << errors
|
39
|
+
else
|
40
|
+
record.errors[attribute] << model.errors
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/soulless/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soulless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anthony Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- lib/soulless/serialization.rb
|
114
114
|
- lib/soulless/validations.rb
|
115
115
|
- lib/soulless/validations/uniqueness_validator.rb
|
116
|
+
- lib/soulless/validations/using_validator.rb
|
116
117
|
- lib/soulless/version.rb
|
117
118
|
- soulless.gemspec
|
118
119
|
homepage: https://github.com/anthonator/soulless
|