Permalink
Browse files

Refactor remote validators to use callbacks.

  • Loading branch information...
1 parent cbb6d92 commit d7c7b3a611839726aa3b81c625aad51ee266e78a @morganchristiansson committed Nov 23, 2011
Showing with 95 additions and 26 deletions.
  1. +74 −16 test/javascript/public/test/validators/uniqueness.js
  2. +21 −10 vendor/assets/javascripts/rails.validations.js
@@ -35,73 +35,131 @@ test('when matching uniqueness on a non-nested resource', function() {
var element = $('<input type="text" name="user[email]"/>');
var options = { 'message': "failed validation" };
element.val('nottaken@test.com');
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), undefined);
+ var callback=function(message) {
+ start();
+ equal(message, undefined);
+ };
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback);
+ stop();
});
test('when matching uniqueness on a non-nested resource', function() {
var element = $('<input type="text" name="user[email]"/>');
var options = { 'message': "failed validation" };
element.val('taken@test.com');
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), "failed validation");
+ var callback=function(message) {
+ start();
+ equal(message, "failed validation");
+ }
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback);
+ stop();
});
test('when matching uniqueness on a nested singular resource', function() {
var element = $('<input type="text" name="profile[user_attributes][email]"/>');
var options = { 'message': "failed validation" };
element.val('nottaken@test.com');
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), undefined);
+ var callback=function(message) {
+ start();
+ equal(message, undefined);
+ }
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback);
+ stop();
});
test('when matching uniqueness on a nested singular resource', function() {
var element = $('<input type="text" name="profile[user_attributes][email]"/>');
var options = { 'message': "failed validation" };
element.val('taken@test.com');
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), "failed validation");
+ var callback=function(message) {
+ start();
+ equal(message, "failed validation");
+ }
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback);
+ stop();
});
test('when using scopes with no replacement', function() {
var element = $('<input type="text" name="person[age]" />');
var options = { 'message': "failed validation", 'with': /\d+/, 'scope': { 'name': 'test name' } };
element.val('test');
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), "failed validation");
+ var callback = function(message) {
+ start();
+ equal(message, "failed validation");
+ };
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback);
+ stop();
});
test('when using scopes with replacement', function() {
var element = $('<input type="text" name="person[age]" />');
var options = { 'message': "failed validation", 'with': /\d+/, 'scope': { 'name': 'test name' } };
element.val('test')
$('#qunit-fixture').append('<input type="text" name="person[name]" />').find('input[name="person[name]"]').val('other name');
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), undefined);
+ var callback = function(message) {
+ start();
+ equal(message, undefined);
+ };
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback);
+ stop();
});
test('when validating by scope and mixed focus order', function() {
- var unique_element = $('#user_email'), scope_element = $('#user_name');
+ window._callback = function() {
+ start();
+ delete window._callback;
+ equal($('.message[for="user_email"]').text(), '');
+
+ window._callback = function() {
+ start();
+ delete window._callback;
+ equal($('.message[for="user_email"]').text(), 'must be unique');
+ };
+ setTimeout(function() {
+ var scope_element = $('#user_name');
+ scope_element.val('test name');
+ scope_element.trigger('change');
+ scope_element.trigger('focusout');
+ stop();
+ }, 0);
+ };
+ var unique_element = $('#user_email');
unique_element.val('free@test.com');
unique_element.trigger('change');
unique_element.trigger('focusout');
- equal($('.message[for="user_email"]').text(), '');
-
- scope_element.val('test name');
- scope_element.trigger('change');
- scope_element.trigger('focusout');
- equal($('.message[for="user_email"]').text(), 'must be unique');
+ stop();
});
test('when matching uniqueness on a resource with a defined class name', function() {
var element = $('<input type="text" name="user2[email]"/>');
var options = { 'message': "failed validation", 'class': "active_record_test_module/user2" };
element.val('nottaken@test.com');
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), 'failed validation');
+ var callback = function(message) {
+ start();
+ equal(message, 'failed validation');
+ };
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback);
+ stop();
});
test('when allowing blank', function() {
var element = $('<input type="text" name="user2[email]" />');
var options = { 'message': "failed validation", 'with': /\d+/, 'allow_blank': true };
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), undefined);
+ var callback = function(message) {
+ start();
+ equal(message, undefined);
+ };
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback);
});
test('when not allowing blank', function() {
var element = $('<input type="text" name="user2[email]" />');
var options = { 'message': "failed validation", 'with': /\d+/ };
- equal(ClientSideValidations.validators.remote.uniqueness(element, options), "failed validation");
+ var callback = function(message) {
+ start();
+ equal(message, "failed validation");
+ };
+ ClientSideValidations.validators.remote.uniqueness(element, options, callback)
+ stop();
});
+
@@ -109,12 +109,21 @@
}
if (valid) {
- for (kind in ClientSideValidations.validators.remote) {
- if (validators[kind] && (message = ClientSideValidations.validators.remote[kind](element, validators[kind]))) {
- element.trigger('element:validate:fail', message).data('valid', false);
- valid = false;
- break;
- }
+ //for (kind in ClientSideValidations.validators.remote) {
+ var kind = "uniqueness";
+ if (validators[kind]) {
+ var callback = function(message) {
+ if (message) {
+ element.trigger('element:validate:fail', message).data('valid', false);
+ valid = false;
+ } else { //valid
+ element.data('valid', null); element.trigger('element:validate:pass');
+ }
+ element.trigger('element:validate:after');
+ if(window._callback) window._callback();
+ };
+ ClientSideValidations.validators.remote[kind](element, validators[kind], callback);
+ return false;
}
}
@@ -270,11 +279,11 @@ var ClientSideValidations = {
}
},
remote: {
- uniqueness: function (element, options) {
+ uniqueness: function (element, options, callback) {
if ((message = ClientSideValidations.validators.local.presence(element, options)) && options.allow_blank === true) {
- return;
+ callback();
} else if (message) {
- return message;
+ callback(message);
} else {
var data = {},
name = null;
@@ -318,7 +327,9 @@ var ClientSideValidations = {
data: data,
async: false
}).status === 200) {
- return options.message;
+ callback(options.message);
+ } else {
+ callback();
}
}
}

0 comments on commit d7c7b3a

Please sign in to comment.