context_validations 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +1 -1
- data/README.md +6 -0
- data/lib/context_validations/model.rb +19 -1
- data/lib/context_validations/version.rb +1 -1
- data/test/model_test.rb +17 -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: 73cdca917a7c501806a934a2a078dee76089ce0e
|
4
|
+
data.tar.gz: 28a7ab80f6cadfc68e930a3c605f8d5228c08fef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0178c54af6220b49ce4cd14366827130c6dcad234a2bb2f44af889224b14ae55adcdf29c697d25820ec4808f93a12b232f23de5d81a664bf461484a645c6da2
|
7
|
+
data.tar.gz: e05af07a17649aef1991cfc013851f8a945e0d7ef1093d464c7a898a6ad233488fbd083a15ad118b6759fa27602de97749528a77c480cb8b19e300c16a1e8845
|
data/CONTRIBUTING.md
CHANGED
@@ -16,7 +16,7 @@ If you'd like to submit a pull request please adhere to the following:
|
|
16
16
|
4. Single-quotes instead of double-quotes unless you are using string
|
17
17
|
interpolation or escapes.
|
18
18
|
5. General Rails/Ruby naming conventions for files and classes
|
19
|
-
6. *Do not* use Ruby 1.9
|
19
|
+
6. *Do not* use Ruby 1.9 stabby proc syntax
|
20
20
|
|
21
21
|
Please note that you must adhere to each of the above mentioned rules.
|
22
22
|
Failure to do so will result in an immediate closing of the pull
|
data/README.md
CHANGED
@@ -137,6 +137,12 @@ We highly recommend using [ValidAttribute](https://github.com/bcardarella/valid_
|
|
137
137
|
|
138
138
|
### MiniTest ###
|
139
139
|
|
140
|
+
In `/test/test_helper.rb`:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
require 'context_validations/minitest'
|
144
|
+
```
|
145
|
+
|
140
146
|
You are given access to a `#validations_for(:action_name)` method. You should pass the action in your
|
141
147
|
controller that is the context of the validations and use the `#validations=` setter on the model.
|
142
148
|
|
@@ -32,7 +32,25 @@ module ContextValidations::Model
|
|
32
32
|
if validator.respond_to?(:setup)
|
33
33
|
validator.setup(self.class)
|
34
34
|
end
|
35
|
-
validator.
|
35
|
+
if validator.options[:if]
|
36
|
+
if validator.options[:if].respond_to?(:call)
|
37
|
+
if validator.options[:if].call(self)
|
38
|
+
validator.validate(self)
|
39
|
+
end
|
40
|
+
elsif self.send(validator.options[:if])
|
41
|
+
validator.validate(self)
|
42
|
+
end
|
43
|
+
elsif validator.options[:unless]
|
44
|
+
if validator.options[:unless].respond_to?(:call)
|
45
|
+
if !validator.options[:unless].call(self)
|
46
|
+
validator.validate(self)
|
47
|
+
end
|
48
|
+
elsif !self.send(validator.options[:unless])
|
49
|
+
validator.validate(self)
|
50
|
+
end
|
51
|
+
else
|
52
|
+
validator.validate(self)
|
53
|
+
end
|
36
54
|
end
|
37
55
|
errors.empty?
|
38
56
|
end
|
data/test/model_test.rb
CHANGED
@@ -26,4 +26,21 @@ describe 'Model' do
|
|
26
26
|
@user.valid?.must_equal false
|
27
27
|
@user.errors.count.must_equal 2
|
28
28
|
end
|
29
|
+
|
30
|
+
it 'respect conditional validations set onto the instance' do
|
31
|
+
validations = [
|
32
|
+
ActiveModel::Validations::PresenceValidator.new(:attributes => [:first_name], :if => :can_validate?),
|
33
|
+
ActiveModel::Validations::PresenceValidator.new(:attributes => [:first_name], :if => Proc.new { |model| model.can_validate? }),
|
34
|
+
ActiveModel::Validations::PresenceValidator.new(:attributes => [:first_name], :unless => :cannot_validate?),
|
35
|
+
ActiveModel::Validations::PresenceValidator.new(:attributes => [:first_name], :unless => Proc.new { |model| model.cannot_validate? })
|
36
|
+
]
|
37
|
+
def @user.can_validate?
|
38
|
+
false
|
39
|
+
end
|
40
|
+
def @user.cannot_validate?
|
41
|
+
true
|
42
|
+
end
|
43
|
+
@user.validations = validations
|
44
|
+
@user.valid?.must_equal true
|
45
|
+
end
|
29
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: context_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Cardarella
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|