Skip to content

Commit

Permalink
Merge pull request #240 from netlify/fix-persistence-for-files-collec…
Browse files Browse the repository at this point in the history
…tions

Fix persistence for collections stored in different files
  • Loading branch information
biilmann authored Feb 22, 2017
2 parents aa9a97e + e908895 commit db078d2
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/backends/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,15 @@ class Backend {
entryObj = {
path,
slug,
raw: this.entryToRaw(collection, entryData),
raw: this.entryToRaw(collection, entryDraft.get("entry")),
};
} else {
const path = entryDraft.getIn(["entry", "path"]);
const slug = entryDraft.getIn(["entry", "slug"]);
entryObj = {
path,
slug: entryDraft.getIn(["entry", "slug"]),
raw: this.entryToRaw(collection, entryData),
slug,
raw: this.entryToRaw(collection, entryDraft.get("entry")),
};
}

Expand Down Expand Up @@ -201,9 +202,23 @@ class Backend {


entryToRaw(collection, entry) {
const format = resolveFormat(collection, entry);
const fieldsOrder = collection.get('fields').map(f => f.get('name')).toArray();
return format && format.toFile(entry, fieldsOrder);
const format = resolveFormat(collection, entry.toJS());
const fieldsOrder = this.fieldsOrder(collection, entry);
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');
}
}

Expand Down

0 comments on commit db078d2

Please sign in to comment.