format_validators 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/CHANGELOG +10 -0
  2. data/Gemfile +14 -1
  3. data/Gemfile.lock +134 -8
  4. data/VERSION +1 -1
  5. data/app/validators/ssn_format_validator.rb +11 -0
  6. data/format_validators.gemspec +125 -0
  7. data/lib/format_validators/railtie.rb +1 -1
  8. data/spec/dummy/Rakefile +7 -0
  9. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  10. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  11. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  12. data/spec/dummy/config.ru +4 -0
  13. data/spec/dummy/config/application.rb +45 -0
  14. data/spec/dummy/config/boot.rb +10 -0
  15. data/spec/dummy/config/database.yml +22 -0
  16. data/spec/dummy/config/environment.rb +5 -0
  17. data/spec/dummy/config/environments/development.rb +26 -0
  18. data/spec/dummy/config/environments/production.rb +49 -0
  19. data/spec/dummy/config/environments/test.rb +35 -0
  20. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  21. data/spec/dummy/config/initializers/inflections.rb +10 -0
  22. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  23. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  24. data/spec/dummy/config/initializers/session_store.rb +8 -0
  25. data/spec/dummy/config/locales/en.yml +5 -0
  26. data/spec/dummy/config/routes.rb +58 -0
  27. data/spec/dummy/db/test.sqlite3 +0 -0
  28. data/spec/dummy/log/development.log +0 -0
  29. data/spec/dummy/log/production.log +0 -0
  30. data/spec/dummy/log/server.log +0 -0
  31. data/spec/dummy/log/test.log +99 -0
  32. data/spec/dummy/public/404.html +26 -0
  33. data/spec/dummy/public/422.html +26 -0
  34. data/spec/dummy/public/500.html +26 -0
  35. data/spec/dummy/public/favicon.ico +0 -0
  36. data/spec/dummy/public/javascripts/application.js +2 -0
  37. data/spec/dummy/public/javascripts/controls.js +965 -0
  38. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  39. data/spec/dummy/public/javascripts/effects.js +1123 -0
  40. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  41. data/spec/dummy/public/javascripts/rails.js +191 -0
  42. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  43. data/spec/dummy/script/rails +6 -0
  44. data/spec/format_validators_spec.rb +0 -3
  45. data/spec/spec_helper.rb +30 -8
  46. data/spec/validators/ssn_format_validator_spec.rb +99 -0
  47. metadata +148 -9
@@ -0,0 +1,191 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+
176
+ Ajax.Responders.register({
177
+ onCreate: function(request) {
178
+ var csrf_meta_tag = $$('meta[name=csrf-token]')[0];
179
+
180
+ if (csrf_meta_tag) {
181
+ var header = 'X-CSRF-Token',
182
+ token = csrf_meta_tag.readAttribute('content');
183
+
184
+ if (!request.options.requestHeaders) {
185
+ request.options.requestHeaders = {};
186
+ }
187
+ request.options.requestHeaders[header] = token;
188
+ }
189
+ }
190
+ });
191
+ })();
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -1,7 +1,4 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "FormatValidators" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
4
  end
@@ -1,12 +1,34 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'rspec'
4
- require 'format_validators'
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
5
3
 
6
- # Requires supporting files with custom matchers and macros, etc,
7
- # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
4
+ require 'ruby-debug'
5
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
6
+ require "rails/test_help"
7
+ require "rspec/rails"
8
+
9
+ ActionMailer::Base.delivery_method = :test
10
+ ActionMailer::Base.perform_deliveries = true
11
+ ActionMailer::Base.default_url_options[:host] = "test.com"
12
+
13
+ Rails.backtrace_cleaner.remove_silencers!
14
+
15
+ # Configure capybara for integration testing
16
+ require "capybara/rails"
17
+ Capybara.default_driver = :rack_test
18
+ Capybara.default_selector = :css
19
+
20
+ # Run any available migration
21
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
22
+
23
+ # Load support files
24
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
9
25
 
10
26
  RSpec.configure do |config|
11
-
27
+ # Remove this line if you don't want RSpec's should and should_not
28
+ # methods or matchers
29
+ require 'rspec/expectations'
30
+ config.include RSpec::Matchers
31
+
32
+ # == Mock Framework
33
+ config.mock_with :rspec
12
34
  end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ class BasicRecord
