/* 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); } });