whyvalidationssuckin96 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.0
1
+ 1.3.1
@@ -0,0 +1,28 @@
1
+ require 'whyvalidationssuckin96/skippable_validation'
2
+ require 'whyvalidationssuckin96/attribute_based_validation'
3
+ require 'whyvalidationssuckin96/vendor/rfc822'
4
+
5
+ module WhyValidationsSuckIn96
6
+
7
+ # Checks whether a given attribute is a valid email address
8
+ #
9
+ # @example Default usage
10
+ # setup_validations do
11
+ # validates_as_email :email
12
+ # end
13
+ #
14
+ class ValidatesEmail < Validation
15
+ include WhyValidationsSuckIn96::SkippableValidation
16
+ include WhyValidationsSuckIn96::AttributeBasedValidation
17
+
18
+ DefaultOptions = {:message => "is not a valid email address"}
19
+
20
+ def validate
21
+ super
22
+ WhyValidationsSuckIn96::RFC822::EmailAddress.match(attribute_value) ? pass : fail
23
+ end
24
+
25
+ end # Validation
26
+
27
+ ValidationBuilder.register_macro :validates_as_email, WhyValidationsSuckIn96::ValidatesEmail
28
+ end # WhyValidationsSuckIn96
@@ -7,4 +7,5 @@ require 'whyvalidationssuckin96/macros/validates_inclusion'
7
7
  require 'whyvalidationssuckin96/macros/validates_length'
8
8
  require 'whyvalidationssuckin96/macros/validates_numericality'
9
9
  require 'whyvalidationssuckin96/macros/validates_presence'
10
- require 'whyvalidationssuckin96/macros/validates_url'
10
+ require 'whyvalidationssuckin96/macros/validates_url'
11
+ require 'whyvalidationssuckin96/macros/validates_email'
@@ -61,9 +61,14 @@ module WhyValidationsSuckIn96
61
61
  def scope_columns
62
62
  Array(options[:scope])
63
63
  end
64
-
64
+
65
65
  def find_conditions
66
- ["LOWER(#{scope_class.connection.quote_column_name(attribute.to_s)}) = LOWER(?)", attribute_value]
66
+ column = scope_class.columns.detect { |col| col.name == attribute.to_s }
67
+ if column.type == :text or column.type == :string
68
+ ["LOWER(#{scope_class.connection.quote_column_name(attribute.to_s)}) = LOWER(?)", attribute_value]
69
+ else
70
+ ["#{scope_class.connection.quote_column_name(attribute.to_s)} = ?", attribute_value]
71
+ end
67
72
  end
68
73
 
69
74
  def scope_conditions
@@ -84,4 +89,4 @@ module WhyValidationsSuckIn96
84
89
  end # Validation
85
90
 
86
91
  ValidationBuilder.register_macro :validates_uniqueness_of, WhyValidationsSuckIn96::ValidatesUniqueness
