Skip to content

Commit

Permalink
fixing bug where calling AssociationCollection.destroy() wasn't
Browse files Browse the repository at this point in the history
destroying the entity.
  • Loading branch information
danschultz committed May 10, 2011
1 parent 76cdc20 commit 2d8c41d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/mesh/model/Entity.as
Expand Up @@ -162,6 +162,7 @@ package mesh.model
*/
public function destroy():DestroyRequest
{
markForRemoval();
return Mesh.service(reflect.clazz).destroy([this]);
}

Expand Down
18 changes: 18 additions & 0 deletions src/mesh/model/associations/AssociationCollection.as
Expand Up @@ -33,6 +33,9 @@ package mesh.model.associations

afterLoad(loaded);
afterAdd(populateInverseAssociation);

afterAdd(registerObserversOnEntity);
afterRemove(unregisterObserversOnEntity);
}

/**
Expand Down Expand Up @@ -181,6 +184,11 @@ package mesh.model.associations
}
}

private function handleEntityDestroyed(entity:Entity):void
{
remove(entity);
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -215,6 +223,16 @@ package mesh.model.associations
}
}

private function registerObserversOnEntity(entity:Entity):void
{
entity.addObserver("afterDestroy", handleEntityDestroyed);
}

private function unregisterObserversOnEntity(entity:Entity):void
{
entity.removeObserver("afterDestroy", handleEntityDestroyed);
}

/**
* @copy #removeItem()
*/
Expand Down
19 changes: 16 additions & 3 deletions tests/mesh/model/AssociationCollectionTests.as
Expand Up @@ -2,14 +2,14 @@ package mesh.model
{
import flash.utils.flash_proxy;

import mesh.Address;
import mesh.Customer;
import mesh.Order;
import mesh.model.associations.AssociationCollection;

import org.flexunit.assertThat;
import org.hamcrest.collection.array;
import org.hamcrest.object.equalTo;
import mesh.Address;
import mesh.Customer;
import mesh.Order;

public class AssociationCollectionTests
{
Expand Down Expand Up @@ -75,6 +75,19 @@ package mesh.model
assertThat(_collection.isDirty, equalTo(true));
}

[Test]
public function testDestroyEntityBelongingToAssociation():void
{
var order:Order = new Order();
_collection.add(order);
_collection.save().execute();

_collection.destroy(order).execute();
assertThat(order.isDestroyed, equalTo(true));
assertThat(_collection.contains(order), equalTo(false));
assertThat(_collection.isDirty, equalTo(false));
}

[Test]
public function testDestroyedEntityIsNewWhenReadded():void
{
Expand Down

0 comments on commit 2d8c41d

Please sign in to comment.