activevalidators 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activevalidators (1.2.3)
4
+ activevalidators (1.2.4)
5
5
  activemodel (>= 3.0.0)
6
6
  activerecord (>= 3.0.0)
7
7
  date_validator (= 0.5.9)
data/README.md CHANGED
@@ -10,10 +10,6 @@ In your Gemfile ( >>= 1.1.0 ):
10
10
 
11
11
  gem 'activevalidators'
12
12
 
13
- **N.B: if you use version <= 1.0.2, use this instead:**
14
-
15
- gem 'activevalidators', :require => 'active_validators'
16
-
17
13
  In your models, the gem provides new validators like `email`, or `url`:
18
14
 
19
15
  class User
@@ -21,6 +17,9 @@ In your models, the gem provides new validators like `email`, or `url`:
21
17
  validates :link_url, :url => true
22
18
  validates :user_phone, :phone => true
23
19
  validates :password, :password => { :strength => :medium }
20
+ validates :twitter_at, :twitter => { :format => :username_with_at }
21
+ validates :twitter_url, :twitter => { :format => :url }
22
+ validates :twitter, :twitter => true
24
23
  end
25
24
 
26
25
  class Article
@@ -48,6 +47,7 @@ Exhaustive list of supported validators and their implementation:
48
47
  * `email` : based on the `mail` gem
49
48
  * `url` : based on a regular expression
50
49
  * `phone` : based on a regular expression
50
+ * `twitter` : based on a regular expression
51
51
  * `slug` : based on `ActiveSupport::String#parameterize`
52
52
  * `ip` : based on `Resolv::IPv[4|6]::Regex`
53
53
  * `credit_card` : based on the `Luhn` algorithm
@@ -80,6 +80,7 @@ Contributors
80
80
  * Franck Verrot
81
81
  * Oriol Gual
82
82
  * Paco Guzmán
83
+ * Garrett Bjerkhoel
83
84
 
84
85
  Copyright
85
86
  ---------
@@ -4,7 +4,7 @@ $:.unshift lib unless $:.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "activevalidators"
7
- s.version = '1.2.3'
7
+ s.version = '1.2.4'
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Franck Verrot", "Paco Guzmán", "Oriol Gual", "Garrett Bjerkhoel"]
10
10
  s.email = ["franck@verrot.fr"]
@@ -0,0 +1,22 @@
1
+ module ActiveModel
2
+ module Validations
3
+
4
+ class TwitterValidator < EachValidator
5
+ def validate_each(record, attribute, value)
6
+ format = options[:format].to_sym if options[:format]
7
+
8
+ if value.nil?
9
+ record.errors.add_on_blank(attribute)
10
+ elsif format == :url
11
+ match = value.match(/^https?:\/\/(www\.)?twitter.com\/([A-Za-z0-9_]{1,15})$/i)
12
+ record.errors.add(attribute) unless match && !match[2].nil? # www. is first capture
13
+ elsif format == :username_with_at
14
+ record.errors.add(attribute) unless value.match(/^@([A-Za-z0-9_]{1,15})$/i)
15
+ else
16
+ record.errors.add(attribute) unless value.match(/^([A-Za-z0-9_]{1,15})$/i)
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -16,5 +16,6 @@ module ActiveModel
16
16
  autoload :CreditCardValidator
17
17
  autoload :DateValidator, 'date_validator'
18
18
  autoload :PasswordValidator
19
+ autoload :TwitterValidator
19
20
  end
20
21
  end
@@ -10,5 +10,5 @@ end
10
10
  class TestRecord
11
11
  include ActiveModel::Validations
12
12
  attr_accessor :ip, :url, :slug, :responder, :global_condition,
13
- :local_condition, :phone, :email, :card, :password
13
+ :local_condition, :phone, :email, :card, :password, :twitter_username
14
14
  end
