@@ -11,6 +11,7 @@ const config = require('../config');
11
11
const converters = require ( '../lib/mobiledoc/converters' ) ;
12
12
const relations = require ( './relations' ) ;
13
13
const MOBILEDOC_REVISIONS_COUNT = 10 ;
14
+ const ALL_STATUSES = [ 'published' , 'draft' , 'scheduled' ] ;
14
15
15
16
let Post ;
16
17
let Posts ;
@@ -517,6 +518,51 @@ Post = ghostBookshelf.Model.extend({
517
518
}
518
519
519
520
return options . context && options . context . public ? 'page:false' : 'page:false+status:published' ;
521
+ } ,
522
+
523
+ /**
524
+ * You can pass an extra `status=VALUES` or "staticPages" field.
525
+ * Long-Term: We should deprecate these short cuts and force users to use the filter param.
526
+ */
527
+ extraFilters : function extraFilters ( options ) {
528
+ if ( ! options . staticPages && ! options . status ) {
529
+ return null ;
530
+ }
531
+
532
+ let filter = null ;
533
+
534
+ // CASE: "staticPages" is passed
535
+ if ( options . staticPages && options . staticPages !== 'all' ) {
536
+ // CASE: convert string true/false to boolean
537
+ if ( ! _ . isBoolean ( options . staticPages ) ) {
538
+ options . staticPages = _ . includes ( [ 'true' , '1' ] , options . staticPages ) ;
539
+ }
540
+
541
+ filter = `page:${ options . staticPages } ` ;
542
+ } else if ( options . staticPages === 'all' ) {
543
+ filter = 'page:[true, false]' ;
544
+ }
545
+
546
+ // CASE: "status" is passed, combine filters
547
+ if ( options . status && options . status !== 'all' ) {
548
+ options . status = _ . includes ( ALL_STATUSES , options . status ) ? options . status : 'published' ;
549
+
550
+ if ( ! filter ) {
551
+ filter = `status:${ options . status } ` ;
552
+ } else {
553
+ filter = `${ filter } +status:${ options . status } ` ;
554
+ }
555
+ } else if ( options . status === 'all' ) {
556
+ if ( ! filter ) {
557
+ filter = `status:[${ ALL_STATUSES } ]` ;
558
+ } else {
559
+ filter = `${ filter } +status:[${ ALL_STATUSES } ]` ;
560
+ }
561
+ }
562
+
563
+ delete options . status ;
564
+ delete options . staticPages ;
565
+ return filter ;
520
566
}
521
567
} , {
522
568
allowedFormats : [ 'mobiledoc' , 'html' , 'plaintext' ] ,
@@ -530,53 +576,26 @@ Post = ghostBookshelf.Model.extend({
530
576
} ;
531
577
} ,
532
578
533
- orderDefaultRaw : function ( ) {
534
- return '' +
579
+ orderDefaultRaw : function ( options ) {
580
+ let order = '' +
535
581
'CASE WHEN posts.status = \'scheduled\' THEN 1 ' +
536
582
'WHEN posts.status = \'draft\' THEN 2 ' +
537
583
'ELSE 3 END ASC,' +
538
584
'CASE WHEN posts.status != \'draft\' THEN posts.published_at END DESC,' +
539
585
'posts.updated_at DESC,' +
540
586
'posts.id DESC' ;
541
- } ,
542
-
543
- /**
544
- * @deprecated in favour of filter
545
- */
546
- processOptions : function processOptions ( options ) {
547
- if ( ! options . staticPages && ! options . status ) {
548
- return options ;
549
- }
550
-
551
- // This is the only place that 'options.where' is set now
552
- options . where = { statements : [ ] } ;
553
587
554
- // Step 4: Setup filters (where clauses)
555
- if ( options . staticPages && options . staticPages !== 'all' ) {
556
- // convert string true/false to boolean
557
- if ( ! _ . isBoolean ( options . staticPages ) ) {
558
- options . staticPages = _ . includes ( [ 'true' , '1' ] , options . staticPages ) ;
559
- }
560
- options . where . statements . push ( { prop : 'page' , op : '=' , value : options . staticPages } ) ;
561
- delete options . staticPages ;
562
- } else if ( options . staticPages === 'all' ) {
563
- options . where . statements . push ( { prop : 'page' , op : 'IN' , value : [ true , false ] } ) ;
564
- delete options . staticPages ;
588
+ // CASE: if the filter contains an `IN` operator, we should return the posts first, which match both tags
589
+ if ( options . filter && options . filter . match ( / ( t a g s | t a g ) : \s ? \[ .* \] / ) ) {
590
+ order = `count(tags.id) DESC, ${ order } ` ;
565
591
}
566
592
567
- // Unless `all` is passed as an option, filter on
568
- // the status provided.
569
- if ( options . status && options . status !== 'all' ) {
570
- // make sure that status is valid
571
- options . status = _ . includes ( [ 'published' , 'draft' , 'scheduled' ] , options . status ) ? options . status : 'published' ;
572
- options . where . statements . push ( { prop : 'status' , op : '=' , value : options . status } ) ;
573
- delete options . status ;
574
- } else if ( options . status === 'all' ) {
575
- options . where . statements . push ( { prop : 'status' , op : 'IN' , value : [ 'published' , 'draft' , 'scheduled' ] } ) ;
576
- delete options . status ;
593
+ // CASE: if the filter contains an `IN` operator, we should return the posts first, which match both authors
594
+ if ( options . filter && options . filter . match ( / ( a u t h o r s | a u t h o r ) : \s ? \[ .* \] / ) ) {
595
+ order = `count(authors.id) DESC, ${ order } ` ;
577
596
}
578
597
579
- return options ;
598
+ return order ;
580
599
} ,
581
600
582
601
/**
0 commit comments