Fix bug saving related objects for articles — authors, tags, and attachments.

This commit is contained in:
Brent Simmons 2017-12-02 13:20:27 -08:00
parent a5c1bf41cb
commit b5d8233955
1 changed files with 22 additions and 0 deletions

View File

@ -114,6 +114,28 @@ extension Article: DatabaseObject {
return articleID return articleID
} }
} }
public func relatedObjectsWithName(_ name: String) -> [DatabaseObject]? {
switch name {
case RelationshipName.authors:
return databaseObjectArray(with: authors)
case RelationshipName.tags:
return databaseObjectArray(with: tags)
case RelationshipName.attachments:
return databaseObjectArray(with: attachments)
default:
return nil
}
}
private func databaseObjectArray<T: DatabaseObject>(with objects: Set<T>?) -> [DatabaseObject]? {
guard let objects = objects else {
return nil
}
return Array(objects)
}
} }
extension Set where Element == Article { extension Set where Element == Article {