Skip to content

Commit

Permalink
refactoring: meta + UT
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Oct 5, 2016
1 parent 14cf168 commit 273a12a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/cli/cms/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fileAttr from './fileAttr'
import * as regex from './regex'
import * as sql from './sql'
import * as revision from './revision'
import * as meta from './meta'

export {
removeDuplicateAttr
Expand All @@ -12,4 +13,5 @@ export {
,sql
,revision
,regex
,meta
}
31 changes: 31 additions & 0 deletions src/cli/cms/data/meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import extend from 'extend'
import {
cmsData
,config
,dateSlug
} from '../../'

export function add(tpl, json, type, obj = {}, date = null, realType = 'draft') {
let meta = config.meta.name

// json[meta] = extend({}, json[meta])
var currentDate = (typeof date !== 'undefined' && date !== null && date !== '') ? date : new Date()
var abeUrl = (type === 'publish') ? json[meta].link : cmsData.fileAttr.add(json[meta].link, 'd' + dateSlug(currentDate.toISOString())) + ''

if(typeof json[meta].date === 'undefined' || json[meta].date === null) {
json[meta].date = currentDate
}
json[meta].latest = {
date: currentDate,
abeUrl: abeUrl
}
json[meta].status = realType === 'reject' ? 'draft' : realType
if(typeof json[meta][type] === 'undefined' || json[meta][type] === null) {
json[meta][type] = JSON.parse(JSON.stringify(obj))
json[meta][type].date = currentDate
json[meta][type].abeUrl = abeUrl
}
json[meta][type].latest = JSON.parse(JSON.stringify(obj))
json[meta][type].latest.date = currentDate
json[meta][type].latest.abeUrl = abeUrl
}
2 changes: 1 addition & 1 deletion src/cli/cms/operations/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function save(url, tplPath, json = null, text = '', type = '', previousSa
date = new Date(date)
}
}
Util.addMetas(tpl, json, type, {}, date, realType)
cmsData.meta.add(tpl, json, type, {}, date, realType)

if(typeof text === 'undefined' || text === null || text === '') {
text = cmsTemplate.template.getTemplate(fullTpl)
Expand Down
25 changes: 0 additions & 25 deletions src/cli/core/utils/abe-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,6 @@ export default class Utils {
)
}

static addMetas(tpl, json, type, obj = {}, date = null, realType = 'draft') {
let meta = config.meta.name

json[meta] = extend({}, json[meta])
var currentDate = (typeof date !== 'undefined' && date !== null && date !== '') ? date : new Date()
var abeUrl = (type === 'publish') ? json[meta].link : cmsData.fileAttr.add(json[meta].link, 'd' + dateSlug(currentDate.toISOString())) + ''

if(typeof json[meta].date === 'undefined' || json[meta].date === null) {
json[meta].date = currentDate
}
json[meta].latest = {
date: currentDate,
abeUrl: abeUrl
}
json[meta].status = realType === 'reject' ? 'draft' : realType
if(typeof json[meta][type] === 'undefined' || json[meta][type] === null) {
json[meta][type] = JSON.parse(JSON.stringify(obj))
json[meta][type].date = currentDate
json[meta][type].abeUrl = abeUrl
}
json[meta][type].latest = JSON.parse(JSON.stringify(obj))
json[meta][type].latest.date = currentDate
json[meta][type].latest.abeUrl = abeUrl
}

static sanitizeSourceAttribute(obj, jsonPage){
if(typeof obj.sourceString !== 'undefined' && obj.sourceString !== null && obj.sourceString.indexOf('{{') > -1) {
var matches = obj.sourceString.match(/({{[a-zA-Z._]+}})/g)
Expand Down
33 changes: 33 additions & 0 deletions test/meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var chai = require('chai');
var path = require('path');

var config = require('../src/cli').config
config.set({root: __dirname + '/fixtures/'})

var cmsData = require('../src/cli').cmsData;
var Manager = require('../src/cli').Manager;
var fse = require('fs-extra');

describe('Meta', function() {
before( function(done) {
Manager.instance.init()
.then(function () {

this.fixture = {
tag: fse.readFileSync(__dirname + '/fixtures/templates/article.html', 'utf8')
}
done()

}.bind(this))
});

/**
* cmsData.meta.add
*
*/
it('cmsData.meta.add()', function() {
var json = {abe_meta: {link: 'article.html'}};
cmsData.meta.add('article', json);
chai.expect(json.abe_meta.date).to.not.be.undefined;
});
});

0 comments on commit 273a12a

Please sign in to comment.