0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Changed boolean handling

closes #2638
- replaced format() with parse() to convert values when fetched
- changed validation from integer to boolean
- added checks to tests
This commit is contained in:
Sebastian Gierlinger 2014-04-25 09:55:53 +02:00
parent 999136a1f9
commit 0bdfadd9d6
3 changed files with 19 additions and 8 deletions

View file

@ -7,8 +7,8 @@ var db = {
markdown: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true},
html: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true},
image: {type: 'text', maxlength: 2000, nullable: true},
featured: {type: 'bool', nullable: false, defaultTo: false},
page: {type: 'bool', nullable: false, defaultTo: false, validations: {'isIn': [['0', '1']]}},
featured: {type: 'bool', nullable: false, defaultTo: false, validations: {'isIn': [[false, true]]}},
page: {type: 'bool', nullable: false, defaultTo: false, validations: {'isIn': [[false, true]]}},
status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'draft'},
language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'},
meta_title: {type: 'string', maxlength: 150, nullable: true},

View file

@ -74,24 +74,30 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return attrs;
},
// Convert bools to ints to be consistent
// across db providers
// Convert integers to real booleans
fixBools: function (attrs) {
var self = this;
_.each(attrs, function (value, key) {
if (typeof value === "boolean") {
attrs[key] = value ? 1 : 0;
if (schema.tables[self.tableName][key].type === "bool") {
attrs[key] = value ? true : false;
}
});
return attrs;
},
// format date before writing to DB, bools work
format: function (attrs) {
return this.fixDates(attrs);
},
// format data and bool when fetching from DB
parse: function (attrs) {
return this.fixBools(this.fixDates(attrs));
},
toJSON: function (options) {
var attrs = this.fixBools(this.fixDates(_.extend({}, this.attributes))),
var attrs = _.extend({}, this.attributes),
relations = this.relations;
if (options && options.shallow) {

View file

@ -100,6 +100,8 @@ describe('Post API', function () {
jsonResponse.posts.should.have.length(5);
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
testUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
_.isBoolean(jsonResponse.posts[0].featured).should.eql(true);
_.isBoolean(jsonResponse.posts[0].page).should.eql(true);
done();
});
});
@ -206,6 +208,8 @@ describe('Post API', function () {
jsonResponse.posts.should.exist;
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
jsonResponse.posts[0].page.should.eql(0);
_.isBoolean(jsonResponse.posts[0].featured).should.eql(true);
_.isBoolean(jsonResponse.posts[0].page).should.eql(true);
done();
});
});
@ -224,7 +228,8 @@ describe('Post API', function () {
jsonResponse.should.exist;
jsonResponse.posts.should.exist;
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
jsonResponse.posts[0].page.should.eql(1);
jsonResponse.posts[0].page.should.eql(true);
_.isBoolean(jsonResponse.posts[0].page).should.eql(true);
done();
});
});