Permalink
Browse files

Merge pull request #536 from yappbox/fail_fast_on_undefined_record_type

Fail fast with a useful error when you pass toOne or toMany an undefined value
  • Loading branch information...
2 parents 49e8df6 + 66eb5ea commit f04ab0827549f66f5a77a08e1174c7fc161f2a51 @etgryphon etgryphon committed Aug 3, 2011
Showing with 39 additions and 0 deletions.
  1. +13 −0 frameworks/datastore/models/record.js
  2. +26 −0 frameworks/datastore/tests/models/single_attribute.js
@@ -1336,6 +1336,9 @@ SC.Record.mixin( /** @scope SC.Record */ {
opts = opts || {};
var isNested = opts.nested || opts.isNested;
var attr;
+
+ this._throwUnlessRecordTypeDefined(recordType, 'toMany');
+
if(isNested){
attr = SC.ChildrenAttribute.attr(recordType, opts);
}
@@ -1362,6 +1365,9 @@ SC.Record.mixin( /** @scope SC.Record */ {
opts = opts || {};
var isNested = opts.nested || opts.isNested;
var attr;
+
+ this._throwUnlessRecordTypeDefined(recordType, 'toOne');
+
if(isNested){
attr = SC.ChildAttribute.attr(recordType, opts);
}
@@ -1370,6 +1376,13 @@ SC.Record.mixin( /** @scope SC.Record */ {
}
return attr;
},
+
+ _throwUnlessRecordTypeDefined: function(recordType, relationshipType) {
+ if (!recordType) {
+ throw "Attempted to create " + relationshipType + " attribute with " +
+ "undefined recordType. Did you forget to sc_require a dependency?";
+ }
+ },
/**
Returns all storeKeys mapped by Id for this record type. This method is
@@ -282,3 +282,29 @@ test("isEditable NO should not fire property change observer", function() {
rec5.removeObserver('readOnlyRelatedTo', modifierListener);
});
+test("adding toOne pointing to non existing class should throw error", function() {
+ var message;
+ try {
+ MyApp.InvalidModel = SC.Record.extend({
+ foo: SC.Record.toOne(MyApp.DoesNotExist)
+ });
+ } catch (x) {
+ message = x;
+ }
+
+ same(message, 'Attempted to create toOne attribute with undefined recordType. Did you forget to sc_require a dependency?');
+});
+
+test("adding toMany pointing to non existing class should throw error", function() {
+ var message;
+ try {
+ MyApp.InvalidModel = SC.Record.extend({
+ foo: SC.Record.toMany(MyApp.DoesNotExist)
+ });
+ } catch (x) {
+ message = x;
+ }
+
+ same(message, 'Attempted to create toMany attribute with undefined recordType. Did you forget to sc_require a dependency?');
+});
+

0 comments on commit f04ab08

Please sign in to comment.