mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Merge pull request #2651 from sebgie/issue#2638
Changed boolean handling
This commit is contained in:
commit
d089ddfe87
3 changed files with 19 additions and 8 deletions
|
@ -7,8 +7,8 @@ var db = {
|
||||||
markdown: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true},
|
markdown: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true},
|
||||||
html: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true},
|
html: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true},
|
||||||
image: {type: 'text', maxlength: 2000, nullable: true},
|
image: {type: 'text', maxlength: 2000, nullable: true},
|
||||||
featured: {type: 'bool', nullable: false, defaultTo: false},
|
featured: {type: 'bool', nullable: false, defaultTo: false, validations: {'isIn': [[false, true]]}},
|
||||||
page: {type: 'bool', nullable: false, defaultTo: false, validations: {'isIn': [['0', '1']]}},
|
page: {type: 'bool', nullable: false, defaultTo: false, validations: {'isIn': [[false, true]]}},
|
||||||
status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'draft'},
|
status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'draft'},
|
||||||
language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'},
|
language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'},
|
||||||
meta_title: {type: 'string', maxlength: 150, nullable: true},
|
meta_title: {type: 'string', maxlength: 150, nullable: true},
|
||||||
|
|
|
@ -74,24 +74,30 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||||
return attrs;
|
return attrs;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Convert bools to ints to be consistent
|
// Convert integers to real booleans
|
||||||
// across db providers
|
|
||||||
fixBools: function (attrs) {
|
fixBools: function (attrs) {
|
||||||
|
var self = this;
|
||||||
_.each(attrs, function (value, key) {
|
_.each(attrs, function (value, key) {
|
||||||
if (typeof value === "boolean") {
|
if (schema.tables[self.tableName][key].type === "bool") {
|
||||||
attrs[key] = value ? 1 : 0;
|
attrs[key] = value ? true : false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return attrs;
|
return attrs;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// format date before writing to DB, bools work
|
||||||
format: function (attrs) {
|
format: function (attrs) {
|
||||||
|
return this.fixDates(attrs);
|
||||||
|
},
|
||||||
|
|
||||||
|
// format data and bool when fetching from DB
|
||||||
|
parse: function (attrs) {
|
||||||
return this.fixBools(this.fixDates(attrs));
|
return this.fixBools(this.fixDates(attrs));
|
||||||
},
|
},
|
||||||
|
|
||||||
toJSON: function (options) {
|
toJSON: function (options) {
|
||||||
var attrs = this.fixBools(this.fixDates(_.extend({}, this.attributes))),
|
var attrs = _.extend({}, this.attributes),
|
||||||
relations = this.relations;
|
relations = this.relations;
|
||||||
|
|
||||||
if (options && options.shallow) {
|
if (options && options.shallow) {
|
||||||
|
|
|
@ -100,6 +100,8 @@ describe('Post API', function () {
|
||||||
jsonResponse.posts.should.have.length(5);
|
jsonResponse.posts.should.have.length(5);
|
||||||
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
||||||
testUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
testUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
||||||
|
_.isBoolean(jsonResponse.posts[0].featured).should.eql(true);
|
||||||
|
_.isBoolean(jsonResponse.posts[0].page).should.eql(true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -206,6 +208,8 @@ describe('Post API', function () {
|
||||||
jsonResponse.posts.should.exist;
|
jsonResponse.posts.should.exist;
|
||||||
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
||||||
jsonResponse.posts[0].page.should.eql(0);
|
jsonResponse.posts[0].page.should.eql(0);
|
||||||
|
_.isBoolean(jsonResponse.posts[0].featured).should.eql(true);
|
||||||
|
_.isBoolean(jsonResponse.posts[0].page).should.eql(true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -224,7 +228,8 @@ describe('Post API', function () {
|
||||||
jsonResponse.should.exist;
|
jsonResponse.should.exist;
|
||||||
jsonResponse.posts.should.exist;
|
jsonResponse.posts.should.exist;
|
||||||
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
|
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue