Skip to content

Commit

Permalink
add column Kind for tag TryGhost#5
Browse files Browse the repository at this point in the history
  • Loading branch information
tamvm committed Sep 24, 2019
1 parent c218e8e commit b388e1b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
1 change: 0 additions & 1 deletion core/server/data/finpub/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ delete val;

const CompanyHandler = {
get: function(code) {
console.log('check code ', code, companies[code]);
if (code) {
return companies[code.toLowerCase()];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const common = require('../../../../lib/common');
const commands = require('../../../schema').commands;
const table = 'tags';
const newColumnNames = [
'kind',
];

function printResult(operation, columnName) {
return `${operation} column ${columnName} in ${table} table`;
}

module.exports.up = (options) => {
const connection = options.connection;
return Promise.map(newColumnNames, (newColumnName) => {
return connection.schema.hasColumn(table, newColumnName)
.then((exists) => {
if (exists) {
common.logging.warn(printResult('Adding', newColumnName));
return;
}
common.logging.info(printResult('Adding', newColumnName));
return commands.addColumn(table, newColumnName, connection);
});
});
};

module.exports.down = (options) => {
const connection = options.connection;
return Promise.map(newColumnNames, (newColumnName) => {
return connection.schema.hasColumn(table, newColumnName)
.then((exists) => {
if (!exists) {
common.logging.warn(printResult('Dropping', newColumnName));
return;
}
common.logging.info(printResult('Dropping', newColumnName));
return commands.dropColumn(table, newColumnName, connection);
});
});
};
3 changes: 2 additions & 1 deletion core/server/data/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ module.exports = {
created_at: {type: 'dateTime', nullable: false},
created_by: {type: 'string', maxlength: 24, nullable: false},
updated_at: {type: 'dateTime', nullable: true},
updated_by: {type: 'string', maxlength: 24, nullable: true}
updated_by: {type: 'string', maxlength: 24, nullable: true},
kind: {type: 'integer', nullable: true, unsigned: true, defaultTo: 0}
},
posts_tags: {
id: {type: 'string', maxlength: 24, nullable: false, primary: true},
Expand Down
9 changes: 8 additions & 1 deletion core/server/models/tag.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const ghostBookshelf = require('./base');
const CompanyHelper = require('../data/finpub/company');

const TagTypes = {
Company: 1
}

let Tag, Tags;

Tag = ghostBookshelf.Model.extend({
Expand Down Expand Up @@ -92,7 +96,6 @@ Tag = ghostBookshelf.Model.extend({
onCreating: function onCreating(model, attr, options) {
// overwrite onCreating to automatically add meta_title
// and meta_description
console.log('----------------------------> on creating tag ', this.get('name'));
let company = CompanyHelper.get(this.get('name'));
if (company) {

Expand All @@ -107,6 +110,10 @@ Tag = ghostBookshelf.Model.extend({
if (!options.importing || (options.importing && !this.get('description'))) {
this.set('description', String(company.name) + ' (' + company.exchange + ')');
}

if (!options.importing || (options.importing && !this.get('kind'))) {
this.set('kind', TagTypes.Company);
}
}

// call parent
Expand Down

0 comments on commit b388e1b

Please sign in to comment.