@@ -0,0 +1,154 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Twitter Validation" do
4
+ before(:each) do
5
+ TestRecord.reset_callbacks(:validate)
6
+ TestRecord.validates :twitter_username, :twitter => true
7
+ end
8
+
9
+ subject { TestRecord.new }
10
+
11
+ it "rejects invalid urls" do
12
+ subject.should_not be_valid
13
+ subject.should have(1).error
14
+ end
15
+
16
+ it "generates an error message of type blank" do
17
+ subject.should_not be_valid
18
+ subject.errors[:twitter_username].should include subject.errors.generate_message(:twitter_username, :blank)
19
+ end
20
+
21
+ describe "for twitter url validator" do
22
+ before do
23
+ TestRecord.reset_callbacks(:validate)
24
+ TestRecord.validates :twitter_username, :twitter => { :format => :url }
25
+ end
26
+
27
+ it "validates with http" do
28
+ subject.twitter_username = 'http://twitter.com/garrettb'
29
+ subject.should be_valid
30
+ end
31
+
32
+ it "validates with https protocol" do
33
+ subject.twitter_username = 'https://twitter.com/garrettb'
34
+ subject.should be_valid
35
+ end
36
+
37
+ it "generate error with ftp protocol" do
38
+ subject.twitter_username = 'ftp://twitter.com/garrettb'
39
+ subject.should_not be_valid
40
+ subject.should have(1).error
41
+ end
42
+
43
+ it "validates with www and http" do
44
+ subject.twitter_username = 'http://www.twitter.com/garrettb'
45
+ subject.should be_valid
46
+ end
47
+
48
+ it "generate error without www dot" do
49
+ subject.twitter_username = 'http://wwwtwitter.com/garrettb'
50
+ subject.should_not be_valid
51
+ subject.should have(1).error
52
+ end
53
+
54
+ it "generate error without no username" do
55
+ subject.twitter_username = 'http://twitter.com'
56
+ subject.should_not be_valid
57
+ subject.should have(1).error
58
+ end
59
+
60
+ it "generate error without no username and trailing slash" do
61
+ subject.twitter_username = 'http://twitter.com/'
62
+ subject.should_not be_valid
63
+ subject.should have(1).error
64
+ end
65
+
66
+ it "generate error with too long of username" do
67
+ subject.twitter_username = 'http://twitter.com/garrettbjerkhoelwashere'
68
+ subject.should_not be_valid
69
+ subject.should have(1).error
70
+ end
71
+
72
+ it "generate error with invalid character" do
73
+ subject.twitter_username = 'http://twitter.com/garrettbjerkhoé'
74
+ subject.should_not be_valid
75
+ subject.should have(1).error
76
+ end
77
+ end
78
+
79
+ describe "for twitter at sign validator" do
80
+ before do
81
+ TestRecord.reset_callbacks(:validate)
82
+ TestRecord.validates :twitter_username, :twitter => { :format => :username_with_at }
83
+ end
84
+
85
+ it "validate with valid username" do
86
+ subject.twitter_username = '@garrettb'
87
+ subject.should be_valid
88
+ end
89
+
90
+ it "validate with one character" do
91
+ subject.twitter_username = '@a'
92
+ subject.should be_valid
93
+ end
94
+
95
+ it "generate error with too long of username" do
96
+ subject.twitter_username = '@garrettbjerkhoelwashere'
97
+ subject.should_not be_valid
98
+ subject.should have(1).error
99
+ end
100
+
101
+ it "generate error with no username" do
102
+ subject.twitter_username = '@'
103
+ subject.should_not be_valid
104
+ subject.should have(1).error
105
+ end
106
+
107
+ it "generate error with invalid character" do
108
+ subject.twitter_username = '@érik'
109
+ subject.should_not be_valid
110
+ subject.should have(1).error
111
+ end
112
+ end
113
+
114
+ describe "for twitter without at sign validator" do
115
+ before do
116
+ TestRecord.reset_callbacks(:validate)
117
+ TestRecord.validates :twitter_username, :twitter => true
118
+ end
119
+
120
+ it "validate with valid username" do
121
+ subject.twitter_username = 'garrettb'
122
+ subject.should be_valid
123
+ end
124
+
125
+ it "validate with one character" do
126
+ subject.twitter_username = 'a'
127
+ subject.should be_valid
128
+ end
129
+
130
+ it "generate error with too long of username" do
131
+ subject.twitter_username = 'garrettbjerkhoelwashere'
132
+ subject.should_not be_valid
133
+ subject.should have(1).error
134
+ end
135
+
136
+ it "generate error with no username" do
137
+ subject.twitter_username = ''
138
+ subject.should_not be_valid
139
+ subject.should have(1).error
140
+ end
141
+
142
+ it "generate error with invalid character" do
143
+ subject.twitter_username = 'érik'
144
+ subject.should_not be_valid
145
+ subject.should have(1).error
146
+ end
147
+
148
+ it "generate error with at sign character" do
149
+ subject.twitter_username = '@garrettb'
150
+ subject.should_not be_valid
151
+ subject.should have(1).error
152
+ end
153
+ end
154
+ end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activevalidators
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 23
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
8
  - 2
