activemodel-validators 1.2.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a3e3d6ba7a3262bbbe57a2c20e90a9a9bfcb19be
4
- data.tar.gz: 6ad2eb5ae67287dacc7b1c684dc70d2533dabcf9
2
+ SHA256:
3
+ metadata.gz: 961ba5817f1275b85ef77cd36078274940be5a204d283215f22c282873b6fc25
4
+ data.tar.gz: a4ba63270f369593bb3bf295b2a1db59ee5073b62d20d6ab4baa22a9787ca17e
5
5
  SHA512:
6
- metadata.gz: 840f7dff2d3b512b2f5b4471901c7ba2ddd42f4d2520d3e47a38779286c3bf9e4ec1a4e2ae0e460cec056747d45f07733c91135f3da3b27711af57677c430cc4
7
- data.tar.gz: 45e68afce515e74156fdf6ddd2643905e556c578149a8ec2d4efad8d0fab3eb11e861f3943ecb7b221bf84f2d8c6aa99f729e6dadcdd3ce097b88da813c9107e
6
+ metadata.gz: ecf522be4a8849c91c4f99010452884ff966e7e459fe286d3f40103159a26e2d8de1c7c7af138fcba502c49fa55665d37a0a47f20136c04e6077bd7863adf35a
7
+ data.tar.gz: c9e13fba3fce4ce0bed10092d60add8dba4fa2dcd3deb83f0e0aaeb2ecf6e2bc164ff498b87ca2f5b267c75138bc7b5f8495f81bb6daded210e31e812241a583
data/Readme.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ActiveModel Validators
2
2
  =======================
3
3
 
4
- A collection of Rails 3 ActiveModel Validators
4
+ A collection of Rails ActiveModel Validators
5
5
 
6
6
  Example Usage
7
7
  -------------
@@ -35,7 +35,6 @@ class Thing < ActiveRecord::Base
35
35
  validates :end_date, date: {required: true}
36
36
  validates :graduation_year, year: true
37
37
 
38
- validates :phone, phone: true
39
38
  validates :address_postal_code, postal_code: true
40
39
  validates :address_state, address_state: true
41
40
  end
@@ -59,7 +58,6 @@ Documentation
59
58
  * [DateValidator](activemodel-validators/blob/master/lib/activemodel-validators/date_validator.rb)
60
59
  * [YearValidator](activemodel-validators/blob/master/lib/activemodel-validators/year_validator.rb)
61
60
 
62
- * [PhoneValidator](activemodel-validators/blob/master/lib/activemodel-validators/phone_validator.rb)
63
61
  * [PostalCodeValidator](activemodel-validators/blob/master/lib/activemodel-validators/postal_code_validator.rb)
64
62
  * [AddressStateValidator](activemodel-validators/blob/master/lib/activemodel-validators/address_state_validator.rb)
65
63
 
@@ -117,6 +115,8 @@ Other collections not worth taking a look at:
117
115
  Specialized validators that stand on their own:
118
116
  -----------------------------------------------
119
117
 
118
+ * https://github.com/daddyz/phonelib (`phone: true`)
119
+ * https://github.com/perfectline/validates_url
120
120
  * https://github.com/balexand/email_validator
121
121
 
122
122
 
@@ -15,4 +15,3 @@ en:
15
15
 
16
16
  year_invalid: "is not a valid year (must be a number between %{min} and %{max})"
17
17
  date_invalid: "is not a valid date (must be in %{expected_format} format)"
18
- must_have_at_least_10_digits: "must have at least 10 digits"
@@ -73,7 +73,7 @@ module ActiveModel
73
73
  return if value.blank? or other_value.blank?
74
74
  unless value.send(operator, other_value)
75
75
  record.errors.add(attribute, message_key,
76
- options.merge(
76
+ **options.merge(
77
77
  value: value,
78
78
  operator: operator,
79
79
  operator_text: operator_text,
@@ -1,23 +1,27 @@
1
- class SumOfValidator < ActiveModel::EachValidator
2
- def check_validity!
3
- unless options[:attr_names]
4
- raise ArgumentError, "must supply :attr_names option"
5
- end
6
- end
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
- def validate_each(record, attribute, value)
9
- addends = options[:attr_names].map {|_| record[_] }
10
- return if value.blank? || addends.any?(&:blank?)
11
- sum = addends.sum
12
- unless value == sum
13
- record.errors.add(attribute, options[:message] || :sum_of,
14
- options.merge(
15
- attr_names: options[:attr_names].to_sentence,
16
- addends: addends.join(' + '),
17
- sum: sum,
18
- value: value,
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
@@ -1,5 +1,5 @@
1
1
  module Activemodel
2
2
  module Validators
3
- Version = "1.2.0"
3
+ Version = "3.0.1"
4
4
  end
5
5
  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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel-validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 3.0.1
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-10-22 00:00:00.000000000 Z
11
+ date: 2022-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -80,7 +80,6 @@ files:
80
80
  - lib/activemodel-validators/less_or_greater_than_validator.rb
81
81
  - lib/activemodel-validators/less_than_validator.rb
82
82
  - lib/activemodel-validators/multiple_of_validator.rb
83
- - lib/activemodel-validators/phone_validator.rb
84
83
  - lib/activemodel-validators/postal_code_validator.rb
85
84
  - lib/activemodel-validators/rails.rb
86
85
  - lib/activemodel-validators/restrict_to_validator.rb
@@ -91,6 +90,7 @@ files:
91
90
  - spec/date_validator_spec.rb
92
91
  - spec/less_or_greater_than_validator_spec.rb
93
92
  - spec/spec_helper.rb
93
+ - spec/sum_of_validator_spec.rb
94
94
  - spec/support/active_record.rb
95
95
  - spec/support/models/response.rb
96
96
  - spec/support/models/user.rb
@@ -113,8 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
- rubyforge_project: activemodel-validators
117
- rubygems_version: 2.2.2
116
+ rubygems_version: 3.2.22
118
117
  signing_key:
119
118
  specification_version: 4
120
119
  summary: Some reusable ActiveModel validations
@@ -1,8 +0,0 @@
1
- class PhoneValidator < ActiveModel::EachValidator
2
- def validate_each(record, attribute, value)
3
- return if value.blank?
4
- unless value.to_s.gsub(/[\s-]+/, '').length >= 10
5
- record.errors.add attribute, :must_have_at_least_10_digits
6
- end
7
- end
8
- end