Skip to content
This repository has been archived by the owner on Aug 21, 2020. It is now read-only.

Commit

Permalink
* moving a page via reorganize creates soft redirects properly, inclu…
Browse files Browse the repository at this point in the history
…ding for descendants

* all tests passing again
  • Loading branch information
Tom Boutell committed Nov 14, 2014
1 parent 7c41304 commit 8912ddf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 59 deletions.
36 changes: 36 additions & 0 deletions app.js
@@ -0,0 +1,36 @@
/* jshint node:true */

var argv = require('yargs').argv;
var request = require('request');
var fs = require('fs');
var async = require('async');
var _ = require('lodash');

var urls = fs.readFileSync(argv._[0], 'utf8').split(/\n/);

var tries = argv.tries || 10;

var speed = {};

return async.eachSeries(urls, function(url, callback) {
return async.times(tries, function(callback) {
var start = Date.now();
return request(url, function(err, response) {
console.log(response.statusCode);
if (err && (response.statusCode >= 400)) {
console.log(url + ': ERROR: ' + response.statusCode);
}
return callback(null, Date.now() - start);
});
}, function(err, results) {
var total;
for (var i = 0; (i < results.length); i++) {
total += results[i];
}
speed[url] = total / results.length;
return callback(null);
});
}, function(err) {

});

31 changes: 22 additions & 9 deletions index.js
Expand Up @@ -911,7 +911,7 @@ function pages(options, callback) {
var rank;
var originalPath;
var originalSlug;
async.series([getMoved, getTarget, getOldParent, getParent, permissions, nudgeNewPeers, moveSelf, moveDescendants, trashDescendants ], finish);
async.series([getMoved, getTarget, getOldParent, getParent, permissions, nudgeNewPeers, moveSelf, updateRedirects, moveDescendants, trashDescendants ], finish);
function getMoved(callback) {
if (moved) {
return callback(null);
Expand Down Expand Up @@ -1067,6 +1067,9 @@ function pages(options, callback) {
return callback(null);
});
}
function updateRedirects(callback) {
return apos.updateRedirect(originalSlug, moved.slug, callback);
}
function moveDescendants(callback) {
return self.updateDescendantPathsAndSlugs(moved, originalPath, originalSlug, function(err, changedArg) {
if (err) {
Expand Down Expand Up @@ -1149,14 +1152,24 @@ function pages(options, callback) {
_id: desc._id,
slug: newSlug
});
apos.pages.update({ _id: desc._id }, { $set: {
// Always matches
path: desc.path.replace(matchParentPathPrefix, page.path + '/'),
// Might not match, and we don't care (if they edited the slug that far up,
// they did so intentionally)
slug: newSlug,
level: desc.level + (page.level - oldLevel)
}}, callback);
return async.series({
update: function(callback) {
return apos.pages.update({ _id: desc._id }, { $set: {
// Always matches
path: desc.path.replace(matchParentPathPrefix, page.path + '/'),
// Might not match, and we don't care (if they edited the slug that far up,
// they did so intentionally)
slug: newSlug,
level: desc.level + (page.level - oldLevel)
}}, callback);
},
redirect: function(callback) {
if (desc.slug === newSlug) {
return setImmediate(callback);
}
return apos.updateRedirect(desc.slug, newSlug, callback);
}
}, callback);
});
}, function(err) {
if (err) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -30,6 +30,7 @@
},
"devDependencies": {
"apostrophe": "*",
"mocha": "*"
"mocha": "*",
"mongodb": "^1.4.19"
}
}
81 changes: 32 additions & 49 deletions test/test.js
Expand Up @@ -44,7 +44,8 @@ describe('apostrophe-pages', function() {
request: {},
locals: {},
get: function() {},
post: function() {}
post: function() {},
all: function() {}
}
}, function(err) {
assert(!err);
Expand Down Expand Up @@ -624,27 +625,21 @@ describe('apostrophe-pages', function() {

describe('add page', function() {
it('adds a new page beneath /contact called /contact/new-kid', function(done) {
var req = {
user: {
permissions: {
admin: true
}
},
var req = apos.getTaskReq();
req.body = {
parent: '/contact',
title: 'New Kid',
published: true,
tags: [ 'one', 'two' ],
type: 'default',
body: {
parent: '/contact',
title: 'New Kid',
published: true,
tags: [ 'one', 'two' ],
type: 'default',
body: {
type: 'area',
items: [
{
type: 'richText',
content: 'This is a test'
}
]
}
type: 'area',
items: [
{
type: 'richText',
content: 'This is a test'
}
]
}
};
var res = {
Expand Down Expand Up @@ -674,20 +669,14 @@ describe('apostrophe-pages', function() {

describe('edit page settings', function() {
it('propagates slug changes to children properly', function(done) {
var req = {
user: {
permissions: {
admin: true
}
},
body: {
originalSlug: '/contact/about',
slug: '/contact/about2',
title: 'About2',
published: true,
tags: [ 'one', 'two' ],
type: 'default'
}
var req = apos.getTaskReq();
req.body = {
originalSlug: '/contact/about',
slug: '/contact/about2',
title: 'About2',
published: true,
tags: [ 'one', 'two' ],
type: 'default'
};
var res = {
send: function(data) {
Expand All @@ -710,20 +699,14 @@ describe('apostrophe-pages', function() {
return pages._editRoute(req, res);
});
it('retains children when avoiding a duplicate slug error', function(done) {
var req = {
user: {
permissions: {
admin: true
}
},
body: {
originalSlug: '/contact/about2',
slug: '/contact/new-kid',
title: 'About2',
published: true,
tags: [ 'one', 'two' ],
type: 'default'
}
var req = apos.getTaskReq();
req.body = {
originalSlug: '/contact/about2',
slug: '/contact/new-kid',
title: 'About2',
published: true,
tags: [ 'one', 'two' ],
type: 'default'
};
var res = {
send: function(data) {
Expand Down

0 comments on commit 8912ddf

Please sign in to comment.