3a21b2ec by Adam Heath

Add 100% test coverage, and fix the few bugs that were found.

1 parent 1b9359a4
...@@ -22,7 +22,7 @@ define( ...@@ -22,7 +22,7 @@ define(
22 var validation = _.extend({}, oldValidation); 22 var validation = _.extend({}, oldValidation);
23 var found; 23 var found;
24 var f = function(value) { 24 var f = function(value) {
25 if (value === validateNestedValue) { 25 if (typeof value === 'object' && value.fn === validateNestedValue) {
26 found = true; 26 found = true;
27 } 27 }
28 }; 28 };
...@@ -44,7 +44,7 @@ define( ...@@ -44,7 +44,7 @@ define(
44 found = false; 44 found = false;
45 _.each(validators, f); 45 _.each(validators, f);
46 if (!found) { 46 if (!found) {
47 validators.push(validateNestedValue); 47 validators.push({fn: validateNestedValue});
48 } 48 }
49 } 49 }
50 } 50 }
...@@ -57,21 +57,21 @@ define( ...@@ -57,21 +57,21 @@ define(
57 modelClass.prototype[methodName] = function() { 57 modelClass.prototype[methodName] = function() {
58 var oldValidation = updateValidation(this); 58 var oldValidation = updateValidation(this);
59 try { 59 try {
60 if (originalMethod) { 60 return originalMethod.apply(this, arguments);
61 return originalMethod.apply(this, arguments);
62 } else {
63 return modelClass.__super__[methodName].apply(this, arguments);
64 }
65 } finally { 61 } finally {
66 this.validation = oldValidation; 62 this.validation = oldValidation;
67 } 63 }
68 }; 64 };
65 return modelClass;
69 } 66 }
70 67
71 function wrapSetFunction(modelClass) { 68 function wrapSetFunction(modelClass) {
72 var originalMethod = modelClass.prototype.set; 69 var originalMethod = modelClass.prototype.set;
73 modelClass.prototype.set = function(key, val, options) { 70 modelClass.prototype.set = function(key, val, options) {
74 var attr, attrs, curVal, nestedOptions, newVal; 71 var attr, attrs, curVal, nestedOptions, newVal;
72 if (typeof(key) === 'undefined') {
73 return this;
74 }
75 if (key === null) { 75 if (key === null) {
76 return this; 76 return this;
77 } 77 }
...@@ -93,23 +93,15 @@ define( ...@@ -93,23 +93,15 @@ define(
93 } 93 }
94 } 94 }
95 } 95 }
96 if (originalMethod) { 96 return originalMethod.call(this, attrs, options);
97 return originalMethod.call(this, attrs, options);
98 } else {
99 return modelClass.__super__.set.call(this, attrs, options);
100 }
101 }; 97 };
98 return modelClass;
102 } 99 }
103 100
104 function wrapToJSONFunction(modelClass) { 101 function wrapToJSONFunction(modelClass) {
105 var originalMethod = modelClass.prototype.toJSON; 102 var originalMethod = modelClass.prototype.toJSON;
106 modelClass.prototype.toJSON = function(options) { 103 modelClass.prototype.toJSON = function(options) {
107 var result; 104 var result = originalMethod.apply(this, arguments);
108 if (originalMethod) {
109 result = originalMethod.apply(this, arguments);
110 } else {
111 result = modelClass.__super__.toJSON.apply(this, arguments);
112 }
113 if (options && options.deep) { 105 if (options && options.deep) {
114 _.each(result, function(value, key) { 106 _.each(result, function(value, key) {
115 if (value instanceof Backbone.Model) { 107 if (value instanceof Backbone.Model) {
...@@ -119,6 +111,7 @@ define( ...@@ -119,6 +111,7 @@ define(
119 } 111 }
120 return result; 112 return result;
121 }; 113 };
114 return modelClass;
122 } 115 }
123 116
124 var NestedModels = { 117 var NestedModels = {
...@@ -128,12 +121,17 @@ define( ...@@ -128,12 +121,17 @@ define(
128 wrapToJSONFunction: wrapToJSONFunction, 121 wrapToJSONFunction: wrapToJSONFunction,
129 wrapValidationFunction: wrapValidationFunction, 122 wrapValidationFunction: wrapValidationFunction,
130 123
124 wrapValidationFunctions: function(modelClass) {
125 modelClass = wrapValidationFunction(modelClass, 'isValid');
126 modelClass = wrapValidationFunction(modelClass, 'validate');
127 modelClass = wrapValidationFunction(modelClass, 'preValidate');
128 return modelClass;
129 },
130
131 mixin: function(modelClass) { 131 mixin: function(modelClass) {
132 wrapSetFunction(modelClass); 132 modelClass = NestedModels.wrapSetFunction(modelClass);
133 wrapToJSONFunction(modelClass); 133 modelClass = NestedModels.wrapToJSONFunction(modelClass);
134 wrapValidationFunction(modelClass, 'isValid'); 134 modelClass = NestedModels.wrapValidationFunctions(modelClass);
135 wrapValidationFunction(modelClass, 'validate');
136 wrapValidationFunction(modelClass, 'preValidate');
137 return modelClass; 135 return modelClass;
138 }, 136 },
139 }; 137 };
......