-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added plugin based author and public tag models in API v2 (#10284)
refs #10124 - Author model returns only users that have published non-page posts - Added a public controller for tags (should be extracted to separate Content API controller TryGhost/Ghost#10106) - Made resource configuration dynamic based on current theme engine - This needs a follow-up PR with fixes to the problems described in the PR
- Loading branch information
1 parent
737db4b
commit 6f4bf3b
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const _ = require('lodash'); | ||
const _debug = require('ghost-ignition').debug._base; | ||
const debug = _debug('ghost-query'); | ||
|
||
const addHasPostsWhere = (tableName, config) => { | ||
const comparisonField = `${tableName}.id`; | ||
|
||
return function (qb) { | ||
return qb.whereIn(comparisonField, function () { | ||
const innerQb = this | ||
.distinct(`${config.joinTable}.${config.joinTo}`) | ||
.select() | ||
.from(config.joinTable) | ||
.whereRaw(`${config.joinTable}.${config.joinTo} = ${comparisonField}`) | ||
.join('posts', 'posts.id', `${config.joinTable}.post_id`) | ||
.andWhere('posts.status', '=', 'published'); | ||
|
||
debug(`QUERY has posts: ${innerQb.toSQL().sql}`); | ||
|
||
return innerQb; | ||
}); | ||
}; | ||
}; | ||
|
||
const hasPosts = function hasPosts(Bookshelf) { | ||
const modelPrototype = Bookshelf.Model.prototype; | ||
|
||
Bookshelf.Model = Bookshelf.Model.extend({ | ||
initialize: function () { | ||
return modelPrototype.initialize.apply(this, arguments); | ||
}, | ||
|
||
fetch: function () { | ||
if (this.shouldHavePosts) { | ||
this.query(addHasPostsWhere(_.result(this, 'tableName'), this.shouldHavePosts)); | ||
} | ||
|
||
if (_debug.enabled('ghost-query')) { | ||
debug('QUERY', this.query().toQuery()); | ||
} | ||
|
||
return modelPrototype.fetch.apply(this, arguments); | ||
}, | ||
|
||
fetchAll: function () { | ||
if (this.shouldHavePosts) { | ||
this.query(addHasPostsWhere(_.result(this, 'tableName'), this.shouldHavePosts)); | ||
} | ||
|
||
if (_debug.enabled('ghost-query')) { | ||
debug('QUERY', this.query().toQuery()); | ||
} | ||
|
||
return modelPrototype.fetchAll.apply(this, arguments); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = hasPosts; | ||
module.exports.addHasPostsWhere = addHasPostsWhere; |