has_validated_attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ *.gem
2
+ .bundle
3
+ .rvmrc
4
+ Gemfile.lock
5
+ pkg/*
6
+ log/*.log
7
+ *.tmproj
8
+ tmp
9
+ log
10
+ .gitconfig
11
+ bin/*
12
+ .rbenv-version
13
+ .rbenv-gemsets
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format nested
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 1.9.2
4
+
5
+ script: rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in send_grid.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,62 @@
1
+ == HasValidatedAttributes {<img src="https://secure.travis-ci.org/kylejginavan/has_validated_attributes.png"/>}[http://travis-ci.org/kylejginavan/has_validated_attributes]
2
+
3
+ == DESCRIPTION
4
+
5
+ has_validated_attributes is a Ruby on Rails gem that lets you validate your fields .
6
+
7
+ == INSTALLATION & SETUP:
8
+
9
+ add in your Gemfile:
10
+
11
+ gem 'has_validated_attributes'
12
+
13
+ in your model:
14
+
15
+ has_validated_attributes :field => :normalized_type
16
+
17
+ for example:
18
+
19
+ class CoolStuff < ActiveRecord::Base
20
+ has_validated_attributes :username_attr => {:format => :username}, :name_attr => {:format => :name}, :email_attr => {:format => :email},
21
+ :phone_number_attr => {:format => :phone_number}, :domain_attr => {:format => :domain},
22
+ :phone_extension_attr => {:format => :phone_extension}, :zipcode_attr => {:format => :zipcode},
23
+ :middle_initial_attr => {:format => :middle_initial}, :dollar_attr => {:format => :dollar},
24
+ :positive_dollar_attr => {:format => :positive_dollar}, :percent_attr => {:format => :percent},
25
+ :positive_percent_attr => {:format => :positive_percent}, :url_attr => {:format => :url}, :ssn_attr => {:format => :ssn},
26
+ :taxid_attr => {:format => :taxid}, :age_attr => {:format => :age}, :number_attr => {:format => :number}
27
+ end
28
+
29
+ == Extra Options
30
+ You can pass extra options like
31
+ {:username_attr => {:format => :username, :allow_nil => true, :allow_blank => true, :message => "some message", :if => :food?}
32
+
33
+
34
+ == Validations availables
35
+ :username, :phone_number, :phone_extension, :person_name, :email,
36
+ :zipcode, :dollar, :percent, :positive_percent, :middle_initial, :url,
37
+ :positive_dollar, :domain, :ssn, :taxid, :age, :number
38
+
39
+
40
+ == COPYRIGHT
41
+
42
+ Copyright (c) 2011 Kyle Ginavan
43
+
44
+ Permission is hereby granted, free of charge, to any person obtaining
45
+ a copy of this software and associated documentation files (the
46
+ "Software"), to deal in the Software without restriction, including
47
+ without limitation the rights to use, copy, modify, merge, publish,
48
+ distribute, sublicense, and/or sell copies of the Software, and to
49
+ permit persons to whom the Software is furnished to do so, subject to
50
+ the following conditions:
51
+
52
+ The above copyright notice and this permission notice shall be
53
+ included in all copies or substantial portions of the Software.
54
+
55
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
58
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
59
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
60
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
61
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62
+ See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+ Bundler::GemHelper.install_tasks
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{has_validated_attributes}
7
+ s.version = HasValidatedAttributes::VERSION
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
+ s.authors = ["Kyle Ginavan"]
10
+ s.date = %q{2010-05-18}
11
+ s.homepage = "https://github.com/kylejginavan/has_validated_attributes"
12
+ s.summary = %q{Ruby on Rails gem for validate data prior to save}
13
+ s.description = %q{has_validated_attributes is a Ruby on Rails gem that lets you validate your fields.}
14
+ s.email = %q{kylejginavan@gmail.com}
15
+ s.rubyforge_project = "has_validated_attributes"
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_development_dependency "rspec"
21
+ s.add_development_dependency("activerecord", ['>= 3.1.0'])
22
+ s.add_development_dependency("sqlite3")
23
+ s.add_development_dependency('database_cleaner')
24
+ end
25
+
@@ -0,0 +1,58 @@
1
+ module HasValidatedAttributes
2
+ extend ActiveSupport::Concern
3
+
4
+ #instance methods
5
+ def self.validations(*args)
6
+ args.first.each do |name, format|
7
+ HasValidatedAttributes.define_singleton_method "#{name}_format" do |field_name = nil, options = {}|
8
+ validation = {}
9
+ validation.merge!(:if => "#{field_name}?".to_sym) if format.delete(:has_if?)
10
+ ### length options ###
11
+ opts = options.select{|k, v| k.match(/length/)}
12
+ opts.each{|k,v| validation.merge!(:length => {k.to_s.split("_").first.to_sym => v});options.delete(k)} if opts.present?
13
+ ### extra options ###
14
+ validation.merge!(options) if options.present?
15
+
16
+ format.merge(validation)
17
+ end
18
+ end
19
+ end
20
+
21
+ #loading all methods dynamically
22
+ validations :name => {:format => {:with => /\A[^[:cntrl:]\\<>]*\z/, :message => "avoid non-printing characters and \\&gt;&lt;/ please."}, :length => {:maximum => 63}, :has_if? => true},
23
+ :username => {:length => {:within => 5..127}, :format => {:with => /\A\w[\w\.\-_@]+\z/, :message => "use only letters, numbers, and .-_@ please."}, :uniqueness => true},
24
+ :rails_name => {:format => {:with => /^[a-zA-Z\_]*?$/u, :message => "should only include underscores and letters."}},
25
+ :email => {:length => {:maximum => 63}, :format => {:with => /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|pro|mobi|name|aero|jobs|museum)\z/i, :message => "should look like an email address."}},
26
+ :phone_number => {:numericality => {:greater_than_or_equal_to => 1000000000, :less_than => 10000000000, :message => 'accepts only 10 numbers and (),.- characters'}, :has_if? => true},
27
+ :phone_extension => {:numericality => {:greater_than_or_equal_to => 0, :less_than => 100000000, :message => 'accepts only numbers (0-9)'}, :has_if? => true},
28
+ :domain => {:length => {:maximum => 63}, :format => {:with => /\A(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|pro|mobi|name|aero|jobs|museum)\z/i, :message => "should look like a domain name."}},
29
+ :zipcode => {:format => {:with => /^\d{5}(\d{4})?$/, :message => "must contain 5 or 9 numbers"}, :has_if? => true},
30
+ :middle_initial => {:format => {:with => /^[a-zA-Z]{0,1}$/u, :message => "accepts only one letter"}},
31
+ :dollar => {:format => {:with => /^-?[0-9]{0,12}(\.[0-9]{0,2})?$/, :message => "accepts only numeric characters, period, and negative sign"}, :numericality => {:greater_than => -1000000000000, :less_than => 1000000000000}, :allow_nil => true},
32
+ :positive_dollar => {:format => {:with => /^[0-9]{0,12}(\.[0-9]{0,2})?$/, :message => "accepts only numeric characters, period"}, :numericality => {:greater_than_or_equal_to => 0, :less_than => 1000000000000}, :allow_nil => true},
33
+ :percent => {:format => {:with => /^-?[0-9]{0,3}(\.[0-9]{0,3})?$/, :message => "accepts only numeric characters, period, negative sign, and must be equal/less/greater than +/- 100"}},
34
+ :positive_percent => {:format => {:with => /^[0-9]{0,3}(\.[0-9]{0,3})?$/, :message => "accepts only numeric characters, period, and must be less than 100"}, :numericality => {:greater_than_or_equal_to => 0, :less_than_or_equal_to => 100}, :allow_nil => true},
35
+ :url => {:length => {:maximum => 255}, :format => {:with => /^(http|https|ftp):\/\/[A-Z0-9]+([\.]{1}[a-z0-9-]{1,63})*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix, :message => "web address isnt valid"}, :has_if? => true},
36
+ :social_security_number => {:length => {:is => 9}, :numericality => {:greater_than_or_equal_to => 0, :less_than => 1000000000, :message => "must be in the format 111-11-1111"}, :has_if? => true},
37
+ :taxid => {:length => {:is => 9}, :numericality => {:greater_than_or_equal_to => 9999999, :less_than => 1000000000, :message => "must be in the format 11-1111111"}, :has_if? => true},
38
+ :age => {:numericality => {:greater_than_or_equal_to => 0, :less_than_or_equal_to => 110, :message => 'must contain only 3 numbers and less than 110'}},
39
+ :number => {:numericality => {:message => "accepts only numbers (0-9)"}}
40
+
41
+ included do
42
+ class_eval do
43
+ def self.has_validated_attributes(args = {})
44
+ if args.blank? || !args.is_a?(Hash)
45
+ raise ArgumentError, 'Must define the fields you want to be validate with has_validated_attributes :field_one => {:format => :phone}, :field_two => {:format => :zipcode, :required => true}'
46
+ end
47
+
48
+ args.each do |field, options|
49
+ type = options.delete(:format)
50
+ validates field.to_sym, HasValidatedAttributes.send("#{type}_format".to_sym, field, options)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ #include activerecord
58
+ ActiveRecord::Base.send :include, HasValidatedAttributes
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module HasValidatedAttributes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: spec/db/test.sqlite3
data/spec/db/schema.rb ADDED
@@ -0,0 +1,23 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+
3
+ create_table "resources", :force => true do |t|
4
+ t.string "username_attr"
5
+ t.string "name_attr"
6
+ t.string "domain_attr"
7
+ t.string "email_attr"
8
+ t.string "phone_number_attr"
9
+ t.string "phone_extension_attr"
10
+ t.string "middle_initial_attr"
11
+ t.string "ssn_attr"
12
+ t.string "zipcode_attr"
13
+ t.string "taxid_attr"
14
+ t.string "dollar_attr"
15
+ t.string "positive_dollar_attr"
16
+ t.string "percent_attr"
17
+ t.string "positive_percent_attr"
18
+ t.string "url_attr"
19
+ t.string "age_attr"
20
+ t.string "number_attr"
21
+ end
22
+
23
+ end
data/spec/db/test.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ class Resource < ActiveRecord::Base
4
+ has_validated_attributes :username_attr => {:format => :username, :required => true}#, :name_attr => {:format => :username, :required => true}
5
+ end
6
+
7
+ describe "HasValidatedAttributes" do
8
+ before(:each) do
9
+ @resource = Resource.create(:username_attr => "testusername", :name_attr => "testname", :email_attr => "test@example.com",
10
+ :phone_number_attr => "1111111111", :phone_extension_attr => "111111", :domain_attr => "www.test.com", :zipcode_attr => "11111",
11
+ :middle_initial_attr => "A", :dollar_attr => "-11", :positive_dollar_attr => "1", :percent_attr => "12",
12
+ :positive_percent_attr => "99", :url_attr => "http://www.google.com", :ssn_attr => "111111111", :taxid_attr => "111111111",
13
+ :number_attr => "1")
14
+ end
15
+
16
+ describe "#username" do
17
+ it "should return error" do
18
+ [">*,.<><", "<<< test", "Kansas City", "-- Hey --", "& youuuu", "21 Jump"].each do |value|
19
+ @resource.username_attr = value
20
+ @resource.valid?.should be_false
21
+ @resource.errors.full_messages.should == ["Username attr use only letters, numbers, and .-_@ please."]
22
+ end
23
+ end
24
+ end
25
+ end
Binary file
@@ -0,0 +1,348 @@
1
+ require 'spec_helper'
2
+
3
+ class Resource < ActiveRecord::Base
4
+ has_validated_attributes :name_attr => {:format => :name, :maximum_length => 10}, :username_attr => {:format => :username}, :email_attr => {:format => :email},
5
+ :phone_number_attr => {:format => :phone_number}, :phone_extension_attr => {:format => :phone_extension},
6
+ :domain_attr => {:format => :domain}, :zipcode_attr => {:format => :zipcode},
7
+ :middle_initial_attr => {:format => :middle_initial}, :dollar_attr => {:format => :dollar},
8
+ :positive_dollar_attr => {:format => :positive_dollar}, :percent_attr => {:format => :percent},
9
+ :positive_percent_attr => {:format => :positive_percent}, :url_attr => {:format => :url}, :ssn_attr => {:format => :social_security_number},
10
+ :taxid_attr => {:format => :taxid}, :age_attr => {:format => :age}, :number_attr => {:format => :number}
11
+ end
12
+
13
+ describe "HasValidatedAttributes" do
14
+ before(:each) do
15
+ @resource = Resource.create(:username_attr => "testusername", :name_attr => "testname", :email_attr => "test@example.com",
16
+ :phone_number_attr => "1111111111", :phone_extension_attr => "111111", :domain_attr => "www.test.com", :zipcode_attr => "11111",
17
+ :middle_initial_attr => "A", :dollar_attr => "-11", :positive_dollar_attr => "1", :percent_attr => "12",
18
+ :positive_percent_attr => "99", :url_attr => "http://www.google.com", :ssn_attr => "111111111", :taxid_attr => "111111111",
19
+ :number_attr => "1", :age_attr => 28)
20
+ end
21
+
22
+ describe "#username" do
23
+ it "should return error" do
24
+ [">*,.<><", "<<< test", "Kansas City", "-- Hey --", "& youuuu", "21 Jump"].each do |value|
25
+ @resource.username_attr = value
26
+ @resource.valid?.should be_false
27
+ @resource.errors.full_messages.should == ["Username attr use only letters, numbers, and .-_@ please."]
28
+ end
29
+ end
30
+
31
+ it "should return error with less than 5 chars" do
32
+ @resource.username_attr = "test"
33
+ @resource.valid?.should be_false
34
+ @resource.errors.full_messages.should == ["Username attr is too short (minimum is 5 characters)"]
35
+ end
36
+
37
+ it "should return error with more than 127 chars" do
38
+ @resource.username_attr = "test" * 128
39
+ @resource.valid?.should be_false
40
+ @resource.errors.full_messages.should == ["Username attr is too long (maximum is 127 characters)"]
41
+ end
42
+
43
+ it "should return ok" do
44
+ ["kansascity", "kansascity@org1", "kansas.city@org1", "kansas_city@org1", "kansas-city", "1kc.-_@"].each do |value|
45
+ @resource.username_attr = value
46
+ @resource.valid?.should be_true
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "#name" do
52
+ it "should return error" do
53
+ [">*", "< test"].each do |value|
54
+ @resource.name_attr = value
55
+ @resource.valid?.should be_false
56
+ @resource.errors.full_messages.should == ["Name attr avoid non-printing characters and \\&gt;&lt;/ please."]
57
+ end
58
+ end
59
+
60
+ it "should return error with more than 10 chars" do
61
+ @resource.name_attr = "test" * 6
62
+ @resource.valid?.should be_false
63
+ @resource.errors.full_messages.should == ["Name attr is too long (maximum is 10 characters)"]
64
+ end
65
+
66
+ it "should return ok" do
67
+ ["k c", "- H-", " t", "& u", "21 ", "brok", nil].each do |value|
68
+ @resource.name_attr = value
69
+ @resource.valid?.should be_true
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "#email" do
75
+ it "should return error" do
76
+ ["Abc.example.com", "A@b@c@example.com", "()[]\;:,<>@example.com", "abc@example.comar"].each do |value|
77
+ @resource.email_attr = value
78
+ @resource.valid?.should be_false
79
+ @resource.errors.full_messages.should == ["Email attr should look like an email address."]
80
+ end
81
+ end
82
+
83
+ it "should return error with more than 63 chars" do
84
+ @resource.email_attr = "test@example.com" * 64
85
+ @resource.valid?.should be_false
86
+ @resource.errors.full_messages.should == ["Email attr is too long (maximum is 63 characters)", "Email attr should look like an email address."]
87
+ end
88
+
89
+ it "should return ok" do
90
+ ["abc@example.com", "Abc@example.com", "aBC@example.com", "abc.123@example.com"].each do |value|
91
+ @resource.email_attr = value
92
+ @resource.valid?.should be_true
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "#phone_number" do
98
+ it "should return error" do
99
+ [">*", "< test", "www.test..com", "www.test.c", "www-test.com", "abc", "123", "&*()", "www.test-com"].each do |value|
100
+ @resource.phone_number_attr = value
101
+ @resource.valid?.should be_false
102
+ @resource.errors.full_messages.should == ["Phone number attr accepts only 10 numbers and (),.- characters"]
103
+ end
104
+ end
105
+
106
+ it "should return ok" do
107
+ ["9134456677", "5444456677"].each do |value|
108
+ @resource.phone_number_attr = value
109
+ @resource.valid?.should be_true
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "#phone_extension" do
115
+ it "should return error" do
116
+ ["-1", "qwert"].each do |value|
117
+ @resource.phone_extension_attr = value
118
+ @resource.valid?.should be_false
119
+ @resource.errors.full_messages.should == ["Phone extension attr accepts only numbers (0-9)"]
120
+ end
121
+ end
122
+
123
+ it "should return ok" do
124
+ ["123", "123456"].each do |value|
125
+ @resource.phone_extension_attr = value
126
+ @resource.valid?.should be_true
127
+ end
128
+ end
129
+ end
130
+
131
+
132
+ describe "#domain" do
133
+ it "should return error" do
134
+ [">*", "<test", "test-er"].each do |value|
135
+ @resource.domain_attr = value
136
+ @resource.valid?.should be_false
137
+ @resource.errors.full_messages.should == ["Domain attr should look like a domain name."]
138
+ end
139
+ end
140
+
141
+ it "should return error with more than 63 chars" do
142
+ @resource.domain_attr = "a" * 64 + ".com"
143
+ @resource.valid?.should be_false
144
+ @resource.errors.full_messages.should == ["Domain attr is too long (maximum is 63 characters)"]
145
+ end
146
+
147
+
148
+ it "should return ok" do
149
+ ["test.com", "hey.com", "dynamicadvisorsgroup.com", "advisorsexcel.com"].each do |value|
150
+ @resource.domain_attr = value
151
+ @resource.valid?.should be_true
152
+ end
153
+ end
154
+ end
155
+
156
+ describe "#zipcode" do
157
+ it "should return error" do
158
+ ["5555", "5555555555","-99999"].each do |value|
159
+ @resource.zipcode_attr = value
160
+ @resource.valid?.should be_false
161
+ @resource.errors.full_messages.should == ["Zipcode attr must contain 5 or 9 numbers"]
162
+ end
163
+ end
164
+
165
+ it "should return ok" do
166
+ ["11111", "333333333"].each do |value|
167
+ @resource.zipcode_attr = value
168
+ @resource.valid?.should be_true
169
+ end
170
+ end
171
+ end
172
+
173
+
174
+ describe "#middle_initial" do
175
+ it "should return error" do
176
+ ["k c", "55555", "55555-5555", "55555 5555", "55555.5555", "(888)88-9999", " ,-99999"].each do |value|
177
+ @resource.middle_initial_attr = value
178
+ @resource.valid?.should be_false
179
+ @resource.errors.full_messages.should == ["Middle initial attr accepts only one letter"]
180
+ end
181
+ end
182
+
183
+ it "should return ok" do
184
+ ["a", "A"].each do |value|
185
+ @resource.middle_initial_attr = value
186
+ @resource.valid?.should be_true
187
+ end
188
+ end
189
+ end
190
+
191
+ describe "#dollar" do
192
+ it "should return error" do
193
+ ["0.2222"].each do |value|
194
+ @resource.dollar_attr = value
195
+ @resource.valid?.should be_false
196
+ @resource.errors.full_messages.should == ["Dollar attr accepts only numeric characters, period, and negative sign"]
197
+ end
198
+ end
199
+
200
+ it "should return ok" do
201
+ ["0", "1", "100", "1000", "-1000.99"].each do |value|
202
+ @resource.dollar_attr = value
203
+ @resource.valid?.should be_true
204
+ end
205
+ end
206
+ end
207
+
208
+
209
+ describe "#positive dollar" do
210
+ it "should return error" do
211
+ ["-0.2", "-1"].each do |value|
212
+ @resource.positive_dollar_attr = value
213
+ @resource.valid?.should be_false
214
+ @resource.errors.full_messages.should == ["Positive dollar attr accepts only numeric characters, period", "Positive dollar attr must be greater than or equal to 0"]
215
+ end
216
+ end
217
+
218
+ it "should return ok" do
219
+ ["1", "100", "1000", "1000.99"].each do |value|
220
+ @resource.positive_dollar_attr = value
221
+ @resource.valid?.should be_true
222
+ end
223
+ end
224
+ end
225
+
226
+ describe "#percent" do
227
+ it "should return error" do
228
+ ["ewqr", "&"].each do |value|
229
+ @resource.percent_attr = value
230
+ @resource.valid?.should be_false
231
+ @resource.errors.full_messages.should == ["Percent attr accepts only numeric characters, period, negative sign, and must be equal/less/greater than +/- 100"]
232
+ end
233
+ end
234
+
235
+ it "should return ok" do
236
+ ["99.999", "0.001", "99"].each do |value|
237
+ @resource.percent_attr = value
238
+ @resource.valid?.should be_true
239
+ end
240
+ end
241
+ end
242
+
243
+
244
+ describe "#positive_percent" do
245
+ it "should return error" do
246
+ ["-100"].each do |value|
247
+ @resource.positive_percent_attr = value
248
+ @resource.valid?.should be_false
249
+ @resource.errors.full_messages.should == ["Positive percent attr accepts only numeric characters, period, and must be less than 100", "Positive percent attr must be greater than or equal to 0"]
250
+ end
251
+ end
252
+
253
+ it "should return ok" do
254
+ ["99.999", "0.001", "99"].each do |value|
255
+ @resource.positive_percent_attr = value
256
+ @resource.valid?.should be_true
257
+ end
258
+ end
259
+ end
260
+
261
+ describe "#url" do
262
+ it "should return error" do
263
+ ["ewqr", "&", "test.c", "www.test", "test.", "www-test.com"].each do |value|
264
+ @resource.url_attr = value
265
+ @resource.valid?.should be_false
266
+ @resource.errors.full_messages.should == ["Url attr web address isnt valid"]
267
+ end
268
+ end
269
+
270
+ it "should return ok" do
271
+ ["http://www.example.com", "http://fiance.example.com"].each do |value|
272
+ @resource.url_attr = value
273
+ @resource.valid?.should be_true
274
+ end
275
+ end
276
+ end
277
+
278
+ describe "#ssn" do
279
+ it "should return error" do
280
+ ["111-111-111"].each do |value|
281
+ @resource.ssn_attr = value
282
+ @resource.valid?.should be_false
283
+ @resource.errors.full_messages.should == ["Ssn attr is the wrong length (should be 9 characters)", "Ssn attr must be in the format 111-11-1111"]
284
+ end
285
+ end
286
+
287
+ it "should return ok" do
288
+ ["111111111"].each do |value|
289
+ @resource.ssn_attr = value
290
+ @resource.valid?.should be_true
291
+ end
292
+ end
293
+ end
294
+
295
+ describe "#taxid" do
296
+ it "should return error" do
297
+ @resource.taxid_attr = "ab-cdefgh"
298
+ @resource.valid?.should be_false
299
+ @resource.errors.full_messages.should == ["Taxid attr must be in the format 11-1111111"]
300
+ end
301
+
302
+ it "should return error is is less or more than 9 chars" do
303
+ ["111", "1111111111"].each do |value|
304
+ @resource.taxid_attr = value
305
+ @resource.valid?.should be_false
306
+ @resource.errors.full_messages.should == ["Taxid attr is the wrong length (should be 9 characters)", "Taxid attr must be in the format 11-1111111"]
307
+ end
308
+ end
309
+
310
+ it "should return ok" do
311
+ ["111111111"].each do |value|
312
+ @resource.taxid_attr = value
313
+ @resource.valid?.should be_true
314
+ end
315
+ end
316
+ end
317
+
318
+
319
+ describe "#age" do
320
+ it "should return error" do
321
+ @resource.age_attr = "111"
322
+ @resource.valid?.should be_false
323
+ @resource.errors.full_messages.should == ["Age attr must contain only 3 numbers and less than 110"]
324
+ end
325
+
326
+ it "should return ok" do
327
+ ["1", "10", "100"].each do |value|
328
+ @resource.age_attr = value
329
+ @resource.valid?.should be_true
330
+ end
331
+ end
332
+ end
333
+
334
+ describe "#number" do
335
+ it "should return error" do
336
+ @resource.number_attr = "aaa"
337
+ @resource.valid?.should be_false
338
+ @resource.errors.full_messages.should == ["Number attr accepts only numbers (0-9)"]
339
+ end
340
+
341
+ it "should return ok" do
342
+ ["1", "10", "100"].each do |value|
343
+ @resource.number_attr = value
344
+ @resource.valid?.should be_true
345
+ end
346
+ end
347
+ end
348
+ end
@@ -0,0 +1,35 @@
1
+ require "active_support"
2
+ require "active_record"
3
+ require "database_cleaner"
4
+
5
+ ENV['debug'] = 'test' unless ENV['debug']
6
+
7
+ # Establish DB Connection
8
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'database.yml')))
9
+ ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
10
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
11
+
12
+ # Load Test Schema into the Database
13
+ load(File.dirname(__FILE__) + "/db/schema.rb")
14
+
15
+ # Load in our code
16
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
17
+
18
+ require 'has_validated_attributes'
19
+
20
+ RSpec.configure do |config|
21
+
22
+ config.before(:suite) do
23
+ DatabaseCleaner.strategy = :transaction
24
+ DatabaseCleaner.clean_with(:truncation)
25
+ end
26
+
27
+ config.before(:each) do
28
+ DatabaseCleaner.start
29
+ end
30
+
31
+ config.after(:each) do
32
+ DatabaseCleaner.clean
33
+ end
34
+
35
+ end
data/spec/test.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ class Resource < ActiveRecord::Base
4
+ has_validated_attributes :username_attr => {:format => :username, :allow_blank => false}
5
+ end
6
+
7
+ describe "HasValidatedAttributes" do
8
+ before(:each) do
9
+ @resource = Resource.create(:username_attr => "testusername", :name_attr => "testname", :email_attr => "test@example.com",
10
+ :phone_number_attr => "1111111111", :phone_extension_attr => "111111", :domain_attr => "www.test.com", :zipcode_attr => "11111",
11
+ :middle_initial_attr => "A", :dollar_attr => "-11", :positive_dollar_attr => "1", :percent_attr => "12",
12
+ :positive_percent_attr => "99", :url_attr => "http://www.google.com", :ssn_attr => "111111111", :taxid_attr => "111111111",
13
+ :number_attr => "1")
14
+ end
15
+
16
+ describe "#username" do
17
+ it "should return error" do
18
+ [">*,.<><", "<<< test", "Kansas City", "-- Hey --", "& youuuu", "21 Jump", ""].each do |value|
19
+ @resource.username_attr = value
20
+ @resource.valid?.should be_false
21
+ @resource.errors.full_messages.should == ["Username attr use only letters, numbers, and .-_@ please."]
22
+ end
23
+ end
24
+ end
25
+
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_validated_attributes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Kyle Ginavan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-05-18 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: activerecord
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 3.1.0
35
+ type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: sqlite3
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: database_cleaner
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id004
59
+ description: has_validated_attributes is a Ruby on Rails gem that lets you validate your fields.
60
+ email: kylejginavan@gmail.com
61
+ executables: []
62
+
63
+ extensions: []
64
+
65
+ extra_rdoc_files: []
66
+
67
+ files:
68
+ - .gitignore
69
+ - .rspec
70
+ - .travis.yml
71
+ - Gemfile
72
+ - README.rdoc
73
+ - Rakefile
74
+ - has_validated_attributes.gemspec
75
+ - lib/has_validated_attributes.rb
76
+ - lib/version.rb
77
+ - spec/db/database.yml
78
+ - spec/db/schema.rb
79
+ - spec/db/test.rb
80
+ - spec/db/test.sqlite3
81
+ - spec/has_validate_fields_spec.rb
82
+ - spec/spec_helper.rb
83
+ - spec/test.rb
84
+ homepage: https://github.com/kylejginavan/has_validated_attributes
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project: has_validated_attributes
107
+ rubygems_version: 1.8.15
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Ruby on Rails gem for validate data prior to save
111
+ test_files:
112
+ - spec/db/database.yml
113
+ - spec/db/schema.rb
114
+ - spec/db/test.rb
115
+ - spec/db/test.sqlite3
116
+ - spec/has_validate_fields_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/test.rb