Add support for skipping relationship connections if all connection attributes evaluate to nil. Add support for establishing connections when some connection attributes evaluate to nil. fixes #1099 closes #1102

This commit is contained in:
Blake Watters
2012-12-25 23:53:01 -05:00
parent aecc1db352
commit 4b2b7dbe4c
2 changed files with 42 additions and 2 deletions

View File

@@ -45,9 +45,9 @@ static NSDictionary *RKConnectionAttributeValuesWithObject(RKConnectionDescripti
for (NSString *sourceAttribute in connection.attributes) {
NSString *destinationAttribute = [connection.attributes objectForKey:sourceAttribute];
id sourceValue = [managedObject valueForKey:sourceAttribute];
[destinationEntityAttributeValues setValue:sourceValue forKey:destinationAttribute];
[destinationEntityAttributeValues setValue:sourceValue ?: [NSNull null] forKey:destinationAttribute];
}
return destinationEntityAttributeValues;
return [[destinationEntityAttributeValues allValues] isEqualToArray:@[ [NSNull null] ]] ? nil : destinationEntityAttributeValues;
}
@interface RKRelationshipConnectionOperation ()
@@ -143,6 +143,7 @@ static NSDictionary *RKConnectionAttributeValuesWithObject(RKConnectionDescripti
if ([self.connection isForeignKeyConnection]) {
NSDictionary *attributeValues = RKConnectionAttributeValuesWithObject(self.connection, self.managedObject);
if ([attributeValues count] == 0) return nil;
NSSet *managedObjects = [self.managedObjectCache managedObjectsWithEntity:[self.connection.relationship destinationEntity]
attributeValues:attributeValues
inManagedObjectContext:self.managedObjectContext];

View File

@@ -301,4 +301,43 @@
assertThat(human.cats, hasItems(asia, nil));
}
- (void)testConnectionOfOptionalRelationshipIsSkippedWhenAllConnectionAttributesEvaluateToNil
{
RKHuman *human = [RKTestFactory insertManagedObjectForEntityForName:@"Human" inManagedObjectContext:nil withProperties:nil];
RKCat __unused *asia = [RKTestFactory insertManagedObjectForEntityForName:@"Cat" inManagedObjectContext:nil withProperties:@{@"birthYear": @2011}];
RKCat __unused *lola = [RKTestFactory insertManagedObjectForEntityForName:@"Cat" inManagedObjectContext:nil withProperties:@{@"birthYear": @2012}];
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:[RKTestFactory managedObjectStore]];
[mapping addConnectionForRelationship:@"cats" connectedBy:@"sex"];
RKFetchRequestManagedObjectCache *managedObjectCache = [RKFetchRequestManagedObjectCache new];
RKConnectionDescription *connection = [mapping connectionForRelationship:@"cats"];
RKRelationshipConnectionOperation *operation = [[RKRelationshipConnectionOperation alloc] initWithManagedObject:human connection:connection managedObjectCache:managedObjectCache];
[operation start];
assertThat(human.cats, hasCountOf(0));
}
- (void)testConnectionOfOptionalRelationshipIsEvaluatedWhenAtLeastOneAttributeEvaluatesToNonNil
{
RKHuman *human = [RKTestFactory insertManagedObjectForEntityForName:@"Human" inManagedObjectContext:nil withProperties:nil];
human.sex = @"female";
RKCat *asia = [RKTestFactory insertManagedObjectForEntityForName:@"Cat" inManagedObjectContext:nil withProperties:@{@"birthYear": @2011}];
asia.sex = @"female";
asia.name = @"Asia";
RKCat *lola = [RKTestFactory insertManagedObjectForEntityForName:@"Cat" inManagedObjectContext:nil withProperties:@{@"birthYear": @2012}];
lola.sex = @"female";
lola.name = nil;
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:[RKTestFactory managedObjectStore]];
[mapping addConnectionForRelationship:@"cats" connectedBy:@[ @"sex", @"name" ]];
RKFetchRequestManagedObjectCache *managedObjectCache = [RKFetchRequestManagedObjectCache new];
RKConnectionDescription *connection = [mapping connectionForRelationship:@"cats"];
RKRelationshipConnectionOperation *operation = [[RKRelationshipConnectionOperation alloc] initWithManagedObject:human connection:connection managedObjectCache:managedObjectCache];
[operation start];
assertThat(human.cats, hasCountOf(1));
assertThat(human.cats, hasItems(lola, nil));
}
@end