87
- end # WhyValidationsSuckIn96
92
+ end # WhyValidationsSuckIn96
@@ -0,0 +1,32 @@
1
+ #
2
+ # RFC822 Email Address Regex
3
+ # --------------------------
4
+ #
5
+ # Originally written by Cal Henderson
6
+ # c.f. http://iamcal.com/publish/articles/php/parsing_email/
7
+ #
8
+ # Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
9
+ #
10
+ # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
11
+ # http://creativecommons.org/licenses/by-sa/2.5/
12
+ #
13
+ module WhyValidationsSuckIn96
14
+ module RFC822
15
+ EmailAddress = begin
16
+ qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
17
+ dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
18
+ atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
19
+ '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
20
+ quoted_pair = '\\x5c[\\x00-\\x7f]'
21
+ domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
22
+ quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
23
+ domain_ref = atom
24
+ sub_domain = "(?:#{domain_ref}|#{domain_literal})"
25
+ word = "(?:#{atom}|#{quoted_string})"
26
+ domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
27
+ local_part = "#{word}(?:\\x2e#{word})*"
28
+ addr_spec = "#{local_part}\\x40#{domain}"
29
+ pattern = /\A#{addr_spec}\z/
30
+ end
31
+ end
32
+ end # WhyValidationsSuckIn96
@@ -0,0 +1,46 @@
1
+ require 'teststrap'
2
+
3
+ context "validates email" do
4
+
5
+ should "add a validation macro" do
6
+ WhyValidationsSuckIn96::ValidationBuilder.instance_methods
7
+ end.includes('validates_as_email')
8
+
9
+ context "with some default options" do
10
+ setup do
11
+ WhyValidationsSuckIn96::ValidatesEmail.new(Object.new, :attribute => :email)
12
+ end
13
+
14
+ should "have a message accessor with a default message" do
15
+ topic.message
16
+ end.equals("is not a valid email address")
17
+ end # with some default options
18
+
19
+ context "validating an object" do
20
+ validatable = OpenStruct.new(:email => "")
21
+
22
+ setup do
23
+ WhyValidationsSuckIn96::ValidatesEmail.new(validatable, :attribute => :email)
24
+ end
25
+
26
+ should "allow a valid regular email address" do
27
+ validatable.email = "foo@example.com"
28
+ topic.validates?
29
+ end
30
+
31
+ should "allow an email address with periods" do
32
+ validatable.email = "foo.bar@example.com"
33
+ topic.validates?
34
+ end
35
+
36
+ should "allow an email address with a plus sign" do
37
+ validatable.email = "foo+bar@example.com"
38
+ topic.validates?
39
+ end
40
+
41
+ should "disallow a whack email address" do
42
+ validatable.email = "foo"
43
+ topic.validates?
44
+ end.equals(false)
45
+ end # validating an object
46
+ end # validates email
@@ -25,6 +25,7 @@ ActiveRecord::Schema.define(:version => 1) do
25
25
 
26
26
  create_table :chapters do |t|
27
27
  t.string :name
28
+ t.integer :number
28
29
  t.belongs_to :book
29
30
  end
30
31
 
@@ -124,4 +125,4 @@ class Collage < VisualWork
124
125
  setup_validations do
125
126
  validates_uniqueness_of :name, :scope => :author
126
127
  end
127
- end
128
+ end
@@ -68,6 +68,27 @@ context "validates uniqueness" do
68
68
  work.save
69
69
  end
70
70
  end # with default options
71
+
72
+ context "uniqueness of integers" do
73
+ setup do
74
+ Class.new(Chapter) do
75
+ setup_validations do
76
+ validates_uniqueness_of :number, :scope_to => :book_id
77
+ end
78
+ end
79
+ end
80
+
81
+ should "not allow two objects with the same number" do
82
+ chapter = topic.new(:name => "A Dark and Stormy Night",
83
+ :number => 1, :book_id => 1)
84
+ chapter.save!
85
+ other_chapter = topic.new(:name => "Another Dark and Stormy Night",
86
+ :number => 1, :book_id => 1)
87
+ !other_chapter.valid? && other_chapter.failed_validations.detect do |fv|
88
+ fv.kind_of?(WhyValidationsSuckIn96::ValidatesUniqueness) && fv.attribute == :number
89
+ end
90
+ end
91
+ end
71
92
 
72
93
  context "specifying case sensitivity" do
73
94
  setup do
@@ -150,4 +171,4 @@ context "validates uniqueness" do
150
171
  end
151
172
  end # when using with_scope
152
173
  end # validating an object
153
- end # validates uniqueness
174
+ end # validates uniqueness
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{whyvalidationssuckin96}
8
- s.version = "1.3.0"
8
+ s.version = "1.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["gabrielg", "douglasmeyer"]
12
- s.date = %q{2010-01-15}
12
+ s.date = %q{2010-01-27}
13
13
  s.description = %q{A library for setting up model validations, such as in ActiveRecord.}
