Skip to content

Commit

Permalink
Fix for issue #25: handling permanent, document-scoped slugs in subdo…
Browse files Browse the repository at this point in the history
…cument array.
  • Loading branch information
mykwillis committed Jan 15, 2021
1 parent ef95ec6 commit 67ce225
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/slug-generator.js
Expand Up @@ -334,6 +334,7 @@ async function findSame(
isCounter,
options,
findOne,
permanent
// slugsMdfPaths
) {
// console.log(`findSame#${doc.n} ${path}: ${slug}`);
Expand Down Expand Up @@ -380,7 +381,7 @@ async function findSame(
query._id = {
$ne: doc._id,
};
} else {
} else if (!permanent) {
query = false;
}
}
Expand Down Expand Up @@ -572,6 +573,7 @@ async function setSlugs(doc, slugs, options, findOne, cache) {
query,
findOne,
cache,
slug.permanent
// slugsMdfPaths
),
);
Expand Down Expand Up @@ -609,6 +611,7 @@ async function makeUniqueCounterSlug(
groups,
findOne,
cache,
permanent,
// slugsMdfPaths
) {
let slug = makeSlug(values, options);
Expand All @@ -624,6 +627,7 @@ async function makeUniqueCounterSlug(
true,
options,
findOne,
permanent
// slugsMdfPaths
);
if (result) {
Expand Down
56 changes: 56 additions & 0 deletions test/uniqueNestedGroup.js
Expand Up @@ -21,6 +21,22 @@ const childSchema = mongoose.Schema({
},
});

const childPermenantSchema = mongoose.Schema({
title: {
type: String,
required: true,
maxlength: 100,
},
slug: {
type: String,
slug: 'title',
index: true,
permanent: true,
slugPaddingSize: 4,
uniqueGroupSlug: '/_id',
},
});

const scratchSchema = mongoose.Schema({
title: {
type: String,
Expand Down Expand Up @@ -52,7 +68,17 @@ const scratchSchema = mongoose.Schema({
// ],
});

const scratchPermanentSchema = mongoose.Schema({
title: {
type: String,
required: true,
maxlength: 100,
},
children: [childPermenantSchema],
});

const Scratch = mongoose.model('scratchSchema', scratchSchema);
const ScratchPermanent = mongoose.model('scratchPermenantSchema', scratchPermanentSchema);

describe('Grouped Nested Resources (Counter)', function () {
before(async () => {
Expand Down Expand Up @@ -106,4 +132,34 @@ describe('Grouped Nested Resources (Counter)', function () {
doc.should.have.nested.property('children[1].title').and.equal('This is subtitle');
doc.should.have.nested.property('children[1].slug').and.equal('this-is-subtitle-0001');
});

it('Update existing resource with additional child and check children are unique locally', async () => {
const doc = await ScratchPermanent.create({
title: 'This is a long title',
children: [
{
title: 'This is nested subtitle',
},
{
title: 'This is nested subtitle',
},
],
});

doc.children.push(
{
title: 'This is nested subtitle',
});
await doc.save();


doc.should.have.nested.property('children[0].title').and.equal('This is nested subtitle');
doc.should.have.nested.property('children[0].slug').and.equal('this-is-nested-subtitle');

doc.should.have.nested.property('children[1].title').and.equal('This is nested subtitle');
doc.should.have.nested.property('children[1].slug').and.equal('this-is-nested-subtitle-0001');

doc.should.have.nested.property('children[2].title').and.equal('This is nested subtitle');
doc.should.have.nested.property('children[2].slug').and.equal('this-is-nested-subtitle-0002');
});
});

0 comments on commit 67ce225

Please sign in to comment.