activemodel-validators 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/activemodel-validators/sum_of_validator.rb +23 -19
- data/lib/activemodel-validators/version.rb +1 -1
- data/spec/sum_of_validator_spec.rb +61 -0
- metadata +14 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdecd79a81ab24c7e838e1ee0e8983bfaf901b07
|
4
|
+
data.tar.gz: 67a9b577cbc12f1c29fc624527ef4a193bfd05fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c9bb9ef4739163b3c22ac664d3856208112996fc80ca8a684b6d7f9b5479c52f5d8f9c15f1dfc8bb76a2565a8ad8abde745c284055e30ebecaa184096ab5ae3
|
7
|
+
data.tar.gz: e428717cf8afffd6f87c2d377d364bfed90aed8c26f2a4de8a896e461e25e49229b806df5f48899d34cde1597313c75ec46338bf3a60c93c6de865040588558f
|
@@ -1,23 +1,27 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class SumOfValidator < ActiveModel::EachValidator
|
4
|
+
def check_validity!
|
5
|
+
unless options[:attr_names]
|
6
|
+
raise ArgumentError, "must supply :attr_names option"
|
7
|
+
end
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
10
|
+
def validate_each(record, attribute, value)
|
11
|
+
addends = options[:attr_names].map {|_| record[_] }
|
12
|
+
return if value.blank?
|
13
|
+
sum = addends.reject(&:blank?).sum
|
14
|
+
unless value == sum
|
15
|
+
record.errors.add(attribute, options[:message] || :sum_of,
|
16
|
+
options.merge(
|
17
|
+
attr_names: options[:attr_names].to_sentence,
|
18
|
+
addends: addends.join(' + '),
|
19
|
+
sum: sum,
|
20
|
+
value: value,
|
21
|
+
)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
ActiveModel::Validations::SumOfValidator
|
4
|
+
class ActiveModel::Validations::SumOfValidator
|
5
|
+
|
6
|
+
class TestModel < Struct.new(:group_1_count, :group_2_count, :total_count)
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
validates :total_count, sum_of: {attr_names: [:group_1_count, :group_2_count] }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe TestModel do
|
13
|
+
before { subject.group_1_count = 1 }
|
14
|
+
before { subject.group_2_count = 2 }
|
15
|
+
before { subject.total_count = 3 }
|
16
|
+
|
17
|
+
context 'when group_1_count + group_2_count == total_count' do
|
18
|
+
before { subject.valid? }
|
19
|
+
its('errors.messages') { should == {} }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when group_1_count + group_2_count != total_count' do
|
23
|
+
before { subject.group_2_count = 1 }
|
24
|
+
before { subject.valid? }
|
25
|
+
its('errors.messages') { should == {total_count: ["must be the sum of group_1_count and group_2_count (2) but was 3"]} }
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when total_count is blank' do
|
29
|
+
before { subject.total_count = '' }
|
30
|
+
before { subject.valid? }
|
31
|
+
its('errors.messages') { should == {} }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when group_1_count and group_2_count are blank' do
|
35
|
+
before { subject.group_1_count = nil }
|
36
|
+
before { subject.group_2_count = '' }
|
37
|
+
before { subject.valid? }
|
38
|
+
its('errors.messages') { should == {total_count: ["must be the sum of group_1_count and group_2_count (0) but was 3"]} }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when group_2_count is blank' do
|
42
|
+
before { subject.group_2_count = '' }
|
43
|
+
before { subject.valid? }
|
44
|
+
its('errors.messages') { should == {total_count: ["must be the sum of group_1_count and group_2_count (1) but was 3"]} }
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when group_1_count is a string and group_2_count is a fixnum' do
|
48
|
+
before { subject.group_1_count = '1' }
|
49
|
+
before { subject.group_2_count = 2 }
|
50
|
+
it { expect { subject.valid? }.to raise_exception 'no implicit conversion of Fixnum into String' }
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when group_1_count is a fixnum and group_2_count is a string' do
|
54
|
+
before { subject.group_1_count = 1 }
|
55
|
+
before { subject.group_2_count = '2' }
|
56
|
+
it { expect { subject.valid? }.to raise_exception "String can't be coerced into Fixnum" }
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activemodel-validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Rick
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sqlite3
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: 'Some reusable ActiveModel validations, including greater_than, boolean_presence,
|
@@ -60,8 +60,8 @@ executables: []
|
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
-
-
|
64
|
-
-
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
65
65
|
- Gemfile
|
66
66
|
- Rakefile
|
67
67
|
- Readme.md
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- spec/date_validator_spec.rb
|
92
92
|
- spec/less_or_greater_than_validator_spec.rb
|
93
93
|
- spec/spec_helper.rb
|
94
|
+
- spec/sum_of_validator_spec.rb
|
94
95
|
- spec/support/active_record.rb
|
95
96
|
- spec/support/models/response.rb
|
96
97
|
- spec/support/models/user.rb
|
@@ -104,17 +105,17 @@ require_paths:
|
|
104
105
|
- lib
|
105
106
|
required_ruby_version: !ruby/object:Gem::Requirement
|
106
107
|
requirements:
|
107
|
-
- -
|
108
|
+
- - '>='
|
108
109
|
- !ruby/object:Gem::Version
|
109
110
|
version: '0'
|
110
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
112
|
requirements:
|
112
|
-
- -
|
113
|
+
- - '>='
|
113
114
|
- !ruby/object:Gem::Version
|
114
115
|
version: '0'
|
115
116
|
requirements: []
|
116
117
|
rubyforge_project: activemodel-validators
|
117
|
-
rubygems_version: 2.
|
118
|
+
rubygems_version: 2.1.5
|
118
119
|
signing_key:
|
119
120
|
specification_version: 4
|
120
121
|
summary: Some reusable ActiveModel validations
|