working localized flexible content, missing autopopulated flexible content

This commit is contained in:
James
2019-12-17 21:33:46 -05:00
parent bd73c91f77
commit a69d22c1d1
3 changed files with 16 additions and 26 deletions

View File

@@ -24,6 +24,7 @@ export default function localizationPlugin(schema, options) {
if (path.endsWith('global.en')) {
recurse = false;
}
if (schemaType.schema) { // propagate plugin initialization for sub-documents schemas
schemaType.schema.plugin(localizationPlugin, pluginOptions);
if (!recurse)
@@ -34,10 +35,6 @@ export default function localizationPlugin(schema, options) {
return;
}
if (!((schemaType instanceof mongoose.Schema.Types.String) || (schemaType instanceof mongoose.Schema.Types.Embedded))) {
throw new mongoose.Error('localization can only be used on type String or Embedded');
}
let pathArray = path.split('.'),
key = pathArray.pop(),
prefix = pathArray.join('.');
@@ -54,7 +51,6 @@ export default function localizationPlugin(schema, options) {
}, schema.tree);
delete tree[key];
schema.virtual(path)
.get(function () {
@@ -89,8 +85,9 @@ export default function localizationPlugin(schema, options) {
return value;
})
.set(function (value) {
// multiple locales are set as an object
if (typeof value === 'object') {
if (typeof value === 'object' && !Array.isArray(value)) {
let locales = this.schema.options.localization.locales;
locales.forEach(locale => {
if (!value[locale]) {
@@ -99,6 +96,7 @@ export default function localizationPlugin(schema, options) {
}
this.set(`${path}.${locale}`, value[locale]);
}, this);
return;
}
// embedded and sub-documents will use locale methods from the top level document

View File

@@ -63,30 +63,22 @@ const fieldToSchemaMap = {
};
},
flexible: (field, options = {}) => {
const flexible = {
const schema = new mongoose.Schema({
value: {
type: mongoose.Types.ObjectId,
autopopulate: true,
type: mongoose.Types.ObjectId,
refPath: `${options.path ? (options.path + '.') : ''}${field.name}.blockType`,
},
blockType: { type: String, enum: field.blocks },
_id: false,
};
});
const schema = new mongoose.Schema(
field.hasMany !== false ? [flexible] : flexible,
{
hasMany: field.hasMany,
localized: field.localized || false,
}
);
if (field.localized) {
schema.plugin(localizationPlugin, options.localization);
}
schema.plugin(autopopulate);
return schema;
return {
localized: field.localized || false,
type: [schema],
}
},
};