optional_validations 0.1.1 → 0.1.2
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/optional_validations.rb +2 -0
- data/test/test_optional_validations.rb +26 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d56a776847cfdbd903416d9a9e2c09a3dcd3e554
|
4
|
+
data.tar.gz: 2d091783eabfd36132fd4c36a56d34e18ddb4e9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7adbd0532fd5f390381280cd2f04cd0dfa8845a45fc7278ac3dbb940557d77c5b6751c878c3ae2af1b12120e5b707838f0a65c0b3ed7d994865b7b18defe534
|
7
|
+
data.tar.gz: f1d7cc3dd457bf447524c927f344fa035ad320de6ce1003d9e856af4ffc6d6d77cc35dd4a0a1771f10c18854fd67116c3063c28dd4ca27774a7c627a783294a7
|
data/lib/optional_validations.rb
CHANGED
@@ -8,6 +8,8 @@ module ActiveModel
|
|
8
8
|
alias_method :__validates_with, :validates_with
|
9
9
|
def validates_with(*args, &block)
|
10
10
|
|
11
|
+
return __validates_with(*args, &block) unless args[1].present?
|
12
|
+
|
11
13
|
_if = Array(args[1][:if])
|
12
14
|
|
13
15
|
# if multiple attribute names are supplied we need to split them into separate validates_with calls
|
@@ -205,5 +205,31 @@ class OptionalValidationsTest < Minitest::Test
|
|
205
205
|
|
206
206
|
end
|
207
207
|
|
208
|
+
def test_validates_with
|
209
|
+
model = Class.new(ModelBase) do
|
210
|
+
validates_with(Class.new(ActiveModel::Validator) do
|
211
|
+
def validate(record)
|
212
|
+
record.errors.add :base, 'attr1 should be above zero' unless record.attr1 > 0
|
213
|
+
end
|
214
|
+
end)
|
215
|
+
validates_presence_of :attr2
|
216
|
+
end
|
217
|
+
|
218
|
+
m = model.new
|
219
|
+
m.attr1 = 0
|
220
|
+
|
221
|
+
m.validate_only :attr1 do
|
222
|
+
assert !m.valid?
|
223
|
+
end
|
224
|
+
|
225
|
+
m.attr1 = 1
|
226
|
+
|
227
|
+
m.validate_only :attr1 do
|
228
|
+
assert m.valid?
|
229
|
+
end
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
|
208
234
|
|
209
235
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optional_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Brykov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|