14
14
  s.email = %q{gabriel.gironda@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -76,6 +76,7 @@ Gem::Specification.new do |s|
76
76
  "lib/whyvalidationssuckin96/macros/validates_acceptance.rb",
77
77
  "lib/whyvalidationssuckin96/macros/validates_associated.rb",
78
78
  "lib/whyvalidationssuckin96/macros/validates_confirmation.rb",
79
+ "lib/whyvalidationssuckin96/macros/validates_email.rb",
79
80
  "lib/whyvalidationssuckin96/macros/validates_exclusion.rb",
80
81
  "lib/whyvalidationssuckin96/macros/validates_format.rb",
81
82
  "lib/whyvalidationssuckin96/macros/validates_inclusion.rb",
@@ -93,10 +94,12 @@ Gem::Specification.new do |s|
93
94
  "lib/whyvalidationssuckin96/validation_builder.rb",
94
95
  "lib/whyvalidationssuckin96/validation_collection.rb",
95
96
  "lib/whyvalidationssuckin96/validation_support.rb",
97
+ "lib/whyvalidationssuckin96/vendor/rfc822.rb",
96
98
  "test/attribute_based_validation_test.rb",
97
99
  "test/macros/validates_acceptance_test.rb",
98
100
  "test/macros/validates_associated_test.rb",
99
101
  "test/macros/validates_confirmation_test.rb",
102
+ "test/macros/validates_email_test.rb",
100
103
  "test/macros/validates_exclusion_test.rb",
101
104
  "test/macros/validates_format_test.rb",
102
105
  "test/macros/validates_inclusion_test.rb",
@@ -126,6 +129,7 @@ Gem::Specification.new do |s|
126
129
  "test/macros/validates_acceptance_test.rb",
127
130
  "test/macros/validates_associated_test.rb",
128
131
  "test/macros/validates_confirmation_test.rb",
132
+ "test/macros/validates_email_test.rb",
129
133
  "test/macros/validates_exclusion_test.rb",
130
134
  "test/macros/validates_format_test.rb",
131
135
  "test/macros/validates_inclusion_test.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whyvalidationssuckin96
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gabrielg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-15 00:00:00 -06:00
13
+ date: 2010-01-27 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -112,6 +112,7 @@ files:
112
112
  - lib/whyvalidationssuckin96/macros/validates_acceptance.rb
113
113
  - lib/whyvalidationssuckin96/macros/validates_associated.rb
114
114
  - lib/whyvalidationssuckin96/macros/validates_confirmation.rb
115
+ - lib/whyvalidationssuckin96/macros/validates_email.rb
115
116
  - lib/whyvalidationssuckin96/macros/validates_exclusion.rb
116
117
  - lib/whyvalidationssuckin96/macros/validates_format.rb
117
118
  - lib/whyvalidationssuckin96/macros/validates_inclusion.rb
@@ -129,10 +130,12 @@ files:
129
130
  - lib/whyvalidationssuckin96/validation_builder.rb
130
131
  - lib/whyvalidationssuckin96/validation_collection.rb
131
132
  - lib/whyvalidationssuckin96/validation_support.rb
133
+ - lib/whyvalidationssuckin96/vendor/rfc822.rb
132
134
  - test/attribute_based_validation_test.rb
133
135
  - test/macros/validates_acceptance_test.rb
134
136
  - test/macros/validates_associated_test.rb
135
137
  - test/macros/validates_confirmation_test.rb
138
+ - test/macros/validates_email_test.rb
136
139
  - test/macros/validates_exclusion_test.rb
137
140
  - test/macros/validates_format_test.rb
138
141
  - test/macros/validates_inclusion_test.rb
@@ -184,6 +187,7 @@ test_files:
184
187
  - test/macros/validates_acceptance_test.rb
185
188
  - test/macros/validates_associated_test.rb
186
189
  - test/macros/validates_confirmation_test.rb
190
+ - test/macros/validates_email_test.rb
187
191
  - test/macros/validates_exclusion_test.rb
188
192
  - test/macros/validates_format_test.rb
189
193
  - test/macros/validates_inclusion_test.rb