Permalink
Browse files

[api] In one-to-many relationships, prepend parent-id to child-id to …

…ensure uniqueness in keys.
  • Loading branch information...
1 parent 41d37d9 commit 27b8184426978e5e9497221b4b398241e7b1da21 @Marak Marak committed Jun 22, 2012
Showing with 14 additions and 13 deletions.
  1. +2 −0 lib/resourceful/resource.js
  2. +12 −13 test/relationship-test.js
@@ -479,6 +479,7 @@ function relationship(factory, type, r, options) {
if (child instanceof rfactory) {
child[key] = id;
+ child._id = id + '/' + child._id;
child.save(function(err) {
if(err) {
if(callback) return callback(err);
@@ -490,6 +491,7 @@ function relationship(factory, type, r, options) {
var inheritance = {};
inheritance[key] = id;
child = resourceful.mixin({}, child, inheritance);
+ child._id = id + '/' + child._id;
rfactory.create(child, function(err, c) {
if(err) {
if(callback) return callback(err);
View
@@ -18,7 +18,7 @@ function authorAndArticles(name) {
'article #1': {
topic: function (author) {
author.createArticle({
- _id: author._id + '/article-1',
+ _id: 'article-1',
title: name + '\'s article #1'
}, this.callback);
},
@@ -27,7 +27,7 @@ function authorAndArticles(name) {
'article #2': {
topic: function (author) {
author.createArticle({
- _id: author._id + '/article-2',
+ _id: 'article-2',
title: name + '\'s article #2'
}, this.callback);
},
@@ -77,7 +77,7 @@ function category(parentName, childName){
return {
topic: function () {
this.Category.create({
- _id: 'category-' + parentName,
+ _id: parentName,
name: parentName
}, this.callback)
},
@@ -88,7 +88,7 @@ function category(parentName, childName){
'with parent Category': {
topic: function(parent){
parent.createCategory({
- _id: 'category-' + childName,
+ _id: childName,
name: childName
}, this.callback)
},
@@ -101,7 +101,7 @@ function category(parentName, childName){
}
function categoryParentTest(name) {
- var parent_id = 'category-' + name;
+ var parent_id = name;
return {
topic: function(){
// FIXME category pluralized should be categories (maybe use https://github.com/MSNexploder/inflect?)
@@ -126,24 +126,23 @@ function categoryParentTest(name) {
}
}
-function categoryChildTest(name) {
- var child_id = 'category-' + name;
-
+function categoryChildTest(parentName, childName) {
+ var child_id = parentName + '/' + childName;
return {
topic: function(){
this.Category.get(child_id, this.callback);
},
'should return the child': function(err, child){
assert.isNull(err);
- assert.equal(child.name, name);
+ assert.equal(child.name, childName);
},
'and child.category()': {
topic: function(child){
child.category(this.callback)
},
'should return the parent': function(err, parent){
assert.isNull(err);
- assert.notEqual(parent.name, name);
+ assert.notEqual(parent.name, childName);
}
}
}
@@ -233,7 +232,7 @@ vows.describe('resourceful/memory/relationship').addBatch({
'Author #1': authorAndArticles('paul'),
'Author #2': authorAndArticles('bob'),
'Author #3': authorAndArticlesWithoutId('lenny'),
- 'Category #1 & #2': category('alice', 'bob')
+ 'Category #1 & #2': category('hip-hop', 'a-tribe-called-quest')
}
}
}
@@ -251,7 +250,7 @@ vows.describe('resourceful/memory/relationship').addBatch({
'bob.articles': authorTest('bob'),
'Article.byAuthor(\'paul\')': articleTest('paul'),
'Article.byAuthor(\'bob\')': articleTest('bob'),
- 'Category.categories()': categoryParentTest('alice'),
- 'Category.category()': categoryChildTest('bob')
+ 'Category.categories()': categoryParentTest('hip-hop'),
+ 'Category.category()': categoryChildTest('hip-hop', 'a-tribe-called-quest')
}
}).export(module);

0 comments on commit 27b8184

Please sign in to comment.