mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 20:03:12 +03:00
refs #10105 - ordering !== filtering
This commit is contained in:
parent
e89a27f3ab
commit
090a936e16
@ -139,15 +139,6 @@ filter = function filter(Bookshelf) {
|
||||
.query('leftOuterJoin', 'posts_authors', 'posts_authors.post_id', '=', 'posts.id')
|
||||
.query('leftOuterJoin', 'users as authors', 'posts_authors.author_id', '=', 'authors.id');
|
||||
|
||||
// The order override should ONLY happen if we are doing an "IN" query
|
||||
// TODO move the order handling to the query building that is currently inside pagination
|
||||
// TODO make the order handling in pagination handle orderByRaw
|
||||
// TODO extend this handling to all joins
|
||||
if (gql.json.findStatement(this._filters.statements, {prop: /^authors/, op: 'IN'})) {
|
||||
// TODO make this count the number of MATCHING authors, not just the number of authors
|
||||
this.query('orderByRaw', 'count(authors.id) DESC');
|
||||
}
|
||||
|
||||
// We need to add a group by to counter the double left outer join
|
||||
// TODO improve on the group by handling
|
||||
options.groups = options.groups || [];
|
||||
|
@ -590,6 +590,11 @@ Post = ghostBookshelf.Model.extend({
|
||||
order = `count(tags.id) DESC, ${order}`;
|
||||
}
|
||||
|
||||
// CASE: if the filter contains an `IN` operator, we should return the posts first, which match both authors
|
||||
if (options.filter && options.filter.match(/(authors|author):\s?\[.*\]/)) {
|
||||
order = `count(authors.id) DESC, ${order}`;
|
||||
}
|
||||
|
||||
return order;
|
||||
},
|
||||
|
||||
|
@ -86,7 +86,7 @@ describe('Unit: models/post', function () {
|
||||
withRelated: ['authors', 'tags']
|
||||
}).then(() => {
|
||||
queries.length.should.eql(2);
|
||||
queries[0].sql.should.eql('select count(distinct posts.id) as aggregate from `posts` left outer join `posts_tags` on `posts_tags`.`post_id` = `posts`.`id` left outer join `tags` on `posts_tags`.`tag_id` = `tags`.`id` left outer join `posts_authors` on `posts_authors`.`post_id` = `posts`.`id` left outer join `users` as `authors` on `posts_authors`.`author_id` = `authors`.`id` where (`posts`.`page` = ? and `posts`.`status` = ?) and (`authors`.`slug` in (?, ?) and (`tags`.`slug` = ? or `posts`.`feature_image` is not null)) order by count(authors.id) DESC');
|
||||
queries[0].sql.should.eql('select count(distinct posts.id) as aggregate from `posts` left outer join `posts_tags` on `posts_tags`.`post_id` = `posts`.`id` left outer join `tags` on `posts_tags`.`tag_id` = `tags`.`id` left outer join `posts_authors` on `posts_authors`.`post_id` = `posts`.`id` left outer join `users` as `authors` on `posts_authors`.`author_id` = `authors`.`id` where (`posts`.`page` = ? and `posts`.`status` = ?) and (`authors`.`slug` in (?, ?) and (`tags`.`slug` = ? or `posts`.`feature_image` is not null))');
|
||||
queries[0].bindings.should.eql([
|
||||
false,
|
||||
'published',
|
||||
|
Loading…
Reference in New Issue
Block a user