can_has_validations 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -6,6 +6,7 @@ ActiveModel.
|
|
6
6
|
Validations provided:
|
7
7
|
|
8
8
|
* Email
|
9
|
+
* Existence
|
9
10
|
* Grandparent
|
10
11
|
* Ordering
|
11
12
|
* URL
|
@@ -13,7 +14,7 @@ Validations provided:
|
|
13
14
|
|
14
15
|
All validators use the newer Rails 3 syntax:
|
15
16
|
|
16
|
-
validates :some_attribute, :
|
17
|
+
validates :some_attribute, email: true
|
17
18
|
|
18
19
|
(That is, there's not a validates_email_of :some_attribute helper.)
|
19
20
|
|
@@ -33,14 +34,30 @@ Ensures an attribute is generally formatted as an email. It uses a basic regex
|
|
33
34
|
that's designed to match something that looks like an email. It allows for any
|
34
35
|
TLD, so as to not fail as ICANN continues to add TLDs.
|
35
36
|
|
36
|
-
validates :user_email, :
|
37
|
+
validates :user_email, email: true
|
38
|
+
|
39
|
+
|
40
|
+
## Existence validator ##
|
41
|
+
|
42
|
+
Rails 4 changed the default behavior of the Presence validator. In Rails 3.x,
|
43
|
+
it always validated presence, even if `allow_nil: true` or `allow_blank: true`
|
44
|
+
was set. The Rails 4 Presence validator now acts on `allow_nil` and
|
45
|
+
`allow_blank`, which makes it semi-useless.
|
46
|
+
|
47
|
+
The Existence validator restores the previous behavior (but with a new name to
|
48
|
+
avoid any potential conflicts).
|
49
|
+
|
50
|
+
Mongoid 3 and 4 also exhibit the same behavior as Rails 4, even under Rails 3,
|
51
|
+
so this is useful with Mongoid as well.
|
52
|
+
|
53
|
+
validates :name, presence: true
|
37
54
|
|
38
55
|
|
39
56
|
## Grandparent validator ##
|
40
57
|
|
41
58
|
Ensures two (or more) associations share a common parent value.
|
42
59
|
|
43
|
-
|
60
|
+
`allow_nil: true` will not only allow the attribute/association to be nil, but
|
44
61
|
also any of the `:scope` values.
|
45
62
|
|
46
63
|
Consider a model tree like this:
|
@@ -64,7 +81,7 @@ Consider a model tree like this:
|
|
64
81
|
belongs_to :address
|
65
82
|
belongs_to :phone
|
66
83
|
|
67
|
-
validates :phone, :
|
84
|
+
validates :phone, grandparent: {scope: :address, parent: :user}
|
68
85
|
end
|
69
86
|
|
70
87
|
For any `Order`, this ensures that both `:address` and `:phone` belong to the same
|
@@ -72,7 +89,7 @@ For any `Order`, this ensures that both `:address` and `:phone` belong to the sa
|
|
72
89
|
|
73
90
|
Basically it starts with the attribute being validated (`:phone` in this case)
|
74
91
|
and the scoped attributes (just `:address` in this case, but you can supply an
|
75
|
-
array if needed, eg:
|
92
|
+
array if needed, eg: `scope: [:billing_address, :mailing_address]` ).
|
76
93
|
|
77
94
|
Then, it looks for the attribute that is the common parent (`:user` in the above
|
78
95
|
example). So, it's looking for `phone.user` and `address.user`.
|
@@ -95,13 +112,13 @@ either `:before` or `:after` to make them readable.
|
|
95
112
|
Always skips over nil values; use `:presence` to validate those.
|
96
113
|
|
97
114
|
# Short versions:
|
98
|
-
validates :start_at, :
|
99
|
-
validates :finish_at, :
|
100
|
-
validates :start_at, :
|
115
|
+
validates :start_at, before: :finish_at
|
116
|
+
validates :finish_at, after: [:start_at, :alt_start_at]
|
117
|
+
validates :start_at, presence: true, before: :finish_at
|
101
118
|
|
102
119
|
# Long versions, if you need to add extra validation options:
|
103
|
-
validates :start_at, :
|
104
|
-
validates :finish_at, :
|
120
|
+
validates :start_at, before: {value_of: :finish_at, message: "..." }
|
121
|
+
validates :finish_at, after: {values_of: [:start_at, :alt_start_at], if: ... }
|
105
122
|
|
106
123
|
|
107
124
|
## URL validator ##
|
@@ -109,14 +126,14 @@ Always skips over nil values; use `:presence` to validate those.
|
|
109
126
|
Ensure an attribute is generally formatted as a URL. If `addressable/uri` is
|
110
127
|
already loaded, it will be used to parse IDN's.
|
111
128
|
|
112
|
-
validates :website, :
|
129
|
+
validates :website, url: true
|
113
130
|
|
114
131
|
# With IDN parsing:
|
115
132
|
require 'addressable/uri'
|
116
|
-
validates :website, :
|
133
|
+
validates :website, url: true
|
117
134
|
|
118
135
|
# Or, as part of your Gemfile:
|
119
|
-
gem 'addressable', :
|
136
|
+
gem 'addressable', require: 'addressable/uri'
|
120
137
|
gem 'can_has_validations'
|
121
138
|
|
122
139
|
|
@@ -129,14 +146,14 @@ The first is as an equivalent to `attr_readonly :user_id` except that it also
|
|
129
146
|
produces a validation error instead of silently ignoring the change as
|
130
147
|
`attr_readonly` does.
|
131
148
|
|
132
|
-
validates :user_id, :
|
149
|
+
validates :user_id, presence: true, write_once: true
|
133
150
|
|
134
151
|
The second use is to allow an attribute to be nil when the record is first
|
135
152
|
created and allow it to be set once at some arbitrary point in the future, but
|
136
153
|
once set, still make it immutable. A WORM (write once, read many) attribute of
|
137
154
|
sorts.
|
138
155
|
|
139
|
-
validates :user_id, :
|
156
|
+
validates :user_id, write_once: true
|
140
157
|
|
141
158
|
|
142
159
|
## Error messages
|
data/lib/can_has_validations.rb
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
module ActiveModel::Validations
|
5
5
|
class EmailValidator < ActiveModel::EachValidator
|
6
6
|
|
7
|
-
EMAIL_REGEXP = /\A([a-z0-9._+-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\
|
7
|
+
EMAIL_REGEXP = /\A([a-z0-9._+-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
8
8
|
|
9
9
|
def validate_each(record, attribute, value)
|
10
10
|
unless value =~ EMAIL_REGEXP
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Just like `presence` except that it properly ignores allow_nil / allow_blank.
|
2
|
+
# This is how Rails 3.2 worked, but was changed in Rails 4. Mongoid 3 and 4 both
|
3
|
+
# act like Rails 4, so this is useful there too.
|
4
|
+
|
5
|
+
module ActiveModel::Validations
|
6
|
+
class ExistenceValidator < ActiveModel::Validations::PresenceValidator
|
7
|
+
def validate(record)
|
8
|
+
attributes.each do |attribute|
|
9
|
+
value = record.read_attribute_for_validation(attribute)
|
10
|
+
validate_each(record, attribute, value)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_has_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 3.
|
21
|
+
version: '3.0'
|
22
22
|
- - <
|
23
23
|
- !ruby/object:Gem::Version
|
24
24
|
version: '5.0'
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 3.
|
32
|
+
version: '3.0'
|
33
33
|
- - <
|
34
34
|
- !ruby/object:Gem::Version
|
35
35
|
version: '5.0'
|
@@ -49,7 +49,7 @@ dependencies:
|
|
49
49
|
- - ! '>='
|
50
50
|
- !ruby/object:Gem::Version
|
51
51
|
version: '0'
|
52
|
-
description: Assorted Rails 3 validators.
|
52
|
+
description: Assorted Rails 3.x-4.x validators.
|
53
53
|
email:
|
54
54
|
- tm@iprog.com
|
55
55
|
executables: []
|
@@ -58,6 +58,7 @@ extra_rdoc_files: []
|
|
58
58
|
files:
|
59
59
|
- lib/can_has_validations/locale/en.yml
|
60
60
|
- lib/can_has_validations/validators/email_validator.rb
|
61
|
+
- lib/can_has_validations/validators/existence_validator.rb
|
61
62
|
- lib/can_has_validations/validators/grandparent_validator.rb
|
62
63
|
- lib/can_has_validations/validators/ordering_validator.rb
|
63
64
|
- lib/can_has_validations/validators/safe_uniqueness_validator.rb
|
@@ -113,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
113
114
|
version: '0'
|
114
115
|
segments:
|
115
116
|
- 0
|
116
|
-
hash:
|
117
|
+
hash: 3102458666723632577
|
117
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
119
|
none: false
|
119
120
|
requirements:
|
@@ -122,13 +123,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
123
|
version: '0'
|
123
124
|
segments:
|
124
125
|
- 0
|
125
|
-
hash:
|
126
|
+
hash: 3102458666723632577
|
126
127
|
requirements: []
|
127
128
|
rubyforge_project:
|
128
|
-
rubygems_version: 1.8.
|
129
|
+
rubygems_version: 1.8.25
|
129
130
|
signing_key:
|
130
131
|
specification_version: 3
|
131
|
-
summary: Assorted Rails 3 validators
|
132
|
+
summary: Assorted Rails 3.x-4.x validators
|
132
133
|
test_files:
|
133
134
|
- test/can_has_validations_test.rb
|
134
135
|
- test/dummy/app/assets/javascripts/application.js
|