nimboids-client_side_validations 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/client_side_validations.gemspec +45 -0
  2. data/javascript/rails.validations.js +393 -0
  3. data/lib/client_side_validations.rb +8 -0
  4. data/lib/client_side_validations/action_view.rb +13 -0
  5. data/lib/client_side_validations/action_view/form_builder.rb +64 -0
  6. data/lib/client_side_validations/action_view/form_helper.rb +69 -0
  7. data/lib/client_side_validations/action_view/form_tag_helper.rb +12 -0
  8. data/lib/client_side_validations/active_model.rb +52 -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 +26 -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 +26 -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 +237 -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 +151 -0
  49. data/test/active_model/models/person.rb +17 -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 +45 -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 +54 -0
  64. data/test/javascript/public/test/callbacks/elementBefore.js +54 -0
  65. data/test/javascript/public/test/callbacks/elementFail.js +70 -0
  66. data/test/javascript/public/test/callbacks/elementPass.js +70 -0
  67. data/test/javascript/public/test/callbacks/formAfter.js +45 -0
  68. data/test/javascript/public/test/callbacks/formBefore.js +45 -0
  69. data/test/javascript/public/test/callbacks/formFail.js +51 -0
  70. data/test/javascript/public/test/callbacks/formPass.js +50 -0
  71. data/test/javascript/public/test/form_builders/validateForm.js +66 -0
  72. data/test/javascript/public/test/form_builders/validateFormtastic.js +54 -0
  73. data/test/javascript/public/test/form_builders/validateSimpleForm.js +57 -0
  74. data/test/javascript/public/test/settings.js +15 -0
  75. data/test/javascript/public/test/validateElement.js +144 -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 +140 -0
  83. data/test/javascript/public/test/validators/presence.js +15 -0
  84. data/test/javascript/public/test/validators/uniqueness.js +89 -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 +44 -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 +435 -0
