Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue #194 Ability to delete a document while preserving some propert…

…ies (feature request)
  • Loading branch information...
commit 087738006bca78adbd2df0f02aaf761a9c0c9407 1 parent 46ede5d
@tleyden tleyden authored snej committed
Showing with 55 additions and 0 deletions.
  1. +5 −0 Model/CouchModel.h
  2. +28 −0 Model/CouchModel.m
  3. +22 −0 Test/Test_Model.m
View
5 Model/CouchModel.h
@@ -76,6 +76,11 @@
You can still use the model object afterwards, but it will refer to the deleted revision. */
- (RESTOperation*) deleteDocument;
+/** Deletes the document from the database via a PUT rather than a DELETE, saving
+ the additionalProperties into the deleted document.
+ You can still use the model object afterwards, but it will refer to the deleted revision. */
+- (RESTOperation*) deleteDocumentWithAdditionalProperties:(NSDictionary *)additionalProperties;
+
/** The time interval since the document was last changed externally (e.g. by a "pull" replication.
This value can be used to highlight recently-changed objects in the UI. */
@property (readonly) NSTimeInterval timeSinceExternallyChanged;
View
28 Model/CouchModel.m
@@ -147,6 +147,34 @@ - (RESTOperation*) deleteDocument {
return op;
}
+- (RESTOperation*) deleteDocumentWithAdditionalProperties:(NSDictionary *)additionalProperties {
+
+ if (!_document)
+ return nil;
+
+ COUCHLOG2(@"%@ Deleting document with additional properties", self);
+
+ if (additionalProperties == nil) {
+ additionalProperties = @{};
+ }
+
+ NSMutableDictionary* properties = [additionalProperties mutableCopy];
+ [properties setValue:[NSNumber numberWithBool:TRUE] forKey:@"_deleted"];
+ [properties setValue:[self getValueOfProperty:@"_id"] forKey:@"_id"];
+ [properties setValue:[self getValueOfProperty:@"_rev"] forKey:@"_rev"];
+
+ COUCHLOG2(@"%@ Saving <- %@", self, properties);
+ self.needsSave = NO;
+ RESTOperation* op = [_document putProperties: properties];
+ [op onCompletion: ^{
+ if (op.isSuccessful)
+ [self detachFromDocument];
+ }];
+ [op start];
+ return op;
+
+}
+
- (void) didLoadFromDocument {
// subclasses can override this
View
22 Test/Test_Model.m
@@ -234,6 +234,28 @@ - (void) test6_bulkSave {
STAssertEqualObjects(m1.name, @"Alice", nil);
}
+- (void) test6_deleteWithAdditionalProperties {
+
+ TestModel* m1 = [self createModelWithName: @"Alice" grade: 9];
+ AssertWait([m1 save]);
+ TestModel* m2 = [self createModelWithName: @"Bob" grade: 9];
+ AssertWait([m2 save]);
+
+ NSString *m2DocumentID = [[m2 document] propertyForKey:@"_id"];
+
+ AssertWait([m1 deleteDocumentWithAdditionalProperties:nil]);
+ STAssertTrue(([m1 document] == nil), @"Model's document should be nil after being deleted");
+
+ AssertWait([m2 deleteDocumentWithAdditionalProperties:@{@"foo":@"bar"}]);
+ STAssertTrue(([m2 document] == nil), @"Model's document should be nil after being deleted");
+
+ CouchDocument *deletedDocument = [_db documentWithID:m2DocumentID];
+ STAssertTrue([deletedDocument propertyForKey:@"_deleted"] == [NSNumber numberWithBool:TRUE], @"expected _deleted property to be true");
+ STAssertTrue([[deletedDocument propertyForKey:@"foo"] isEqualToString:@"bar"], @"expected foo property to be set");
+ // STAssertTrue([deletedDocument isDeleted], @"Document should be deleted"); <-- why isn't [document isDeleted] true? bug?
+
+}
+
#pragma mark - UTILITIES:
Please sign in to comment.
Something went wrong with that request. Please try again.