4
+ def initialize
5
+ @errors = {:ssn => []}
6
+ end
7
+
8
+ def errors
9
+ @errors
10
+ end
11
+ end
12
+
13
+ describe SsnFormatValidator do
14
+ describe ".validate_each" do
15
+ before(:each) do
16
+ @options = {:attributes => {}}
17
+ @validator = SsnFormatValidator.new(@options)
18
+ @record = BasicRecord.new
19
+ end
20
+
21
+ it "should validate the format for ###-##-####" do
22
+ @record.errors[:ssn].should_not_receive("<<")
23
+ @validator.validate_each(@record, :ssn, "123-45-6789")
24
+ end
25
+
26
+ it "should validate the format for #########" do
27
+ @record.errors[:ssn].should_not_receive("<<")
28
+ @validator.validate_each(@record, :ssn, "123456789")
29
+ end
30
+
31
+ it "should add a record to the errors hash if the validation fails" do
32
+ @record.errors[:ssn].should_receive("<<")
33
+ @validator.validate_each(@record, :ssn, "12345")
34
+ end
35
+
36
+ it "should have a default error message" do
37
+ @record.errors[:ssn].should_receive("<<").with("Ssn does not match the format ###-##-####.")
38
+ @validator.validate_each(@record, :ssn, "12345")
39
+ end
40
+
41
+ describe "bad ssn formats" do
42
+ before(:each) do
43
+ @record.errors[:ssn].should_receive("<<")
44
+ end
45
+
46
+ after(:each) do
47
+ @validator.validate_each(@record, :ssn, @value)
48
+ end
49
+
50
+ it "should not be valid if of the format 2-2-4" do
51
+ @value = "11-11-1111"
52
+ end
53
+
54
+ it "should not be valid if of the format 4-2-4" do
55
+ @value = "1111-11-1111"
56
+ end
57
+
58
+ it "should not be valid if of the format 4-2-4" do
59
+ @value = "1111-11-1111"
60
+ end
61
+
62
+ it "should not be valid if of the format 3-1-4" do
63
+ @value = "111-1-1111"
64
+ end
65
+
66
+ it "should not be valid if of the format 3-3-4" do
67
+ @value = "111-111-1111"
68
+ end
69
+
70
+ it "should not be valid if of the format 3-2-3" do
71
+ @value = "111-11-111"
72
+ end
73
+
74
+ it "should not be valid if of the format 3-2-5" do
75
+ @value = "111-11-11111"
76
+ end
77
+
78
+ it "should not be valid if of the format 8" do
79
+ @value = "11111111"
80
+ end
81
+
82
+ it "should not be valid if of the format 10" do
83
+ @value = "1111111111"
84
+ end
85
+
86
+ it "should not be valid if it is not numbers" do
87
+ @value = "aaaaaaaaa"
88
+ end
89
+
90
+ it "should not be valid if it is not numbers and it has dashes" do
91
+ @value = "aaa-aa-aaaa"
92
+ end
93
+
94
+ it "should not be valid if of the format 3-6" do
95
+ @value = "111-111111"
96
+ end
97
+ end
98
+ end
99
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: format_validators
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeremiah Hemphill
@@ -13,19 +13,41 @@ cert_chain: []
13
13
  date: 2012-03-14 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rspec
16
+ name: rails
17
17
  requirement: &id001 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
21
21
  - !ruby/object:Gem::Version
22
- version: 2.3.0
23
- type: :development
22
+ version: 3.0.7
23
+ type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: *id001
26
26
  - !ruby/object:Gem::Dependency
27
- name: bundler
27
+ name: capybara
28
28
  requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: sqlite3
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: &id004 !ruby/object:Gem::Requirement
29
51
  none: false
30
52
  requirements:
31
53
  - - ~>
@@ -33,10 +55,10 @@ dependencies:
33
55
  version: 1.0.0
34
56
  type: :development
35
57
  prerelease: false
36
- version_requirements: *id002
58
+ version_requirements: *id004
37
59
  - !ruby/object:Gem::Dependency
38
60
  name: jeweler
39
- requirement: &id003 !ruby/object:Gem::Requirement
61
+ requirement: &id005 !ruby/object:Gem::Requirement
40
62
  none: false
41
63
  requirements:
42
64
  - - ~>
