mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Merge pull request #6072 from ErisDS/use-gql
Refactor old processOptions/where to use GQL JSON
This commit is contained in:
commit
b64a0cc1f4
4 changed files with 64 additions and 36 deletions
|
@ -274,20 +274,16 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
|||
// Filter options so that only permitted ones remain
|
||||
options = this.filterOptions(options, 'findPage');
|
||||
|
||||
// Extend the model defaults
|
||||
options = _.defaults(options, this.findPageDefaultOptions());
|
||||
|
||||
// Run specific conversion of model query options to where options
|
||||
options = this.processOptions(itemCollection, options);
|
||||
|
||||
// Ensure only valid fields/columns are added to query
|
||||
if (options.columns) {
|
||||
options.columns = _.intersection(options.columns, this.prototype.permittedAttributes());
|
||||
}
|
||||
// This applies default properties like 'staticPages' and 'status'
|
||||
// And then converts them to 'where' options... this behaviour is effectively deprecated in favour
|
||||
// of using filter - it's only be being kept here so that we can transition cleanly.
|
||||
this.processOptions(_.defaults(options, this.findPageDefaultOptions()));
|
||||
|
||||
// If there are `where` conditionals specified, add those to the query.
|
||||
if (options.where) {
|
||||
itemCollection.query('where', options.where);
|
||||
itemCollection.query(function (qb) {
|
||||
gql.knexify(qb, options.where);
|
||||
});
|
||||
}
|
||||
|
||||
// Apply FILTER
|
||||
|
@ -304,6 +300,11 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
|||
// TODO: this should just be done for all methods @ the API level
|
||||
options.withRelated = _.union(options.withRelated, options.include);
|
||||
|
||||
// Ensure only valid fields/columns are added to query
|
||||
if (options.columns) {
|
||||
options.columns = _.intersection(options.columns, this.prototype.permittedAttributes());
|
||||
}
|
||||
|
||||
if (options.order) {
|
||||
options.order = self.parseOrderOption(options.order);
|
||||
} else {
|
||||
|
|
|
@ -333,8 +333,7 @@ Post = ghostBookshelf.Model.extend({
|
|||
findPageDefaultOptions: function findPageDefaultOptions() {
|
||||
return {
|
||||
staticPages: false, // include static pages
|
||||
status: 'published',
|
||||
where: {}
|
||||
status: 'published'
|
||||
};
|
||||
},
|
||||
|
||||
|
@ -347,14 +346,28 @@ Post = ghostBookshelf.Model.extend({
|
|||
};
|
||||
},
|
||||
|
||||
processOptions: function processOptions(itemCollection, options) {
|
||||
/**
|
||||
* @deprecated in favour of filter
|
||||
*/
|
||||
processOptions: function processOptions(options) {
|
||||
if (!options.staticPages && !options.status) {
|
||||
return options;
|
||||
}
|
||||
|
||||
// This is the only place that 'options.where' is set now
|
||||
options.where = {statements: []};
|
||||
|
||||
// Step 4: Setup filters (where clauses)
|
||||
if (options.staticPages !== 'all') {
|
||||
// convert string true/false to boolean
|
||||
if (!_.isBoolean(options.staticPages)) {
|
||||
options.staticPages = _.contains(['true', '1'], options.staticPages);
|
||||
}
|
||||
options.where['posts.page'] = options.staticPages;
|
||||
options.where.statements.push({prop: 'page', op: '=', value: options.staticPages});
|
||||
delete options.staticPages;
|
||||
} else if (options.staticPages === 'all') {
|
||||
options.where.statements.push({prop: 'page', op: 'IN', value: [true, false]});
|
||||
delete options.staticPages;
|
||||
}
|
||||
|
||||
// Unless `all` is passed as an option, filter on
|
||||
|
@ -362,7 +375,11 @@ Post = ghostBookshelf.Model.extend({
|
|||
if (options.status !== 'all') {
|
||||
// make sure that status is valid
|
||||
options.status = _.contains(['published', 'draft'], options.status) ? options.status : 'published';
|
||||
options.where['posts.status'] = options.status;
|
||||
options.where.statements.push({prop: 'status', op: '=', value: options.status});
|
||||
delete options.status;
|
||||
} else {
|
||||
options.where.statements.push({prop: 'status', op: 'IN', value: ['published', 'draft']});
|
||||
delete options.status;
|
||||
}
|
||||
|
||||
return options;
|
||||
|
|
|
@ -59,16 +59,17 @@ Tag = ghostBookshelf.Model.extend({
|
|||
}
|
||||
}, {
|
||||
findPageDefaultOptions: function findPageDefaultOptions() {
|
||||
return {
|
||||
where: {}
|
||||
};
|
||||
return {};
|
||||
},
|
||||
|
||||
orderDefaultOptions: function orderDefaultOptions() {
|
||||
return {};
|
||||
},
|
||||
|
||||
processOptions: function processOptions(itemCollection, options) {
|
||||
/**
|
||||
* @deprecated in favour of filter
|
||||
*/
|
||||
processOptions: function processOptions(options) {
|
||||
return options;
|
||||
},
|
||||
|
||||
|
|
|
@ -166,9 +166,7 @@ User = ghostBookshelf.Model.extend({
|
|||
}, {
|
||||
findPageDefaultOptions: function findPageDefaultOptions() {
|
||||
return {
|
||||
status: 'active',
|
||||
where: {},
|
||||
whereIn: {}
|
||||
status: 'active'
|
||||
};
|
||||
},
|
||||
|
||||
|
@ -180,28 +178,39 @@ User = ghostBookshelf.Model.extend({
|
|||
};
|
||||
},
|
||||
|
||||
processOptions: function processOptions(itemCollection, options) {
|
||||
// TODO: there are multiple statuses that make a user "active" or "invited" - we a way to translate/map them:
|
||||
// TODO (cont'd from above): * valid "active" statuses: active, warn-1, warn-2, warn-3, warn-4, locked
|
||||
// TODO (cont'd from above): * valid "invited" statuses" invited, invited-pending
|
||||
/**
|
||||
* @deprecated in favour of filter
|
||||
*/
|
||||
processOptions: function processOptions(options) {
|
||||
if (!options.status) {
|
||||
return options;
|
||||
}
|
||||
|
||||
// This is the only place that 'options.where' is set now
|
||||
options.where = {statements: []};
|
||||
|
||||
var allStates = activeStates.concat(invitedStates),
|
||||
value;
|
||||
|
||||
// Filter on the status. A status of 'all' translates to no filter since we want all statuses
|
||||
if (options.status && options.status !== 'all') {
|
||||
if (options.status !== 'all') {
|
||||
// make sure that status is valid
|
||||
// TODO: need a better way of getting a list of statuses other than hard-coding them...
|
||||
options.status = _.indexOf(
|
||||
['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked', 'invited', 'inactive'],
|
||||
options.status) !== -1 ? options.status : 'active';
|
||||
options.status = allStates.indexOf(options.status) > -1 ? options.status : 'active';
|
||||
}
|
||||
|
||||
if (options.status === 'active') {
|
||||
itemCollection.query().whereIn('status', activeStates);
|
||||
value = activeStates;
|
||||
} else if (options.status === 'invited') {
|
||||
itemCollection.query().whereIn('status', invitedStates);
|
||||
} else if (options.status !== 'all') {
|
||||
options.where.status = options.status;
|
||||
value = invitedStates;
|
||||
} else if (options.status === 'all') {
|
||||
value = allStates;
|
||||
} else {
|
||||
value = options.status;
|
||||
}
|
||||
|
||||
options.where.statements.push({prop: 'status', op: 'IN', value: value});
|
||||
delete options.status;
|
||||
|
||||
return options;
|
||||
},
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue