validate_as_email 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +48 -0
- data/features/rspec_matcher.feature +24 -0
- data/features/support/env.rb +5 -0
- data/lib/validate_as_email.rb +4 -0
- data/lib/validate_as_email/rspec.rb +48 -0
- data/lib/validate_as_email/version.rb +1 -1
- data/spec/spec_helper.rb +5 -0
- metadata +4 -1
data/README.md
CHANGED
@@ -25,6 +25,54 @@ Rubinius using Travis CI.
|
|
25
25
|
You will need to be using Rails 3 to make use of this validator, as it
|
26
26
|
is built on top of ActiveModel, which was introduced in Rails 3.
|
27
27
|
|
28
|
+
### Usage with ActiveModel
|
29
|
+
|
30
|
+
``` ruby
|
31
|
+
class Person
|
32
|
+
include ActiveModel::Validations
|
33
|
+
validates_as_email :email
|
34
|
+
attr_accessor :email
|
35
|
+
end
|
36
|
+
|
37
|
+
# OR
|
38
|
+
|
39
|
+
class Person
|
40
|
+
include ActiveModel::Validations
|
41
|
+
validates :email, email: true
|
42
|
+
attr_accessor :email
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
### Usage with ActiveRecord
|
47
|
+
|
48
|
+
``` ruby
|
49
|
+
class Person < ActiveRecord::Base
|
50
|
+
validates_as_email :email
|
51
|
+
attr_accessor :email
|
52
|
+
end
|
53
|
+
|
54
|
+
# OR
|
55
|
+
|
56
|
+
class Person < ActiveRecord::Base
|
57
|
+
validates :email, email: true
|
58
|
+
attr_accessor :email
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
### Built-in RSpec Matcher
|
63
|
+
|
64
|
+
``` ruby
|
65
|
+
class Person < ActiveRecord::Base
|
66
|
+
validates_as_email :email
|
67
|
+
attr_accessor :email
|
68
|
+
end
|
69
|
+
|
70
|
+
describe Person do
|
71
|
+
it { should have_a_valid_email_address_for(:email) }
|
72
|
+
it { should_not have_a_valid_email_address_for(:email) }
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
28
76
|
## Contributing
|
29
77
|
|
30
78
|
1. Fork it
|
@@ -0,0 +1,24 @@
|
|
1
|
+
Feature: RSpec matcher
|
2
|
+
|
3
|
+
Scenario: The built-in RSpec matcher covers validation of email for you
|
4
|
+
Given a file named "rspec_matcher.rb" with:
|
5
|
+
"""
|
6
|
+
require 'active_model'
|
7
|
+
require 'rspec'
|
8
|
+
require 'validate_as_email'
|
9
|
+
|
10
|
+
class Person
|
11
|
+
include ActiveModel::Validations
|
12
|
+
validates_as_email :email
|
13
|
+
attr_accessor :email
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Person do
|
17
|
+
it { should_not have_a_valid_email_address_for(:email) }
|
18
|
+
end
|
19
|
+
"""
|
20
|
+
When I run `rspec rspec_matcher.rb`
|
21
|
+
Then it should fail with:
|
22
|
+
"""
|
23
|
+
not to have a valid email address for :email
|
24
|
+
"""
|
data/features/support/env.rb
CHANGED
data/lib/validate_as_email.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rspec/rails/extensions/active_record/base'
|
2
|
+
# Adds a custom matcher to RSpec to make it easier to make sure your email
|
3
|
+
# column is valid.
|
4
|
+
#
|
5
|
+
# class Person
|
6
|
+
# include ActiveModel::Validations
|
7
|
+
#
|
8
|
+
# validates_as_email :email
|
9
|
+
#
|
10
|
+
# def initialize(attributes = {})
|
11
|
+
# @attributes = attributes
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# def email
|
15
|
+
# @attributes[:email]
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# def email=(address)
|
19
|
+
# @attributes[:email] = address
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# describe Person
|
24
|
+
# it { should have_a_valid_email_address_for(:email) }
|
25
|
+
# end
|
26
|
+
RSpec::Matchers.define(:have_a_valid_email_address_for) do |column_name|
|
27
|
+
match do |klass|
|
28
|
+
%w(
|
29
|
+
matz@example.com
|
30
|
+
a@b.tld
|
31
|
+
1@8.8.8.8
|
32
|
+
).each do |valid|
|
33
|
+
klass.send("#{column_name}=", valid)
|
34
|
+
klass.should have(0).errors_on(column_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
%w(
|
38
|
+
word
|
39
|
+
a@
|
40
|
+
@b
|
41
|
+
example.com
|
42
|
+
8.8.8.8
|
43
|
+
).each do |invalid|
|
44
|
+
klass.send("#{column_name}=", invalid)
|
45
|
+
klass.should have(1).error_on(column_name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validate_as_email
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -163,12 +163,14 @@ extensions: []
|
|
163
163
|
extra_rdoc_files: []
|
164
164
|
files:
|
165
165
|
- lib/active_model/validations/email_validator.rb
|
166
|
+
- lib/validate_as_email/rspec.rb
|
166
167
|
- lib/validate_as_email/version.rb
|
167
168
|
- lib/validate_as_email.rb
|
168
169
|
- README.md
|
169
170
|
- LICENSE
|
170
171
|
- features/active_model.feature
|
171
172
|
- features/active_record.feature
|
173
|
+
- features/rspec_matcher.feature
|
172
174
|
- features/support/cucumber_helpers.rb
|
173
175
|
- features/support/env.rb
|
174
176
|
- spec/active_model/validations/email_validator_spec.rb
|
@@ -201,6 +203,7 @@ summary: The ultimate Rails 3 email validator. Powered by the Mail gem.
|
|
201
203
|
test_files:
|
202
204
|
- features/active_model.feature
|
203
205
|
- features/active_record.feature
|
206
|
+
- features/rspec_matcher.feature
|
204
207
|
- features/support/cucumber_helpers.rb
|
205
208
|
- features/support/env.rb
|
206
209
|
- spec/active_model/validations/email_validator_spec.rb
|