validates_formatting_of 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md
CHANGED
data/README.markdown
CHANGED
@@ -22,6 +22,14 @@ Using validates_formatting_of is as simple as using Rails' built-in validation m
|
|
22
22
|
|
23
23
|
This call will ensure that the user-provided email is a valid email. This way, you will not need to find or write your own regex to validate. All of that logic is contained within `validates_formatting_of`
|
24
24
|
|
25
|
+
# Rails validation options still available
|
26
|
+
|
27
|
+
You can still add the following options when using `validates_formatting_of`:
|
28
|
+
|
29
|
+
* `:allow_nil`
|
30
|
+
* `:allow_blank`
|
31
|
+
* `:message` for custom validation messages
|
32
|
+
|
25
33
|
# Available Formatting Validations
|
26
34
|
|
27
35
|
`validates_formatting_of` has support for the following validations:
|
@@ -78,6 +86,7 @@ This call will ensure that the user-provided email is a valid email. This way, y
|
|
78
86
|
class User < ActiveRecord::Base
|
79
87
|
validates_formatting_of :ssn, :using => :ssn
|
80
88
|
end
|
89
|
+
|
81
90
|
# Customizable
|
82
91
|
|
83
92
|
If, for any reason, you want to use your own regex instead of Rail's built-in methods, you can specify what you want to use with the `:regex` option. For example,
|
@@ -13,9 +13,18 @@ module ValidatesFormattingOf
|
|
13
13
|
|
14
14
|
regex_for_validation = opts[:regex] || validate_with(opts[:using])
|
15
15
|
|
16
|
-
|
16
|
+
# Add :allow_nil and :allow_blank options. Both are false by default.
|
17
|
+
allow_nil = opts[:allow_nil] || false
|
18
|
+
allow_blank = opts[:allow_blank] || false
|
19
|
+
|
20
|
+
validates attribute, :format => { :with => regex_for_validation,
|
21
|
+
:message => opts[:message] },
|
22
|
+
:allow_nil => allow_nil,
|
23
|
+
:allow_blank => allow_blank
|
17
24
|
end
|
18
25
|
|
26
|
+
private
|
27
|
+
|
19
28
|
def validate_with(method)
|
20
29
|
# Actually retrieve the regex to check against
|
21
30
|
formatting.send(method)
|
@@ -124,4 +124,34 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
124
124
|
AnotherPerson.new(:ssn => "23498.7234").should_not be_valid
|
125
125
|
end
|
126
126
|
end
|
127
|
+
describe "custom messages" do
|
128
|
+
|
129
|
+
class Message < SuperModel::Base
|
130
|
+
validates_formatting_of :first_name, :using => :alpha, :message => "is not a valid first name"
|
131
|
+
end
|
132
|
+
|
133
|
+
it "are allowed and can be used in displaying error messages" do
|
134
|
+
message = Message.new(:first_name => "invalid-first-name-123")
|
135
|
+
message.should_not be_valid
|
136
|
+
message.errors.keys.class.should eq Array
|
137
|
+
message.errors.full_messages.first.should =~ /is not a valid first name/
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
# Currently, SuperModel's validations do not detect allow_blank or allow_nil
|
142
|
+
# This functionality has been tested separately in an empty Rails app with perfect
|
143
|
+
# results.
|
144
|
+
#
|
145
|
+
# describe "nil and blank values" do
|
146
|
+
# class People < SuperModel::Base
|
147
|
+
# validates_formatting_of :last_name, :using => :alpha, :allow_blank => true
|
148
|
+
# end
|
149
|
+
# it "are allowed" do
|
150
|
+
# p = People.new(:last_name => "something")
|
151
|
+
# p.should be_valid
|
152
|
+
# p.save!
|
153
|
+
# p.last_name = ""
|
154
|
+
# p.should_not be_valid
|
155
|
+
# end
|
156
|
+
# end
|
127
157
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_formatting_of
|
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:
|
@@ -13,7 +13,7 @@ date: 2011-11-30 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70315145764160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70315145764160
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70315145762460 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - =
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.9.2
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70315145762460
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70315145761260 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - =
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 2.7.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70315145761260
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: supermodel
|
49
|
-
requirement: &
|
49
|
+
requirement: &70315145760480 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - =
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 0.1.6
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70315145760480
|
58
58
|
description: Common Rails validations wrapped in a gem.
|
59
59
|
email:
|
60
60
|
- mbridges.91@gmail.com
|