0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/core/client/app/models/post.js
Kevin Ansfield 3d6856614f Use es6 across client and add ember-suave to enforce rules
no issue
- add ember-suave dependency
- upgrade grunt-jscs dependency
- add a new .jscsrc for the client's tests directory that extends from client's base .jscsrc
- separate client tests in Gruntfile jscs task so they pick up the test's .jscsrc
- standardize es6 usage across client
2015-11-30 10:41:01 +00:00

83 lines
2.7 KiB
JavaScript

/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
import Ember from 'ember';
import DS from 'ember-data';
import ValidationEngine from 'ghost/mixins/validation-engine';
const {computed, inject} = Ember;
const {equal} = computed;
const {Model, attr, belongsTo, hasMany} = DS;
export default Model.extend(ValidationEngine, {
validationType: 'post',
uuid: attr('string'),
title: attr('string', {defaultValue: ''}),
slug: attr('string'),
markdown: attr('string', {defaultValue: ''}),
html: attr('string'),
image: attr('string'),
featured: attr('boolean', {defaultValue: false}),
page: attr('boolean', {defaultValue: false}),
status: attr('string', {defaultValue: 'draft'}),
language: attr('string', {defaultValue: 'en_US'}),
meta_title: attr('string'),
meta_description: attr('string'),
author: belongsTo('user', {async: true}),
author_id: attr('number'),
updated_at: attr('moment-date'),
updated_by: attr(),
published_at: attr('moment-date'),
published_by: belongsTo('user', {async: true}),
created_at: attr('moment-date'),
created_by: attr(),
tags: hasMany('tag', {
embedded: 'always',
async: false
}),
url: attr('string'),
config: inject.service(),
ghostPaths: inject.service('ghost-paths'),
absoluteUrl: computed('url', 'ghostPaths.url', 'config.blogUrl', function () {
let blogUrl = this.get('config.blogUrl');
let postUrl = this.get('url');
return this.get('ghostPaths.url').join(blogUrl, postUrl);
}),
previewUrl: computed('uuid', 'ghostPaths.url', 'config.blogUrl', 'config.routeKeywords.preview', function () {
let blogUrl = this.get('config.blogUrl');
let uuid = this.get('uuid');
let previewKeyword = this.get('config.routeKeywords.preview');
// New posts don't have a preview
if (!uuid) {
return '';
}
return this.get('ghostPaths.url').join(blogUrl, previewKeyword, uuid);
}),
scratch: null,
titleScratch: null,
// Computed post properties
isPublished: equal('status', 'published'),
isDraft: equal('status', 'draft'),
// remove client-generated tags, which have `id: null`.
// Ember Data won't recognize/update them automatically
// when returned from the server with ids.
// https://github.com/emberjs/data/issues/1829
updateTags() {
let tags = this.get('tags');
let oldTags = tags.filterBy('id', null);
tags.removeObjects(oldTags);
oldTags.invoke('deleteRecord');
},
isAuthoredByUser(user) {
return parseInt(user.get('id'), 10) === parseInt(this.get('author_id'), 10);
}
});