validates_formatting_of 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  # This file does all of the preloading. Little to no actual code should go in this file.
2
2
 
3
3
  require "validates_formatting_of/version"
4
+ require "validates_formatting_of/validation_messages"
4
5
  require "validates_formatting_of/validating_methods"
5
6
  require "validates_formatting_of/model_additions"
6
7
  require "validates_formatting_of/railtie" if defined? Rails
@@ -1,24 +1,27 @@
1
1
  module ValidatesFormattingOf
2
2
  module ModelAdditions
3
- def validates_formatting_of(attribute, opts = {})
4
3
 
5
- # Here is where the input is formatted before it is validated.
6
- # Not yet implemented.
7
- #
8
- # unless opts[:preformat].nil?
9
- # before_validation do
10
- # # add formatting changes here
11
- # end
12
- # end
4
+ # Using validates_formatting_of is as simple as using Rails' built-in
5
+ # validation methods in models.
6
+ #
7
+ # class User < ActiveRecord::Base
8
+ # validates_formatting_of :email, :using => :email
9
+ # end
10
+ #
11
+ # This call will ensure that the user-provided email is a valid email. This way,
12
+ # you will not need to find or write your own regex to validate. All of that
13
+ # logic is contained within `validates_formatting_of`
14
+ def validates_formatting_of(attribute, opts = {})
13
15
 
14
16
  regex_for_validation = opts[:regex] || validate_with(opts[:using])
15
17
 
16
18
  # Add :allow_nil and :allow_blank options. Both are false by default.
17
19
  allow_nil = opts[:allow_nil] || false
18
20
  allow_blank = opts[:allow_blank] || false
21
+ validation_message = opts[:message] || ValidationMessages.message(opts[:using])
19
22
 
20
- validates attribute, :format => { :with => regex_for_validation,
21
- :message => opts[:message] },
23
+ validates attribute, :format => { :with => regex_for_validation,
24
+ :message => validation_message },
22
25
  :allow_nil => allow_nil,
23
26
  :allow_blank => allow_blank
24
27
  end
@@ -37,6 +37,7 @@ module ValidatesFormattingOf
37
37
  /^([\(]{1}[0-9]{3}[\)]{1}[ |\-]{0,1}|^[0-9]{3}[\-| ])?[0-9]{3}(\-| ){1}[0-9]{4}$/
38
38
  end
39
39
 
40
+ # IP Address validation
40
41
  def ip_address
41
42
  /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
42
43
  end
@@ -0,0 +1,30 @@
1
+ module ValidatesFormattingOf
2
+ module ValidationMessages
3
+
4
+ class Messages
5
+
6
+ def self.hash
7
+ {
8
+ :email => "is not a valid email",
9
+ :url => "is not a valid URL",
10
+ :alpha => "must be only letters or dashes",
11
+ :alphanum => "must be letters, numbers",
12
+ :credit_card => "is not a valid credit card number",
13
+ :us_zip => "is not a valid zipcode",
14
+ :us_phone => "is not a valid phone number",
15
+ :ip_address => "is not a valid IP address",
16
+ :ssn => "is not a valid social security number",
17
+ :hex_color => "is not a valid hex color"
18
+ }
19
+ end
20
+
21
+ end
22
+
23
+ extend self
24
+
25
+ def message(message)
26
+ Messages.hash[message.downcase.to_sym]
27
+ end
28
+
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module ValidatesFormattingOf
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -131,7 +131,7 @@ describe ValidatesFormattingOf::ModelAdditions do
131
131
  class Color < SuperModel::Base
132
132
  validates_formatting_of :color, :using => :hex_color
133
133
  end
134
- it "validates that the social security number provided is valid" do
134
+ it "validates that the hex color value provided is valid" do
135
135
  Color.new(:color => "efefef").should be_valid
136
136
  Color.new(:color => "98de89").should be_valid
137
137
  Color.new(:color => "000011").should be_valid
@@ -156,6 +156,23 @@ describe ValidatesFormattingOf::ModelAdditions do
156
156
  message.errors.full_messages.first.should =~ /is not a valid first name/
157
157
  end
158
158
 
159
+ end
160
+ describe "default error messages" do
161
+
162
+ class Problems < SuperModel::Base
163
+ validates_formatting_of :name, :using => :alpha
164
+ end
165
+
166
+ it "set a default error" do
167
+ problems = Problems.new(:name => "sdfs12312dfsd")
168
+ problems.should_not be_valid
169
+ problems.errors.full_messages.first.should =~ /letters/i
170
+
171
+ email = Email.new(:email => "fake@email.address")
172
+ email.should_not be_valid
173
+ email.errors.full_messages.first.should =~ /email/i
174
+ end
175
+
159
176
  end
160
177
  # Currently, SuperModel's validations do not detect allow_blank or allow_nil
161
178
  # This functionality has been tested separately in an empty Rails app with perfect
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.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-05 00:00:00.000000000 Z
12
+ date: 2011-12-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70102167212440 !ruby/object:Gem::Requirement
16
+ requirement: &70245176231800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70102167212440
24
+ version_requirements: *70245176231800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70102167210840 !ruby/object:Gem::Requirement
27
+ requirement: &70245176230180 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70102167210840
35
+ version_requirements: *70245176230180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70102167209720 !ruby/object:Gem::Requirement
38
+ requirement: &70245176228780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70102167209720
46
+ version_requirements: *70245176228780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: supermodel
49
- requirement: &70102167208440 !ruby/object:Gem::Requirement
49
+ requirement: &70245176228020 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70102167208440
57
+ version_requirements: *70245176228020
58
58
  description: Common Rails validations wrapped in a gem.
59
59
  email:
60
60
  - mbridges.91@gmail.com
@@ -75,6 +75,7 @@ files:
75
75
  - lib/validates_formatting_of/model_additions.rb
76
76
  - lib/validates_formatting_of/railtie.rb
77
77
  - lib/validates_formatting_of/validating_methods.rb
78
+ - lib/validates_formatting_of/validation_messages.rb
78
79
  - lib/validates_formatting_of/version.rb
79
80
  - spec/spec_helper.rb
80
81
  - spec/validates_formatting_of/model_additions_spec.rb