@@ -0,0 +1,41 @@
1
+ module('Exclusion options');
2
+
3
+ test('when value is not in the list', function() {
4
+ var element = $('<input type="text" />');
5
+ var options = { 'message': "failed validation", 'in': [1, 2, 3] };
6
+ element.val('4');
7
+ equal(clientSideValidations.validators.local.exclusion(element, options), undefined);
8
+ });
9
+
10
+ test('when value is not in the range', function() {
11
+ var element = $('<input type="text" />');
12
+ var options = { 'message': "failed validation", 'range': [1, 3] };
13
+ element.val('4');
14
+ equal(clientSideValidations.validators.local.exclusion(element, options), undefined);
15
+ });
16
+
17
+ test('when value is in the list', function() {
18
+ var element = $('<input type="text" />');
19
+ var options = { 'message': "failed validation", 'in': [1, 2, 3] };
20
+ element.val('1');
21
+ equal(clientSideValidations.validators.local.exclusion(element, options), "failed validation");
22
+ });
23
+
24
+ test('when value is in the range', function() {
25
+ var element = $('<input type="text" />');
26
+ var options = { 'message': "failed validation", 'range': [1, 3] };
27
+ element.val('1');
28
+ equal(clientSideValidations.validators.local.exclusion(element, options), "failed validation");
29
+ });
30
+
31
+ test('when allowing blank', function() {
32
+ var element = $('<input type="text" />');
33
+ var options = { 'message': "failed validation", 'in': [1, 2, 3], allow_blank: true };
34
+ equal(clientSideValidations.validators.local.exclusion(element, options), undefined);
35
+ });
36
+
37
+ test('when not allowing blank', function() {
38
+ var element = $('<input type="text" />');
39
+ var options = { 'message': "failed validation", 'in': [1, 2, 3] };
40
+ equal(clientSideValidations.validators.local.exclusion(element, options), "failed validation");
41
+ });
@@ -0,0 +1,27 @@
1
+ module('Format options');
2
+
3
+ test('when matching format', function() {
4
+ var element = $('<input type="text" />');
5
+ var options = { 'message': "failed validation", 'with': /\d+/ };
6
+ element.val('123');
7
+ equal(clientSideValidations.validators.local.format(element, options), undefined);
8
+ });
9
+
10
+ test('when not matching format', function() {
11
+ var element = $('<input type="text" />');
12
+ var options = { 'message': "failed validation", 'with': /\d+/ };
13
+ element.val('abc');
14
+ equal(clientSideValidations.validators.local.format(element, options), "failed validation");
15
+ });
16
+
17
+ test('when allowing blank', function() {
18
+ var element = $('<input type="text" />');
19
+ var options = { 'message': "failed validation", 'with': /\d+/, 'allow_blank': true };
20
+ equal(clientSideValidations.validators.local.format(element, options), undefined);
21
+ });
22
+
23
+ test('when not allowing blank', function() {
24
+ var element = $('<input type="text" />');
25
+ var options = { 'message': "failed validation", 'with': /\d+/ };
26
+ equal(clientSideValidations.validators.local.format(element, options), "failed validation");
27
+ });
@@ -0,0 +1,42 @@
1
+ module('Inclusion options');
2
+
3
+ test('when value is in the list', function() {
4
+ var element = $('<input type="text" />');
5
+ var options = { 'message': "failed validation", 'in': [1, 2, 3] };
6
+ element.val('1');
7
+ equal(clientSideValidations.validators.local.inclusion(element, options), undefined);
8
+ });
9
+
10
+ test('when value is in the range', function() {
11
+ var element = $('<input type="text" />');
12
+ var options = { 'message': "failed validation", 'range': [1, 3] };
13
+ element.val('1');
14
+ equal(clientSideValidations.validators.local.inclusion(element, options), undefined);
15
+ });
16
+
17
+ test('when value is not in the list', function() {
18
+ var element = $('<input type="text" />');
19
+ var options = { 'message': "failed validation", 'in': [1, 2, 3] };
20
+ element.val('4');
21
+ equal(clientSideValidations.validators.local.inclusion(element, options), "failed validation");
22
+ });
23
+
24
+ test('when value is not in the range', function() {
25
+ var element = $('<input type="text" />');
26
+ var options = { 'message': "failed validation", 'range': [1, 3] };
27
+ element.val('4');
28
+ equal(clientSideValidations.validators.local.inclusion(element, options), "failed validation");
29
+ });
30
+
31
+ test('when allowing blank', function() {
32
+ var element = $('<input type="text" />');
33
+ var options = { 'message': "failed validation", 'in': [1, 2, 3], allow_blank: true };
34
+ equal(clientSideValidations.validators.local.inclusion(element, options), undefined);
35
+ });
36
+
37
+ test('when not allowing blank', function() {
38
+ var element = $('<input type="text" />');
39
+ var options = { 'message': "failed validation", 'in': [1, 2, 3] };
40
+ equal(clientSideValidations.validators.local.inclusion(element, options), "failed validation");
41
+ });
42
+
@@ -0,0 +1,70 @@
1
+ module('Length options');
2
+
3
+ test('when allowed length is 3 and value length is 3', function() {
4
+ var element = $('<input type="text" />');
5
+ var options = { messages: { is: "failed validation" }, is: 3 };
6
+ element.val('123');
7
+ equal(clientSideValidations.validators.local.length(element, options), undefined);
8
+ });
9
+
10
+ test('when allowed length is 3 and value length is 4', function() {
11
+ var element = $('<input type="text" />');
12
+ var options = { messages: { is: "failed validation" }, is: 3 };
13
+ element.val('1234');
14
+ equal(clientSideValidations.validators.local.length(element, options), "failed validation");
15
+ });
16
+
17
+ test('when allowed length is 3 and value length is 2', function() {
18
+ var element = $('<input type="text" />');
19
+ var options = { messages: { is: "failed validation" }, is: 3 };
20
+ element.val('12');
21
+ equal(clientSideValidations.validators.local.length(element, options), "failed validation");
22
+ });
23
+
24
+ test('when allowing blank and allowed length is 3', function() {
25
+ var element = $('<input type="text" />');
26
+ var options = { messages: { is: "failed validation" }, is: 3, allow_blank: true };
27
+ equal(clientSideValidations.validators.local.length(element, options), undefined);
28
+ });
29
+
30
+ test('when allowing blank and allowed length is 3', function() {
31
+ var element = $('<input type="text" />');
32
+ var options = { messages: { is: "failed validation" }, is: 3 };
33
+ equal(clientSideValidations.validators.local.length(element, options), "failed validation");
34
+ });
35
+
36
+ test('when allowed length is 3 and a differnet tokenizer', function() {
37
+ var element = $('<input type="text" />');
38
+ element.val("one two three");
39
+ var options = { messages: { is: "failed validation" }, is: 3, js_tokenizer: "match(/\\w+/g)" };
40
+ equal(clientSideValidations.validators.local.length(element, options), undefined);
41
+ });
42
+
43
+ test('when allowed length minimum is 3 and value length is 3', function() {
44
+ var element = $('<input type="text" />');
45
+ var options = { messages: { is: "failed validation" }, is: 3 };
46
+ element.val('123');
47
+ equal(clientSideValidations.validators.local.length(element, options), undefined);
48
+ });
49
+
50
+ test('when allowed length minimum is 3 and value length is 2', function() {
51
+ var element = $('<input type="text" />');
52
+ var options = { messages: { minimum: "failed validation" }, minimum: 3 };
53
+ element.val('12');
54
+ equal(clientSideValidations.validators.local.length(element, options), "failed validation");
55
+ });
56
+
57
+ test('when allowed length maximum is 3 and value length is 3', function() {
58
+ var element = $('<input type="text" />');
59
+ var options = { messages: { is: "failed validation" }, is: 3 };
60
+ element.val('123');
61
+ equal(clientSideValidations.validators.local.length(element, options), undefined);
62
+ });
63
+
64
+ test('when allowed length maximum is 3 and value length is 4', function() {
65
+ var element = $('<input type="text" />');
66
+ var options = { messages: { maximum: "failed validation" }, maximum: 3 };
67
+ element.val('1234');
68
+ equal(clientSideValidations.validators.local.length(element, options), "failed validation");
69
+ });
70
+
@@ -0,0 +1,140 @@
1
+ module('Numericality options');
2
+
3
+ test('when value is a number', function() {
4
+ var element = $('<input type="text" />');
5
+ var options = { message: "failed validation" };
6
+ element.val('123');
7
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
8
+ });
9
+
10
+ test('when value is a decimal number', function() {
11
+ var element = $('<input type="text" />');
12
+ var options = { message: "failed validation" };
13
+ element.val('123.456');
14
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
15
+ });
16
+
17
+ test('when value is not a number', function() {
18
+ var element = $('<input type="text" />');
19
+ var options = { messages: { numericality: "failed validation" } };
20
+ element.val('abc123');
21
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
22
+ });
23
+
24
+ test('when only allowing integers and value is https://kerryb@github.com/nimboids/client_side_validations.gitinteger', function() {
25
+ var element = $('<input type="text" />');
26
+ var options = { messages: { only_integer: "failed validation" }, only_integer: true };
27
+ element.val('123');
28
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
29
+ });
30
+
31
+ test('when only allowing integers and value is not integer', function() {
32
+ var element = $('<input type="text" />');
33
+ var options = { messages: { only_integer: "failed validation" }, only_integer: true };
34
+ element.val('123.456');
35
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
36
+ });
37
+
38
+ test('when only allowing values greater than 10 and value is greater than 10', function() {
39
+ var element = $('<input type="text" />');
40
+ var options = { messages: { greater_than: "failed validation" }, greater_than: 10 };
41
+ element.val('11');
42
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
43
+ });
44
+
45
+ test('when only allowing values greater than 10 and value is 10', function() {
46
+ var element = $('<input type="text" />');
47
+ var options = { messages: { greater_than: "failed validation" }, greater_than: 10 };
48
+ element.val('10');
49
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
50
+ });
51
+
52
+ test('when only allowing values greater than or equal to 10 and value is 10', function() {
53
+ var element = $('<input type="text" />');
54
+ var options = { messages: { greater_than_or_equal_to: "failed validation" }, greater_than_or_equal_to: 10 };
55
+ element.val('10');
56
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
57
+ });
58
+
59
+ test('when only allowing values greater than or equal to 10 and value is 9', function() {
60
+ var element = $('<input type="text" />');
61
+ var options = { messages: { greater_than_or_equal_to: "failed validation" }, greater_than_or_equal_to: 10 };
62
+ element.val('9');
63
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
64
+ });
65
+
66
+ test('when only allowing values less than 10 and value is less than 10', function() {
67
+ var element = $('<input type="text" />');
68
+ var options = { messages: { less_than: "failed validation" }, less_than: 10 };
69
+ element.val('9');
70
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
71
+ });
72
+
73
+ test('when only allowing values less than 10 and value is 10', function() {
74
+ var element = $('<input type="text" />');
75
+ var options = { messages: { less_than: "failed validation" }, less_than: 10 };
76
+ element.val('10');
77
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
78
+ });
79
+
80
+ test('when only allowing values less than or equal to 10 and value is 10', function() {
81
+ var element = $('<input type="text" />');
82
+ var options = { messages: { less_than_or_equal_to: "failed validation" }, less_than_or_equal_to: 10 };
83
+ element.val('10');
84
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
85
+ });
86
+
87
+ test('when only allowing values less than or equal to 10 and value is 11', function() {
88
+ var element = $('<input type="text" />');
89
+ var options = { messages: { less_than_or_equal_to: "failed validation" }, less_than_or_equal_to: 10 };
90
+ element.val('11');
91
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
92
+ });
93
+
94
+ test('when only allowing values equal to 10 and value is 10', function() {
95
+ var element = $('<input type="text" />');
96
+ var options = { messages: { equal_to: "failed validation" }, equal_to: 10 };
97
+ element.val('10');
98
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
99
+ });
100
+
101
+ test('when only allowing values equal to 10 and value is 11', function() {
102
+ var element = $('<input type="text" />');
103
+ var options = { messages: { equal_to: "failed validation" }, equal_to: 10 };
104
+ element.val('11');
105
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
106
+ });
107
+
108
+ test('when only allowing odd values and the value is odd', function() {
109
+ var element = $('<input type="text" />');
110
+ var options = { messages: { odd: "failed validation" }, odd: true };
111
+ element.val('11');
112
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
113
+ });
114
+
115
+ test('when only allowing odd values and the value is even', function() {
116
+ var element = $('<input type="text" />');
117
+ var options = { messages: { odd: "failed validation" }, odd: true };
118
+ element.val('10');
119
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
120
+ });
121
+
122
+ test('when only allowing even values and the value is even', function() {
123
+ var element = $('<input type="text" />');
124
+ var options = { messages: { even: "failed validation" }, even: true };
125
+ element.val('10');
126
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
127
+ });
128
+
129
+ test('when only allowing even values and the value is odd', function() {
130
+ var element = $('<input type="text" />');
131
+ var options = { messages: { even: "failed validation" }, even: true };
132
+ element.val('11');
133
+ equal(clientSideValidations.validators.local.numericality(element, options), "failed validation");
134
+ });
135
+
136
+ test('when allowing blank', function() {
137
+ var element = $('<input type="text" />');
138
+ var options = { message: "failed validation", allow_blank: true };
139
+ equal(clientSideValidations.validators.local.numericality(element, options), undefined);
140
+ });
@@ -0,0 +1,15 @@
1
+ module('Presence options');
2
+
3
+ test('when value is not empty', function() {
4
+ var element = $('<input type="text" />');
5
+ var options = { message: "failed validation" };
6
+ element.val('not empty');
7
+ equal(clientSideValidations.validators.local.presence(element, options), undefined);
8
+ });
9
+
10
+ test('when value is empty', function() {
11
+ var element = $('<input type="text" />');
12
+ var options = { message: "failed validation" };
13
+ equal(clientSideValidations.validators.local.presence(element, options), "failed validation");
14
+ });
15
+
@@ -0,0 +1,89 @@
1
+ module('Uniqueness options', {
2
+ setup: function() {
3
+ new_user = {
4
+ type: 'ActionView::Helpers::FormBuilder',
5
+ input_tag: '<div class="field_with_errors"><span id="input_tag" /><label class="message"></label></div>',
6
+ label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>',
7
+ validators: {'user[email]':{"uniqueness":{"message": "must be unique", "scope":{name:"pass"}},"presence":{"message": "must be present"}}}
8
+ }
9
+
10
+ $('#qunit-fixture')
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
+ type: 'text'
22
+ }))
23
+ .append($('<input />', {
24
+ name: 'user[email]',
25
+ id: 'user_email',
26
+ 'data-validate': 'true',
27
+ type: 'text'
28
+ }))
29
+
30
+ $('form#new_user').validate();
31
+ }
32
+ });
33
+
34
+ test('when matching uniqueness on a non-nested resource', function() {
35
+ var element = $('<input type="text" name="user[email]"/>');
36
+ var options = { 'message': "failed validation" };
37
+ element.val('nottaken@test.com');
38
+ equal(clientSideValidations.validators.remote.uniqueness(element, options), undefined);
39
+ });
40
+
41
+ test('when matching uniqueness on a non-nested resource', function() {
42
+ var element = $('<input type="text" name="user[email]"/>');
43
+ var options = { 'message': "failed validation" };
44
+ element.val('taken@test.com');
45
+ equal(clientSideValidations.validators.remote.uniqueness(element, options), "failed validation");
46
+ });
47
+
48
+ test('when matching uniqueness on a nested singular resource', function() {
49
+ var element = $('<input type="text" name="profile[user_attributes][email]"/>');
50
+ var options = { 'message': "failed validation" };
51
+ element.val('nottaken@test.com');
52
+ equal(clientSideValidations.validators.remote.uniqueness(element, options), undefined);
53
+ });
54
+
55
+ test('when matching uniqueness on a nested singular resource', function() {
56
+ var element = $('<input type="text" name="profile[user_attributes][email]"/>');
57
+ var options = { 'message': "failed validation" };
58
+ element.val('taken@test.com');
59
+ equal(clientSideValidations.validators.remote.uniqueness(element, options), "failed validation");
60
+ });
61
+
62
+ test('when using scopes with no replacement', function() {
63
+ var element = $('<input type="text" name="person[age]" />');
64
+ var options = { 'message': "failed validation", 'with': /\d+/, 'scope': { 'name': 'test name' } };
65
+ element.val('test');
66
+ equal(clientSideValidations.validators.remote.uniqueness(element, options), "failed validation");
67
+ });
68
+
69
+ test('when using scopes with replacement', function() {
70
+ var element = $('<input type="text" name="person[age]" />');
71
+ var options = { 'message': "failed validation", 'with': /\d+/, 'scope': { 'name': 'test name' } };
72
+ element.val('test')
73
+ $('#qunit-fixture').append('<input type="text" name="person[name]" />').find('input[name="person[name]"]').val('other name');
74
+ equal(clientSideValidations.validators.remote.uniqueness(element, options), undefined);
75
+ });
76
+
77
+ test('when validating by scope and mixed focus order', function() {
78
+ var unique_element = $('#user_email'), scope_element = $('#user_name');
79
+ unique_element.val('free@test.com');
80
+ unique_element.trigger('change');
81
+ unique_element.trigger('focusout');
82
+ equal($('.message[for="user_email"]').text(), '');
83
+
84
+ scope_element.val('test name');
85
+ scope_element.trigger('change');
86
+ scope_element.trigger('focusout');
87
+ equal($('.message[for="user_email"]').text(), 'must be unique');
88
+ });
89
+
@@ -0,0 +1,122 @@
1
+ /*
2
+ * Metadata - jQuery plugin for parsing metadata from elements
3
+ *
4
+ * Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan
5
+ *
6
+ * Dual licensed under the MIT and GPL licenses:
7
+ * http://www.opensource.org/licenses/mit-license.php
8
+ * http://www.gnu.org/licenses/gpl.html
9
+ *
10
+ * Revision: $Id: jquery.metadata.js 4187 2007-12-16 17:15:27Z joern.zaefferer $
11
+ *
12
+ */
13
+
14
+ /**
15
+ * Sets the type of metadata to use. Metadata is encoded in JSON, and each property
16
+ * in the JSON will become a property of the element itself.
17
+ *
18
+ * There are three supported types of metadata storage:
19
+ *
20
+ * attr: Inside an attribute. The name parameter indicates *which* attribute.
21
+ *
22
+ * class: Inside the class attribute, wrapped in curly braces: { }
23
+ *
24
+ * elem: Inside a child element (e.g. a script tag). The
25
+ * name parameter indicates *which* element.
26
+ *
27
+ * The metadata for an element is loaded the first time the element is accessed via jQuery.
28
+ *
29
+ * As a result, you can define the metadata type, use $(expr) to load the metadata into the elements
30
+ * matched by expr, then redefine the metadata type and run another $(expr) for other elements.
31
+ *
32
+ * @name $.metadata.setType
33
+ *
34
+ * @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p>
35
+ * @before $.metadata.setType("class")
36
+ * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
37
+ * @desc Reads metadata from the class attribute
38
+ *
39
+ * @example <p id="one" class="some_class" data="{item_id: 1, item_label: 'Label'}">This is a p</p>
40
+ * @before $.metadata.setType("attr", "data")
41
+ * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
42
+ * @desc Reads metadata from a "data" attribute
43
+ *
44
+ * @example <p id="one" class="some_class"><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p>
45
+ * @before $.metadata.setType("elem", "script")
46
+ * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
47
+ * @desc Reads metadata from a nested script element
48
+ *
49
+ * @param String type The encoding type
50
+ * @param String name The name of the attribute to be used to get metadata (optional)
51
+ * @cat Plugins/Metadata
52
+ * @descr Sets the type of encoding to be used when loading metadata for the first time
53
+ * @type undefined
54
+ * @see metadata()
55
+ */
56
+
57
+ (function($) {
58
+
59
+ $.extend({
60
+ metadata : {
61
+ defaults : {
62
+ type: 'class',
63
+ name: 'metadata',
64
+ cre: /({.*})/,
65
+ single: 'metadata'
66
+ },
67
+ setType: function( type, name ){
68
+ this.defaults.type = type;
69
+ this.defaults.name = name;
70
+ },
71
+ get: function( elem, opts ){
72
+ var settings = $.extend({},this.defaults,opts);
73
+ // check for empty string in single property
74
+ if ( !settings.single.length ) settings.single = 'metadata';
75
+
76
+ var data = $.data(elem, settings.single);
77
+ // returned cached data if it already exists
78
+ if ( data ) return data;
79
+
80
+ data = "{}";
81
+
82
+ if ( settings.type == "class" ) {
83
+ var m = settings.cre.exec( elem.className );
84
+ if ( m )
85
+ data = m[1];
86
+ } else if ( settings.type == "elem" ) {
87
+ if( !elem.getElementsByTagName )
88
+ return undefined;
89
+ var e = elem.getElementsByTagName(settings.name);
90
+ if ( e.length )
91
+ data = $.trim(e[0].innerHTML);
92
+ } else if ( elem.getAttribute != undefined ) {
93
+ var attr = elem.getAttribute( settings.name );
94
+ if ( attr )
95
+ data = attr;
96
+ }
97
+
98
+ if ( data.indexOf( '{' ) <0 )
99
+ data = "{" + data + "}";
100
+
101
+ data = eval("(" + data + ")");
102
+
103
+ $.data( elem, settings.single, data );
104
+ return data;
105
+ }
106
+ }
107
+ });
108
+
109
+ /**
110
+ * Returns the metadata object for the first member of the jQuery object.
111
+ *
112
+ * @name metadata
113
+ * @descr Returns element's metadata object
114
+ * @param Object opts An object contianing settings to override the defaults
115
+ * @type jQuery
116
+ * @cat Plugins/Metadata
117
+ */
118
+ $.fn.metadata = function( opts ){
119
+ return $.metadata.get( this[0], opts );
120
+ };
121
+
122
+ })(jQuery);