client_side_validations 2.9.9 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. data/client_side_validations.gemspec +45 -0
  2. data/javascript/rails.validations.js +380 -0
  3. data/lib/client_side_validations.rb +5 -57
  4. data/lib/client_side_validations/action_view.rb +13 -0
  5. data/lib/client_side_validations/action_view/form_builder.rb +57 -0
  6. data/lib/client_side_validations/action_view/form_helper.rb +58 -0
  7. data/lib/client_side_validations/action_view/form_tag_helper.rb +12 -0
  8. data/lib/client_side_validations/active_model.rb +46 -0
  9. data/lib/client_side_validations/active_model/acceptance.rb +10 -0
  10. data/lib/client_side_validations/active_model/exclusion.rb +15 -0
  11. data/lib/client_side_validations/active_model/format.rb +10 -0
  12. data/lib/client_side_validations/active_model/inclusion.rb +15 -0
  13. data/lib/client_side_validations/active_model/length.rb +22 -0
  14. data/lib/client_side_validations/active_model/numericality.rb +26 -0
  15. data/lib/client_side_validations/active_model/presence.rb +10 -0
  16. data/lib/client_side_validations/active_record.rb +11 -0
  17. data/lib/client_side_validations/active_record/middleware.rb +25 -0
  18. data/lib/client_side_validations/active_record/uniqueness.rb +24 -0
  19. data/lib/client_side_validations/core_ext.rb +3 -0
  20. data/lib/client_side_validations/core_ext/range.rb +10 -0
  21. data/lib/client_side_validations/core_ext/regexp.rb +14 -0
  22. data/lib/client_side_validations/formtastic.rb +21 -0
  23. data/lib/client_side_validations/middleware.rb +83 -0
  24. data/lib/client_side_validations/mongoid.rb +9 -0
  25. data/lib/client_side_validations/mongoid/middleware.rb +20 -0
  26. data/lib/client_side_validations/mongoid/uniqueness.rb +24 -0
  27. data/lib/client_side_validations/simple_form.rb +24 -0
  28. data/lib/client_side_validations/version.rb +3 -0
  29. data/lib/generators/client_side_validations/install_generator.rb +22 -0
  30. data/lib/generators/templates/README +7 -0
  31. data/lib/generators/templates/client_side_validations.rb +14 -0
  32. data/test/action_view/cases/helper.rb +152 -0
  33. data/test/action_view/cases/test_helpers.rb +222 -0
  34. data/test/action_view/cases/test_legacy_helpers.rb +150 -0
  35. data/test/action_view/models.rb +3 -0
  36. data/test/action_view/models/comment.rb +35 -0
  37. data/test/action_view/models/post.rb +35 -0
  38. data/test/active_model/cases/helper.rb +4 -0
  39. data/test/active_model/cases/test_acceptance_validator.rb +16 -0
  40. data/test/active_model/cases/test_base.rb +11 -0
  41. data/test/active_model/cases/test_confirmation_validator.rb +16 -0
  42. data/test/active_model/cases/test_exclusion_validator.rb +20 -0
  43. data/test/active_model/cases/test_format_validator.rb +21 -0
  44. data/test/active_model/cases/test_inclusion_validator.rb +21 -0
  45. data/test/active_model/cases/test_length_validator.rb +61 -0
  46. data/test/active_model/cases/test_numericality_validator.rb +46 -0
  47. data/test/active_model/cases/test_presence_validator.rb +16 -0
  48. data/test/active_model/cases/test_validations.rb +132 -0
  49. data/test/active_model/models/person.rb +12 -0
  50. data/test/active_record/cases/helper.rb +12 -0
  51. data/test/active_record/cases/test_base.rb +11 -0
  52. data/test/active_record/cases/test_middleware.rb +150 -0
  53. data/test/active_record/cases/test_uniqueness_validator.rb +31 -0
  54. data/test/active_record/models/guid.rb +7 -0
  55. data/test/active_record/models/user.rb +10 -0
  56. data/test/base_helper.rb +6 -0
  57. data/test/core_ext/cases/test_core_ext.rb +45 -0
  58. data/test/formtastic/cases/helper.rb +2 -0
  59. data/test/formtastic/cases/test_form_builder.rb +11 -0
  60. data/test/formtastic/cases/test_form_helper.rb +22 -0
  61. data/test/generators/cases/test_install_generator.rb +15 -0
  62. data/test/javascript/config.ru +3 -0
  63. data/test/javascript/public/test/callbacks/elementAfter.js +53 -0
  64. data/test/javascript/public/test/callbacks/elementBefore.js +53 -0
  65. data/test/javascript/public/test/callbacks/elementFail.js +69 -0
  66. data/test/javascript/public/test/callbacks/elementPass.js +69 -0
  67. data/test/javascript/public/test/callbacks/formAfter.js +44 -0
  68. data/test/javascript/public/test/callbacks/formBefore.js +44 -0
  69. data/test/javascript/public/test/callbacks/formFail.js +50 -0
  70. data/test/javascript/public/test/callbacks/formPass.js +49 -0
  71. data/test/javascript/public/test/form_builders/validateForm.js +65 -0
  72. data/test/javascript/public/test/form_builders/validateFormtastic.js +51 -0
  73. data/test/javascript/public/test/form_builders/validateSimpleForm.js +54 -0
  74. data/test/javascript/public/test/settings.js +15 -0
  75. data/test/javascript/public/test/validateElement.js +138 -0
  76. data/test/javascript/public/test/validators/acceptance.js +42 -0
  77. data/test/javascript/public/test/validators/confirmation.js +25 -0
  78. data/test/javascript/public/test/validators/exclusion.js +41 -0
  79. data/test/javascript/public/test/validators/format.js +27 -0
  80. data/test/javascript/public/test/validators/inclusion.js +42 -0
  81. data/test/javascript/public/test/validators/length.js +70 -0
  82. data/test/javascript/public/test/validators/numericality.js +135 -0
  83. data/test/javascript/public/test/validators/presence.js +15 -0
  84. data/test/javascript/public/test/validators/uniqueness.js +88 -0
  85. data/test/javascript/public/vendor/jquery.metadata.js +122 -0
  86. data/test/javascript/public/vendor/qunit.css +196 -0
  87. data/test/javascript/public/vendor/qunit.js +1374 -0
  88. data/test/javascript/server.rb +78 -0
  89. data/test/javascript/views/index.erb +20 -0
  90. data/test/javascript/views/layout.erb +21 -0
  91. data/test/middleware/cases/helper.rb +15 -0
  92. data/test/middleware/cases/test_middleware.rb +8 -0
  93. data/test/mongoid/cases/helper.rb +16 -0
  94. data/test/mongoid/cases/test_base.rb +15 -0
  95. data/test/mongoid/cases/test_middleware.rb +68 -0
  96. data/test/mongoid/cases/test_uniqueness_validator.rb +31 -0
  97. data/test/mongoid/models/book.rb +8 -0
  98. data/test/simple_form/cases/helper.rb +2 -0
  99. data/test/simple_form/cases/test_form_builder.rb +14 -0
  100. data/test/simple_form/cases/test_form_helper.rb +22 -0
  101. metadata +230 -111
  102. data/LICENSE +0 -24
  103. data/README.markdown +0 -132
  104. data/generators/client_side_validations/client_side_validations_generator.rb +0 -17
  105. data/javascript/lib/client_side_validations.js +0 -93
  106. data/javascript/lib/jquery-validation.js +0 -1146
  107. data/lib/client_side_validations/adapters/action_view.rb +0 -153
  108. data/lib/client_side_validations/adapters/active_model.rb +0 -137
  109. data/lib/client_side_validations/adapters/orm_base.rb +0 -89
  110. data/lib/client_side_validations/orm.rb +0 -226
  111. data/lib/client_side_validations/rails.rb +0 -11
  112. data/lib/client_side_validations/template.rb +0 -3
  113. data/lib/generators/client_side_validations_generator.rb +0 -15
@@ -0,0 +1,2 @@
1
+ require 'formtastic'
2
+ require 'client_side_validations/formtastic'
@@ -0,0 +1,11 @@
1
+ require 'formtastic/cases/helper'
2
+
3
+ class ClientSideValidations::Formtastic::SemanticFormBuilderTest < Test::Unit::TestCase
4
+ def test_client_side_form_js_hash
5
+ expected = {
6
+ :type => 'Formtastic::SemanticFormBuilder',
7
+ :inline_error_class => 'inline-errors'
8
+ }
9
+ assert_equal expected, Formtastic::SemanticFormBuilder.client_side_form_js_hash(nil, nil)
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ require 'action_view/cases/helper'
2
+ require 'formtastic/cases/helper'
3
+
4
+ class ClientSideValidations::Formtastic::FormHelperTest < ActionView::TestCase
5
+ include ActionViewTestSetup
6
+ include Formtastic::SemanticFormHelper
7
+
8
+ def client_side_form_js_variable_helper
9
+ ""
10
+ end
11
+
12
+ def test_semantic_form_for
13
+ semantic_form_for(@post, :validate => true) do |f|
14
+ concat f.input(:cost)
15
+ end
16
+
17
+ expected = %{<form accept-charset="UTF-8" action="/posts/123" class="formtastic post" data-validate="true" id="edit_post_123" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /></div><li class="string required" id="post_cost_input"><label for="post_cost">Cost<abbr title="required">*</abbr></label><input data-validators="{&quot;presence&quot;:{&quot;message&quot;:&quot;can't be blank&quot;}}" id="post_cost" name="post[cost]" type="text" /></li></form><script>var edit_post_123 = {"type":"Formtastic::SemanticFormBuilder","inline_error_class":"inline-errors"};</script>}
18
+ assert_equal expected, output_buffer
19
+ end
20
+
21
+ end
22
+
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/test_case'
2
+ require 'generators/client_side_validations/install_generator'
3
+
4
+ class InstallGeneratorTest < Rails::Generators::TestCase
5
+ tests ClientSideValidations::Generators::InstallGenerator
6
+ destination File.expand_path('../../tmp', __FILE__)
7
+ setup :prepare_destination
8
+
9
+ test 'Assert all files are properly created' do
10
+ run_generator
11
+ assert_file 'config/initializers/client_side_validations.rb'
12
+ assert_file 'public/javascripts/rails.validations.js'
13
+ end
14
+ end
15
+
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path('..', __FILE__)
2
+ require 'server'
3
+ run Sinatra::Application
@@ -0,0 +1,53 @@
1
+ module('Element Validate After Callback', {
2
+ setup: function() {
3
+ new_user = {
4
+ type: 'ActionView::Helpers::FormBuilder',
5
+ input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
6
+ label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>'
7
+ }
8
+
9
+ $('#qunit-fixture')
10
+ .append($('<span id="result" />'))
11
+ .append($('<form />', {
12
+ action: '/users',
13
+ 'data-validate': true,
14
+ method: 'post',
15
+ id: 'new_user'
16
+ }))
17
+ .find('form')
18
+ .append($('<input />', {
19
+ name: 'user[name]',
20
+ id: 'user_name',
21
+ 'data-validators': '{presence:{message: "must be present"}}',
22
+ type: 'text'
23
+ }))
24
+ .append($('<label for="user_name">Name</label>'));
25
+
26
+ clientSideValidations.callbacks.element.after = function(element, message) {
27
+ $('#result').text('Element Validate After ' + element.attr('id'));
28
+ }
29
+ $('form#new_user').validate();
30
+ },
31
+ teardown: function() {
32
+ clientSideValidations.callbacks.element.after = function(element, eventData) {}
33
+ }
34
+ });
35
+
36
+ test('runs callback when form element validate', function() {
37
+ var input = $('input');
38
+
39
+ equal($('#result').text(), '');
40
+
41
+ input.trigger('focusout');
42
+ equal($('#result').text(), 'Element Validate After user_name');
43
+ });
44
+
45
+ test('runs callback when form validates', function() {
46
+ var form = $('form'), input = form.find('input');
47
+
48
+ equal($('#result').text(), '');
49
+
50
+ form.submit();
51
+ equal($('#result').text(), 'Element Validate After user_name');
52
+ });
53
+
@@ -0,0 +1,53 @@
1
+ module('Element Validate Before Callback', {
2
+ setup: function() {
3
+ new_user = {
4
+ type: 'ActionView::Helpers::FormBuilder',
5
+ input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
6
+ label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>'
7
+ }
8
+
9
+ $('#qunit-fixture')
10
+ .append($('<span id="result" />'))
11
+ .append($('<form />', {
12
+ action: '/users',
13
+ 'data-validate': true,
14
+ method: 'post',
15
+ id: 'new_user'
16
+ }))
17
+ .find('form')
18
+ .append($('<input />', {
19
+ name: 'user[name]',
20
+ id: 'user_name',
21
+ 'data-validators': '{presence:{message: "must be present"}}',
22
+ type: 'text'
23
+ }))
24
+ .append($('<label for="user_name">Name</label>'));
25
+
26
+ clientSideValidations.callbacks.element.before = function(element) {
27
+ $('#result').text('Element Validate Before ' + element.attr('id'));
28
+ }
29
+ $('form#new_user').validate();
30
+ },
31
+ teardown: function() {
32
+ clientSideValidations.callbacks.element.before = function(element, eventData) {}
33
+ }
34
+ });
35
+
36
+ test('runs callback when form element validate', function() {
37
+ var input = $('input');
38
+
39
+ equal($('#result').text(), '');
40
+
41
+ input.trigger('focusout');
42
+ equal($('#result').text(), 'Element Validate Before user_name');
43
+ });
44
+
45
+ test('runs callback when form validates', function() {
46
+ var form = $('form'), input = form.find('input');
47
+
48
+ equal($('#result').text(), '');
49
+
50
+ form.submit();
51
+ equal($('#result').text(), 'Element Validate Before user_name');
52
+ });
53
+
@@ -0,0 +1,69 @@
1
+ module('Element Validate Fail Callback', {
2
+ setup: function() {
3
+ new_user = {
4
+ type: 'ActionView::Helpers::FormBuilder',
5
+ input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
6
+ label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>'
7
+ }
8
+
9
+ $('#qunit-fixture')
10
+ .append($('<span id="result" />'))
11
+ .append($('<form />', {
12
+ action: '/users',
13
+ 'data-validate': true,
14
+ method: 'post',
15
+ id: 'new_user'
16
+ }))
17
+ .find('form')
18
+ .append($('<input />', {
19
+ name: 'user[name]',
20
+ id: 'user_name',
21
+ 'data-validators': '{presence:{message: "must be present"}}',
22
+ type: 'text'
23
+ }))
24
+ .append($('<label for="user_name">Name</label>'));
25
+
26
+ clientSideValidations.callbacks.element.fail = function(element, message) {
27
+ $('#result').text('Element Validate Fail ' + element.attr('id') + ' ' + message);
28
+ }
29
+ $('form#new_user').validate();
30
+ },
31
+ teardown: function() {
32
+ clientSideValidations.callbacks.element.fail = function(element, message, callback) { callback(); }
33
+ }
34
+ });
35
+
36
+ test('runs callback when form element validate', function() {
37
+ var input = $('input');
38
+
39
+ equal($('#result').text(), '');
40
+
41
+ input.val('test')
42
+ input.trigger('change');
43
+ input.trigger('focusout');
44
+ equal($('#result').text(), '');
45
+
46
+ input.val('')
47
+ input.trigger('change');
48
+ input.trigger('focusout');
49
+ equal($('#result').text(), 'Element Validate Fail user_name must be present');
50
+ });
51
+
52
+ test('runs callback when form validates', function() {
53
+ var form = $('form'), input = form.find('input');
54
+
55
+ equal($('#result').text(), '');
56
+
57
+ input.val('test')
58
+ input.trigger('change');
59
+ form.trigger('submit');
60
+
61
+ equal($('#result').text(), '');
62
+
63
+ input.val('')
64
+ input.trigger('change');
65
+ form.trigger('submit');
66
+
67
+ equal($('#result').text(), 'Element Validate Fail user_name must be present');
68
+ });
69
+
@@ -0,0 +1,69 @@
1
+ module('Element Validate Pass Callback', {
2
+ setup: function() {
3
+ new_user = {
4
+ type: 'ActionView::Helpers::FormBuilder',
5
+ input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
6
+ label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>'
7
+ }
8
+
9
+ $('#qunit-fixture')
10
+ .append($('<span id="result" />'))
11
+ .append($('<form />', {
12
+ action: '/users',
13
+ 'data-validate': true,
14
+ method: 'post',
15
+ id: 'new_user'
16
+ }))
17
+ .find('form')
18
+ .append($('<input />', {
19
+ name: 'user[name]',
20
+ id: 'user_name',
21
+ 'data-validators': '{presence:{message: "must be present"}}',
22
+ type: 'text'
23
+ }))
24
+ .append($('<label for="user_name">Name</label>'));
25
+
26
+ clientSideValidations.callbacks.element.pass = function(element) {
27
+ $('#result').text('Element Validate Pass ' + element.attr('id'));
28
+ }
29
+ $('form#new_user').validate();
30
+ },
31
+ teardown: function() {
32
+ clientSideValidations.callbacks.element.pass = function(element, callback) { callback() }
33
+ }
34
+ });
35
+
36
+ test('runs callback when form element validate', function() {
37
+ var input = $('input');
38
+
39
+ equal($('#result').text(), '');
40
+
41
+ input.val('')
42
+ input.trigger('change');
43
+ input.trigger('focusout');
44
+ equal($('#result').text(), '');
45
+
46
+ input.val('test')
47
+ input.trigger('change');
48
+ input.trigger('focusout');
49
+ equal($('#result').text(), 'Element Validate Pass user_name');
50
+ });
51
+
52
+ test('runs callback when form validates', function() {
53
+ var form = $('form'), input = form.find('input');
54
+
55
+ equal($('#result').text(), '');
56
+
57
+ input.val('')
58
+ input.trigger('change');
59
+ form.trigger('submit');
60
+
61
+ equal($('#result').text(), '');
62
+
63
+ input.val('test')
64
+ input.trigger('change');
65
+ form.trigger('submit');
66
+
67
+ equal($('#result').text(), 'Element Validate Pass user_name');
68
+ });
69
+
@@ -0,0 +1,44 @@
1
+ module('Form Validate After Callback', {
2
+ setup: function() {
3
+ new_user = {
4
+ type: 'ActionView::Helpers::FormBuilder',
5
+ input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
6
+ label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>'
7
+ }
8
+
9
+ $('#qunit-fixture')
10
+ .append($('<span id="result" />'))
11
+ .append($('<form />', {
12
+ action: '/users',
13
+ 'data-validate': true,
14
+ method: 'post',
15
+ id: 'new_user'
16
+ }))
17
+ .find('form')
18
+ .append($('<input />', {
19
+ name: 'user[name]',
20
+ id: 'user_name',
21
+ 'data-validators': '{presence:{message: "must be present"}}',
22
+ type: 'text'
23
+ }))
24
+ .append($('<label for="user_name">Name</label>'));
25
+
26
+ clientSideValidations.callbacks.form.after = function(form, message) {
27
+ $('#result').text('Form Validate After ' + form.attr('id'));
28
+ }
29
+ $('form#new_user').validate();
30
+ },
31
+ teardown: function() {
32
+ clientSideValidations.callbacks.form.after = function(form, eventData) {}
33
+ }
34
+ });
35
+
36
+ test('runs callback', function() {
37
+ var form = $('form'), input = form.find('input');
38
+
39
+ equal($('#result').text(), '');
40
+
41
+ form.submit();
42
+ equal($('#result').text(), 'Form Validate After new_user');
43
+ });
44
+
@@ -0,0 +1,44 @@
1
+ module('Form Validate Before Callback', {
2
+ setup: function() {
3
+ new_user = {
4
+ type: 'ActionView::Helpers::FormBuilder',
5
+ input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
6
+ label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>'
7
+ }
8
+
9
+ $('#qunit-fixture')
10
+ .append($('<span id="result" />'))
11
+ .append($('<form />', {
12
+ action: '/users',
13
+ 'data-validate': true,
14
+ method: 'post',
15
+ id: 'new_user'
16
+ }))
17
+ .find('form')
18
+ .append($('<input />', {
19
+ name: 'user[name]',
20
+ id: 'user_name',
21
+ 'data-validators': '{presence:{message: "must be present"}}',
22
+ type: 'text'
23
+ }))
24
+ .append($('<label for="user_name">Name</label>'));
25
+
26
+ clientSideValidations.callbacks.form.before = function(form, message) {
27
+ $('#result').text('Form Validate Before ' + form.attr('id'));
28
+ }
29
+ $('form#new_user').validate();
30
+ },
31
+ teardown: function() {
32
+ clientSideValidations.callbacks.form.before = function(form, eventData) {}
33
+ }
34
+ });
35
+
36
+ test('runs callback', function() {
37
+ var form = $('form'), input = form.find('input');
38
+
39
+ equal($('#result').text(), '');
40
+
41
+ form.submit();
42
+ equal($('#result').text(), 'Form Validate Before new_user');
43
+ });
44
+
@@ -0,0 +1,50 @@
1
+ module('Form Validate Fail Callback', {
2
+ setup: function() {
3
+ new_user = {
4
+ type: 'ActionView::Helpers::FormBuilder',
5
+ input_tag: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
6
+ label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>'
7
+ }
8
+
9
+ $('#qunit-fixture')
10
+ .append($('<span id="result" />'))
11
+ .append($('<form />', {
12
+ action: '/users',
13
+ 'data-validate': true,
14
+ method: 'post',
15
+ id: 'new_user'
16
+ }))
17
+ .find('form')
18
+ .append($('<input />', {
19
+ name: 'user[name]',
20
+ id: 'user_name',
21
+ 'data-validators': '{presence:{message: "must be present"}}',
22
+ type: 'text'
23
+ }))
24
+ .append($('<label for="user_name">Name</label>'));
25
+
26
+ clientSideValidations.callbacks.form.fail = function(form, message) {
27
+ $('#result').text('Form Validate Fail ' + form.attr('id'));
28
+ }
29
+ $('form#new_user').validate();
30
+ },
31
+ teardown: function() {
32
+ clientSideValidations.callbacks.form.fail = function(form, eventData) {}
33
+ }
34
+ });
35
+
36
+ test('runs callback', function() {
37
+ var form = $('form'), input = form.find('input');
38
+
39
+ equal($('#result').text(), '');
40
+
41
+ form.submit();
42
+ equal($('#result').text(), 'Form Validate Fail new_user');
43
+
44
+ $('#result').text('');
45
+ input.val('test');
46
+ input.trigger('change');
47
+ form.submit();
48
+ equal($('#result').text(), '');
49
+ });
50
+