Fix persistence for collections stored in different files

Getting the order of the fields failed when the collections was not folder based
This commit is contained in:
Mathias Biilmann Christensen 2017-02-21 23:04:12 -08:00
parent aa9a97ecba
commit e9088957b3

View File

@ -164,14 +164,15 @@ class Backend {
entryObj = { entryObj = {
path, path,
slug, slug,
raw: this.entryToRaw(collection, entryData), raw: this.entryToRaw(collection, entryDraft.get("entry")),
}; };
} else { } else {
const path = entryDraft.getIn(["entry", "path"]); const path = entryDraft.getIn(["entry", "path"]);
const slug = entryDraft.getIn(["entry", "slug"]);
entryObj = { entryObj = {
path, path,
slug: entryDraft.getIn(["entry", "slug"]), slug,
raw: this.entryToRaw(collection, entryData), raw: this.entryToRaw(collection, entryDraft.get("entry")),
}; };
} }
@ -201,9 +202,23 @@ class Backend {
entryToRaw(collection, entry) { entryToRaw(collection, entry) {
const format = resolveFormat(collection, entry); const format = resolveFormat(collection, entry.toJS());
const fieldsOrder = collection.get('fields').map(f => f.get('name')).toArray(); const fieldsOrder = this.fieldsOrder(collection, entry);
return format && format.toFile(entry, fieldsOrder); return format && format.toFile(entry.get("data").toJS(), fieldsOrder);
}
fieldsOrder(collection, entry) {
const fields = collection.get('fields');
if (fields) {
return collection.get('fields').map(f => f.get('name')).toArray();
}
const files = collection.get('files');
const file = (files || []).filter(f => f.get("name") === entry.get("slug")).get(0);
if (file == null) {
throw new Error(`No file found for ${ entry.get("slug") } in ${ collection.get('name') }`);
}
return file.get('fields');
} }
} }