2018-03-19 12:53:17 +03:00
|
|
|
import BaseSerializer from './application';
|
2024-06-04 21:46:39 +03:00
|
|
|
import {camelize} from '@ember/string';
|
2018-03-19 12:53:17 +03:00
|
|
|
|
|
|
|
export default BaseSerializer.extend({
|
|
|
|
embed: true,
|
|
|
|
|
2024-06-04 21:46:39 +03:00
|
|
|
include(request) {
|
|
|
|
const queryIncludes = (request.queryParams.include || '').split(',').compact().map(camelize);
|
|
|
|
const includes = new Set(queryIncludes);
|
2018-03-19 12:53:17 +03:00
|
|
|
|
2024-06-04 21:46:39 +03:00
|
|
|
// embedded records that are included by default in the API
|
|
|
|
includes.add('tags');
|
|
|
|
includes.add('authors');
|
2018-03-19 12:53:17 +03:00
|
|
|
|
2024-06-04 21:46:39 +03:00
|
|
|
// clean up some things that mirage doesn't understand
|
|
|
|
includes.delete('authorsRoles');
|
|
|
|
includes.delete('countClicks');
|
|
|
|
includes.delete('postRevisionsAuthor');
|
|
|
|
includes.delete('tiers');
|
|
|
|
|
|
|
|
const result = Array.from(includes);
|
|
|
|
return result;
|
2024-05-07 17:24:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
serialize(postModelOrCollection, request) {
|
|
|
|
const updatePost = (post) => {
|
|
|
|
if (post.status === 'published') {
|
|
|
|
post.update('url', `http://localhost:4200/${post.slug}/`);
|
|
|
|
} else {
|
2024-06-11 18:47:54 +03:00
|
|
|
post.update('url', `http://localhost:4200/p/${post.uuid}/`);
|
2024-05-07 17:24:20 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.isModel(postModelOrCollection)) {
|
|
|
|
updatePost(postModelOrCollection);
|
|
|
|
} else {
|
|
|
|
postModelOrCollection.models.forEach(updatePost);
|
|
|
|
}
|
|
|
|
|
|
|
|
return BaseSerializer.prototype.serialize.call(this, postModelOrCollection, request);
|
2018-03-19 12:53:17 +03:00
|
|
|
}
|
|
|
|
});
|