@@ -44,7 +66,84 @@ dependencies:
44
66
  version: 1.6.4
45
67
  type: :development
46
68
  prerelease: false
47
- version_requirements: *id003
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: ruby-debug19
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: &id007 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: 2.6.0
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *id007
92
+ - !ruby/object:Gem::Dependency
93
+ name: rspec-rails
94
+ requirement: &id008 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: 2.6.1
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *id008
103
+ - !ruby/object:Gem::Dependency
104
+ name: shoulda
105
+ requirement: &id009 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 3.0.0
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: *id009
114
+ - !ruby/object:Gem::Dependency
115
+ name: ruby_parser
116
+ requirement: &id010 !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ~>
120
+ - !ruby/object:Gem::Version
121
+ version: 2.3.1
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: *id010
125
+ - !ruby/object:Gem::Dependency
126
+ name: ZenTest
127
+ requirement: &id011 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: "0"
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: *id011
136
+ - !ruby/object:Gem::Dependency
137
+ name: autotest-rails
138
+ requirement: &id012 !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: "0"
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: *id012
48
147
  description: Complex format validators
49
148
  email: jeremiah@cloudspace.com
50
149
  executables: []
@@ -57,17 +156,57 @@ extra_rdoc_files:
57
156
  files:
58
157
  - .document
59
158
  - .rspec
159
+ - CHANGELOG
60
160
  - Gemfile
61
161
  - Gemfile.lock
62
162
  - LICENSE.txt
63
163
  - README.rdoc
64
164
  - Rakefile
65
165
  - VERSION
166
+ - app/validators/ssn_format_validator.rb
167
+ - format_validators.gemspec
66
168
  - lib/format_validators.rb
67
169
  - lib/format_validators/engine.rb
68
170
  - lib/format_validators/railtie.rb
171
+ - spec/dummy/Rakefile
172
+ - spec/dummy/app/controllers/application_controller.rb
173
+ - spec/dummy/app/helpers/application_helper.rb
174
+ - spec/dummy/app/views/layouts/application.html.erb
175
+ - spec/dummy/config.ru
176
+ - spec/dummy/config/application.rb
177
+ - spec/dummy/config/boot.rb
178
+ - spec/dummy/config/database.yml
179
+ - spec/dummy/config/environment.rb
180
+ - spec/dummy/config/environments/development.rb
181
+ - spec/dummy/config/environments/production.rb
182
+ - spec/dummy/config/environments/test.rb
183
+ - spec/dummy/config/initializers/backtrace_silencers.rb
184
+ - spec/dummy/config/initializers/inflections.rb
185
+ - spec/dummy/config/initializers/mime_types.rb
186
+ - spec/dummy/config/initializers/secret_token.rb
187
+ - spec/dummy/config/initializers/session_store.rb
188
+ - spec/dummy/config/locales/en.yml
189
+ - spec/dummy/config/routes.rb
190
+ - spec/dummy/db/test.sqlite3
191
+ - spec/dummy/log/development.log
192
+ - spec/dummy/log/production.log
193
+ - spec/dummy/log/server.log
194
+ - spec/dummy/log/test.log
195
+ - spec/dummy/public/404.html
196
+ - spec/dummy/public/422.html
197
+ - spec/dummy/public/500.html
198
+ - spec/dummy/public/favicon.ico
199
+ - spec/dummy/public/javascripts/application.js
200
+ - spec/dummy/public/javascripts/controls.js
201
+ - spec/dummy/public/javascripts/dragdrop.js
202
+ - spec/dummy/public/javascripts/effects.js
203
+ - spec/dummy/public/javascripts/prototype.js
204
+ - spec/dummy/public/javascripts/rails.js
205
+ - spec/dummy/public/stylesheets/.gitkeep
206
+ - spec/dummy/script/rails
69
207
  - spec/format_validators_spec.rb
70
208
  - spec/spec_helper.rb
209
+ - spec/validators/ssn_format_validator_spec.rb
71
210
  homepage: http://github.com/jeremiahishere/format_validators
72
211
  licenses:
73
212
  - MIT
@@ -81,7 +220,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
220
  requirements:
82
221
  - - ">="
83
222
  - !ruby/object:Gem::Version
84
- hash: -2846784509835006756
223
+ hash: 4321630142708735267
85
224
  segments:
86
225
  - 0
87
226
  version: "0"