8
- - 3
9
- version: 1.2.3
9
+ - 4
10
+ version: 1.2.4
10
11
  platform: ruby
11
12
  authors:
12
13
  - Franck Verrot
@@ -17,7 +18,7 @@ autorequire:
17
18
  bindir: bin
18
19
  cert_chain: []
19
20
 
20
- date: 2010-12-08 00:00:00 +01:00
21
+ date: 2010-12-11 00:00:00 +01:00
21
22
  default_executable:
22
23
  dependencies:
23
24
  - !ruby/object:Gem::Dependency
@@ -28,6 +29,7 @@ dependencies:
28
29
  requirements:
29
30
  - - ">="
30
31
  - !ruby/object:Gem::Version
32
+ hash: 3
31
33
  segments:
32
34
  - 0
33
35
  version: "0"
@@ -41,6 +43,7 @@ dependencies:
41
43
  requirements:
42
44
  - - ">="
43
45
  - !ruby/object:Gem::Version
46
+ hash: 3
44
47
  segments:
45
48
  - 0
46
49
  version: "0"
@@ -54,6 +57,7 @@ dependencies:
54
57
  requirements:
55
58
  - - ">="
56
59
  - !ruby/object:Gem::Version
60
+ hash: 3
57
61
  segments:
58
62
  - 0
59
63
  version: "0"
@@ -67,6 +71,7 @@ dependencies:
67
71
  requirements:
68
72
  - - ">="
69
73
  - !ruby/object:Gem::Version
74
+ hash: 3
70
75
  segments:
71
76
  - 0
72
77
  version: "0"
@@ -80,6 +85,7 @@ dependencies:
80
85
  requirements:
81
86
  - - ">="
82
87
  - !ruby/object:Gem::Version
88
+ hash: 7
83
89
  segments:
84
90
  - 3
85
91
  - 0
@@ -95,6 +101,7 @@ dependencies:
95
101
  requirements:
96
102
  - - ">="
97
103
  - !ruby/object:Gem::Version
104
+ hash: 7
98
105
  segments:
99
106
  - 3
100
107
  - 0
@@ -110,6 +117,7 @@ dependencies:
110
117
  requirements:
111
118
  - - ">="
112
119
  - !ruby/object:Gem::Version
120
+ hash: 3
113
121
  segments:
114
122
  - 0
115
123
  version: "0"
@@ -123,6 +131,7 @@ dependencies:
123
131
  requirements:
124
132
  - - "="
125
133
  - !ruby/object:Gem::Version
134
+ hash: 25
126
135
  segments:
127
136
  - 0
128
137
  - 5
@@ -156,6 +165,7 @@ files:
156
165
  - lib/active_model/validations/phone_validator.rb
157
166
  - lib/active_model/validations/respond_to_validator.rb
158
167
  - lib/active_model/validations/slug_validator.rb
168
+ - lib/active_model/validations/twitter_validator.rb
159
169
  - lib/active_model/validations/url_validator.rb
160
170
  - lib/activevalidators.rb
161
171
  - spec/spec_helper.rb
@@ -166,6 +176,7 @@ files:
166
176
  - spec/validations/phone_spec.rb
167
177
  - spec/validations/respond_to_spec.rb
168
178
  - spec/validations/slug_spec.rb
179
+ - spec/validations/twitter_spec.rb
169
180
  - spec/validations/url_spec.rb
170
181
  has_rdoc: true
171
182
  homepage: http://github.com/cesario/activevalidators
@@ -181,6 +192,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
181
192
  requirements:
182
193
  - - ">="
183
194
  - !ruby/object:Gem::Version
195
+ hash: 3
184
196
  segments:
185
197
  - 0
186
198
  version: "0"
@@ -189,6 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
201
  requirements:
190
202
  - - ">="
191
203
  - !ruby/object:Gem::Version
204
+ hash: 3
192
205
  segments:
193
206
  - 0
194
207
  version: "0"
@@ -208,4 +221,5 @@ test_files:
208
221
  - spec/validations/phone_spec.rb
209
222
  - spec/validations/respond_to_spec.rb
210
223
  - spec/validations/slug_spec.rb
224
+ - spec/validations/twitter_spec.rb
211
225
  - spec/validations/url_spec.rb