activemodel-validators 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Readme.md +5 -2
- data/lib/activemodel-validators/equal_to_validator.rb +16 -0
- data/lib/activemodel-validators/less_or_greater_than_validator.rb +16 -2
- data/lib/activemodel-validators/version.rb +1 -1
- data/spec/less_or_greater_than_validator_spec.rb +3 -0
- data/spec/support/models/response.rb +2 -1
- 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: a3e3d6ba7a3262bbbe57a2c20e90a9a9bfcb19be
|
4
|
+
data.tar.gz: 6ad2eb5ae67287dacc7b1c684dc70d2533dabcf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 840f7dff2d3b512b2f5b4471901c7ba2ddd42f4d2520d3e47a38779286c3bf9e4ec1a4e2ae0e460cec056747d45f07733c91135f3da3b27711af57677c430cc4
|
7
|
+
data.tar.gz: 45e68afce515e74156fdf6ddd2643905e556c578149a8ec2d4efad8d0fab3eb11e861f3943ecb7b221bf84f2d8c6aa99f729e6dadcdd3ce097b88da813c9107e
|
data/Readme.md
CHANGED
@@ -10,11 +10,14 @@ Example Usage
|
|
10
10
|
class Thing < ActiveRecord::Base
|
11
11
|
validates :birthdate, greater_than: { value: Date.new(1900), message: 'must be later than 1900' }
|
12
12
|
validates :birthdate, less_than: { value: Time.zone.now.years_ago(1).to_date+1, message: 'must be at least 1 year ago' }
|
13
|
-
validates :end_date, greater_than: {attr: :begin_date, operator_text: 'later than'}
|
13
|
+
validates :end_date, greater_than: { attr: :begin_date, operator_text: 'later than' }
|
14
14
|
|
15
|
-
# shortcut for greater_than: { value: 1, operator:
|
15
|
+
# shortcut for greater_than: { value: 1, operator: :>= }
|
16
16
|
validates :how_many_pies, at_least: { value: 1 }
|
17
17
|
|
18
|
+
# shortcut for less_or_greater_than: { attr: :how_many_people, operator: :== }
|
19
|
+
validates :how_many_gifts, equal_to: { attr: :how_many_people }
|
20
|
+
|
18
21
|
# Because presence: true doesn't work for boolean attributes!
|
19
22
|
validates :is_18_or_older, boolean_presence: true
|
20
23
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'activemodel-validators/less_or_greater_than_validator'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
class EqualToValidator < LessOrGreaterThanValidator
|
6
|
+
protected
|
7
|
+
def default_operator
|
8
|
+
:==
|
9
|
+
end
|
10
|
+
|
11
|
+
def allowed_operators
|
12
|
+
[:==]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -17,6 +17,10 @@ module ActiveModel
|
|
17
17
|
raise NotImplementedError, 'must be defined in subclass'
|
18
18
|
end
|
19
19
|
|
20
|
+
def allowed_operators
|
21
|
+
[:<, :<=, :>, :>=, :==]
|
22
|
+
end
|
23
|
+
|
20
24
|
def operator
|
21
25
|
options[:operator] || default_operator
|
22
26
|
end
|
@@ -24,7 +28,7 @@ module ActiveModel
|
|
24
28
|
# Defaults to the standard mathematical name for these operators, but you're free to override with
|
25
29
|
# something that makes more sense ('later than' might make more sense for dates, for example)
|
26
30
|
def operator_text
|
27
|
-
options[:operator_text] ||
|
31
|
+
options[:operator_text] ||
|
28
32
|
{
|
29
33
|
:< => 'less than',
|
30
34
|
:<= => 'less than or equal to',
|
@@ -49,9 +53,19 @@ module ActiveModel
|
|
49
53
|
!!options[:attr]
|
50
54
|
end
|
51
55
|
|
56
|
+
def human_attr_name
|
57
|
+
return unless options[:attr]
|
58
|
+
if defined?(ActiveRecord::Base)
|
59
|
+
ActiveRecord::Base.human_attribute_name(options[:attr])
|
60
|
+
else
|
61
|
+
options[:attr]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
52
65
|
def message_key
|
53
66
|
comparing_to_attr? ? :less_or_greater_than_attr : :less_or_greater_than
|
54
67
|
end
|
68
|
+
|
55
69
|
public
|
56
70
|
|
57
71
|
def validate_each(record, attribute, value)
|
@@ -63,7 +77,7 @@ module ActiveModel
|
|
63
77
|
value: value,
|
64
78
|
operator: operator,
|
65
79
|
operator_text: operator_text,
|
66
|
-
attr_name:
|
80
|
+
attr_name: human_attr_name,
|
67
81
|
other_value: other_value,
|
68
82
|
)
|
69
83
|
)
|
@@ -10,4 +10,7 @@ describe Response do
|
|
10
10
|
|
11
11
|
it { should_not allow_value(0).for(:how_many_pies). with_message('must be at least 1') }
|
12
12
|
it { should allow_value(1).for(:how_many_pies) }
|
13
|
+
|
14
|
+
it { subject.how_many_people = 5
|
15
|
+
should_not allow_value(0).for(:how_many_gifts). with_message('must be equal to How many people (5)') }
|
13
16
|
end
|
@@ -1,8 +1,9 @@
|
|
1
|
-
class Response < Struct.new(:how_many_people, :how_many_pies)
|
1
|
+
class Response < Struct.new(:how_many_people, :how_many_gifts, :how_many_pies)
|
2
2
|
#extend ActiveModel::Naming
|
3
3
|
#include ActiveModel::Conversion
|
4
4
|
include ActiveModel::Validations
|
5
5
|
|
6
6
|
validates :how_many_people, greater_than: { value: 1, operator: :>= }, allow_blank: true
|
7
|
+
validates :how_many_gifts, equal_to: {attr: :how_many_people}, allow_blank: true
|
7
8
|
validates :how_many_pies, at_least: { value: 1 }, allow_blank: true
|
8
9
|
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: 1.
|
4
|
+
version: 1.2.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:
|
11
|
+
date: 2014-10-22 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
|
-
- .gitignore
|
64
|
-
- .rspec
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
65
|
- Gemfile
|
66
66
|
- Rakefile
|
67
67
|
- Readme.md
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- lib/activemodel-validators/blank_validator.rb
|
76
76
|
- lib/activemodel-validators/boolean_presence_validator.rb
|
77
77
|
- lib/activemodel-validators/date_validator.rb
|
78
|
+
- lib/activemodel-validators/equal_to_validator.rb
|
78
79
|
- lib/activemodel-validators/greater_than_validator.rb
|
79
80
|
- lib/activemodel-validators/less_or_greater_than_validator.rb
|
80
81
|
- lib/activemodel-validators/less_than_validator.rb
|
@@ -103,17 +104,17 @@ require_paths:
|
|
103
104
|
- lib
|
104
105
|
required_ruby_version: !ruby/object:Gem::Requirement
|
105
106
|
requirements:
|
106
|
-
- -
|
107
|
+
- - ">="
|
107
108
|
- !ruby/object:Gem::Version
|
108
109
|
version: '0'
|
109
110
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
111
|
requirements:
|
111
|
-
- -
|
112
|
+
- - ">="
|
112
113
|
- !ruby/object:Gem::Version
|
113
114
|
version: '0'
|
114
115
|
requirements: []
|
115
116
|
rubyforge_project: activemodel-validators
|
116
|
-
rubygems_version: 2.
|
117
|
+
rubygems_version: 2.2.2
|
117
118
|
signing_key:
|
118
119
|
specification_version: 4
|
119
120
|
summary: Some reusable ActiveModel validations
|