[Creation] Store location of new objects

The creation service stores the location (the full path) to
the domain object as "model.location"
This commit is contained in:
larkin 2015-06-30 10:55:23 -07:00 committed by Pete Richards
parent 2ca3c6ea93
commit 40e85b718d
2 changed files with 50 additions and 1 deletions

View File

@ -112,12 +112,37 @@ define(
return $q.when( return $q.when(
uuid() uuid()
).then(function (id) { ).then(function (id) {
model = addLocationToModel(id, model, parent);
return doPersist(persistence.getSpace(), id, model); return doPersist(persistence.getSpace(), id, model);
}).then(function (id) { }).then(function (id) {
return addToComposition(id, parent, persistence); return addToComposition(id, parent, persistence);
}); });
} }
// Store the location of an object relative to it's parent.
function addLocationToModel(modelId, model, parent) {
var context = parent.getCapability("context"),
pathObjects,
pathIds;
if (!context) {
$log.warn('No parent context, location will not be set.');
return model;
}
pathObjects = context.getPath();
if (!pathObjects || !pathObjects.length) {
pathObjects = [];
}
pathIds = pathObjects.map(function (object) {
return object.getId();
});
pathIds.push(modelId);
model.location = pathIds.join('/');
return model;
}
return { return {
/** /**
* Create a new domain object with the provided model, as * Create a new domain object with the provided model, as

View File

@ -38,6 +38,7 @@ define(
mockMutationCapability, mockMutationCapability,
mockPersistenceCapability, mockPersistenceCapability,
mockCompositionCapability, mockCompositionCapability,
mockContextCapability,
mockCapabilities, mockCapabilities,
creationService; creationService;
@ -87,16 +88,30 @@ define(
"composition", "composition",
["invoke"] ["invoke"]
); );
mockContextCapability = jasmine.createSpyObj(
"context",
["getPath"]
);
mockCapabilities = { mockCapabilities = {
mutation: mockMutationCapability, mutation: mockMutationCapability,
persistence: mockPersistenceCapability, persistence: mockPersistenceCapability,
composition: mockCompositionCapability composition: mockCompositionCapability,
context: mockContextCapability
}; };
mockPersistenceService.createObject.andReturn( mockPersistenceService.createObject.andReturn(
mockPromise(true) mockPromise(true)
); );
mockContextCapability.getPath.andReturn([
{
getId: function () { return 'root'; }
},
{
getId: function () { return 'parent'; }
}
]);
mockParentObject.getCapability.andCallFake(function (key) { mockParentObject.getCapability.andCallFake(function (key) {
return mockCapabilities[key]; return mockCapabilities[key];
}); });
@ -194,6 +209,15 @@ define(
expect(mockLog.error).toHaveBeenCalled(); expect(mockLog.error).toHaveBeenCalled();
}); });
it("stores location on new domainObjects", function() {
var model = { name: "my model" };
var objectPromise = creationService.createObject(
model,
mockParentObject
);
expect(model.location).toBeDefined();
expect(model.location.indexOf('root/parent')).toBe(0);
});
}); });
} }