mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-04 17:04:59 +03:00
Merge pull request #5491 from ErisDS/issue-5490-no-post-url
Add event handling to pagination plugin
This commit is contained in:
commit
64e20735a3
@ -140,12 +140,13 @@ pagination = function pagination(bookshelf) {
|
|||||||
// Get the table name and idAttribute for this model
|
// Get the table name and idAttribute for this model
|
||||||
var tableName = _.result(this.constructor.prototype, 'tableName'),
|
var tableName = _.result(this.constructor.prototype, 'tableName'),
|
||||||
idAttribute = _.result(this.constructor.prototype, 'idAttribute'),
|
idAttribute = _.result(this.constructor.prototype, 'idAttribute'),
|
||||||
// Create a new collection for running `this` query, ensuring we're definitely using collection,
|
// Create a new collection for running `this` query, ensuring we're definitely using collection,
|
||||||
// rather than model
|
// rather than model
|
||||||
collection = this.constructor.collection(),
|
collection = this.constructor.collection(),
|
||||||
// Clone the base query & set up a promise to get the count of total items in the full set
|
// Clone the base query & set up a promise to get the count of total items in the full set
|
||||||
countPromise = this.query().clone().count(tableName + '.' + idAttribute + ' as aggregate'),
|
countPromise = this.query().clone().count(tableName + '.' + idAttribute + ' as aggregate'),
|
||||||
collectionPromise;
|
collectionPromise,
|
||||||
|
self;
|
||||||
|
|
||||||
// Clone the base query into our collection
|
// Clone the base query into our collection
|
||||||
collection._knex = this.query().clone();
|
collection._knex = this.query().clone();
|
||||||
@ -162,7 +163,22 @@ pagination = function pagination(bookshelf) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the promise to do a fetch on our collection, running the specified query.
|
this.resetQuery();
|
||||||
|
if (this.relatedData) {
|
||||||
|
collection.relatedData = this.relatedData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure that our model (self) gets the correct events fired upon it
|
||||||
|
self = this;
|
||||||
|
collection
|
||||||
|
.on('fetching', function (collection, columns, options) {
|
||||||
|
return self.triggerThen('fetching:collection', collection, columns, options);
|
||||||
|
})
|
||||||
|
.on('fetched', function (collection, resp, options) {
|
||||||
|
return self.triggerThen('fetched:collection', collection, resp, options);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Setup the promise to do a fetch on our collection, running the specified query
|
||||||
// @TODO: ensure option handling is done using an explicit pick elsewhere
|
// @TODO: ensure option handling is done using an explicit pick elsewhere
|
||||||
collectionPromise = collection.fetch(_.omit(options, ['page', 'limit']));
|
collectionPromise = collection.fetch(_.omit(options, ['page', 'limit']));
|
||||||
|
|
||||||
|
@ -61,6 +61,7 @@ Post = ghostBookshelf.Model.extend({
|
|||||||
|
|
||||||
// Ensures local copy of permalink setting is kept up to date
|
// Ensures local copy of permalink setting is kept up to date
|
||||||
this.on('fetching', getPermalinkSetting);
|
this.on('fetching', getPermalinkSetting);
|
||||||
|
this.on('fetching:collection', getPermalinkSetting);
|
||||||
|
|
||||||
this.on('created', function onCreated(model) {
|
this.on('created', function onCreated(model) {
|
||||||
model.emitChange('added');
|
model.emitChange('added');
|
||||||
|
@ -41,6 +41,7 @@ describe('Post Model', function () {
|
|||||||
function checkFirstPostData(firstPost) {
|
function checkFirstPostData(firstPost) {
|
||||||
should.not.exist(firstPost.author_id);
|
should.not.exist(firstPost.author_id);
|
||||||
firstPost.author.should.be.an.Object;
|
firstPost.author.should.be.an.Object;
|
||||||
|
firstPost.url.should.equal('/html-ipsum/');
|
||||||
firstPost.fields.should.be.an.Array;
|
firstPost.fields.should.be.an.Array;
|
||||||
firstPost.tags.should.be.an.Array;
|
firstPost.tags.should.be.an.Array;
|
||||||
firstPost.author.name.should.equal(DataGenerator.Content.users[0].name);
|
firstPost.author.name.should.equal(DataGenerator.Content.users[0].name);
|
||||||
@ -56,6 +57,14 @@ describe('Post Model', function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('findAll', function () {
|
describe('findAll', function () {
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox.stub(SettingsModel, 'findOne', function () {
|
||||||
|
return Promise.resolve({toJSON: function () {
|
||||||
|
return {value: '/:slug/'};
|
||||||
|
}});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('can findAll', function (done) {
|
it('can findAll', function (done) {
|
||||||
PostModel.findAll().then(function (results) {
|
PostModel.findAll().then(function (results) {
|
||||||
should.exist(results);
|
should.exist(results);
|
||||||
@ -85,6 +94,14 @@ describe('Post Model', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('findPage', function () {
|
describe('findPage', function () {
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox.stub(SettingsModel, 'findOne', function () {
|
||||||
|
return Promise.resolve({toJSON: function () {
|
||||||
|
return {value: '/:slug/'};
|
||||||
|
}});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('can findPage (default)', function (done) {
|
it('can findPage (default)', function (done) {
|
||||||
PostModel.findPage().then(function (results) {
|
PostModel.findPage().then(function (results) {
|
||||||
should.exist(results);
|
should.exist(results);
|
||||||
@ -267,7 +284,13 @@ describe('Post Model', function () {
|
|||||||
|
|
||||||
it('can findOne, returning all related data', function (done) {
|
it('can findOne, returning all related data', function (done) {
|
||||||
var firstPost;
|
var firstPost;
|
||||||
// TODO: should take author :-/
|
|
||||||
|
sandbox.stub(SettingsModel, 'findOne', function () {
|
||||||
|
return Promise.resolve({toJSON: function () {
|
||||||
|
return {value: '/:slug/'};
|
||||||
|
}});
|
||||||
|
});
|
||||||
|
|
||||||
PostModel.findOne({}, {include: ['author', 'fields', 'tags', 'created_by', 'updated_by', 'published_by']})
|
PostModel.findOne({}, {include: ['author', 'fields', 'tags', 'created_by', 'updated_by', 'published_by']})
|
||||||
.then(function (result) {
|
.then(function (result) {
|
||||||
should.exist(result);
|
should.exist(result);
|
||||||
|
@ -175,7 +175,7 @@ describe('pagination', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('fetchPage', function () {
|
describe('fetchPage', function () {
|
||||||
var model, bookshelf, mockQuery, fetch, colQuery;
|
var model, bookshelf, on, mockQuery, fetch, colQuery;
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
paginationUtils = pagination.__get__('paginationUtils');
|
paginationUtils = pagination.__get__('paginationUtils');
|
||||||
@ -197,15 +197,19 @@ describe('pagination', function () {
|
|||||||
|
|
||||||
fetch = sandbox.stub().returns(Promise.resolve({}));
|
fetch = sandbox.stub().returns(Promise.resolve({}));
|
||||||
colQuery = sandbox.stub();
|
colQuery = sandbox.stub();
|
||||||
|
on = function () { return this; };
|
||||||
|
on = sandbox.spy(on);
|
||||||
|
|
||||||
model = function () {};
|
model = function () {};
|
||||||
model.prototype.constructor = {
|
model.prototype.constructor = {
|
||||||
collection: sandbox.stub().returns({
|
collection: sandbox.stub().returns({
|
||||||
|
on: on,
|
||||||
fetch: fetch,
|
fetch: fetch,
|
||||||
query: colQuery
|
query: colQuery
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
model.prototype.query = sandbox.stub();
|
model.prototype.query = sandbox.stub();
|
||||||
|
model.prototype.resetQuery = sandbox.stub();
|
||||||
model.prototype.query.returns(mockQuery);
|
model.prototype.query.returns(mockQuery);
|
||||||
|
|
||||||
bookshelf = {Model: model};
|
bookshelf = {Model: model};
|
||||||
@ -231,6 +235,8 @@ describe('pagination', function () {
|
|||||||
model.prototype.query,
|
model.prototype.query,
|
||||||
mockQuery.clone,
|
mockQuery.clone,
|
||||||
paginationUtils.query,
|
paginationUtils.query,
|
||||||
|
on,
|
||||||
|
on,
|
||||||
fetch,
|
fetch,
|
||||||
paginationUtils.formatResponse
|
paginationUtils.formatResponse
|
||||||
);
|
);
|
||||||
@ -255,6 +261,10 @@ describe('pagination', function () {
|
|||||||
mockQuery.count.calledOnce.should.be.true;
|
mockQuery.count.calledOnce.should.be.true;
|
||||||
mockQuery.count.calledWith().should.be.true;
|
mockQuery.count.calledWith().should.be.true;
|
||||||
|
|
||||||
|
on.calledTwice.should.be.true;
|
||||||
|
on.firstCall.calledWith('fetching').should.be.true;
|
||||||
|
on.secondCall.calledWith('fetched').should.be.true;
|
||||||
|
|
||||||
fetch.calledOnce.should.be.true;
|
fetch.calledOnce.should.be.true;
|
||||||
fetch.calledWith({}).should.be.true;
|
fetch.calledWith({}).should.be.true;
|
||||||
|
|
||||||
@ -277,6 +287,8 @@ describe('pagination', function () {
|
|||||||
mockQuery.clone,
|
mockQuery.clone,
|
||||||
paginationUtils.query,
|
paginationUtils.query,
|
||||||
colQuery,
|
colQuery,
|
||||||
|
on,
|
||||||
|
on,
|
||||||
fetch,
|
fetch,
|
||||||
paginationUtils.formatResponse
|
paginationUtils.formatResponse
|
||||||
);
|
);
|
||||||
@ -304,6 +316,10 @@ describe('pagination', function () {
|
|||||||
colQuery.calledOnce.should.be.true;
|
colQuery.calledOnce.should.be.true;
|
||||||
colQuery.calledWith('orderBy', 'undefined.id', 'DESC').should.be.true;
|
colQuery.calledWith('orderBy', 'undefined.id', 'DESC').should.be.true;
|
||||||
|
|
||||||
|
on.calledTwice.should.be.true;
|
||||||
|
on.firstCall.calledWith('fetching').should.be.true;
|
||||||
|
on.secondCall.calledWith('fetched').should.be.true;
|
||||||
|
|
||||||
fetch.calledOnce.should.be.true;
|
fetch.calledOnce.should.be.true;
|
||||||
fetch.calledWith(orderOptions).should.be.true;
|
fetch.calledWith(orderOptions).should.be.true;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user