diff --git a/spec/models/model-spec.coffee b/spec/models/model-spec.coffee index a15ea81b7..459d76168 100644 --- a/spec/models/model-spec.coffee +++ b/spec/models/model-spec.coffee @@ -98,6 +98,9 @@ describe "Model", -> modelKey: 'testCollection' jsonKey: 'test_collection' itemClass: SubmodelItem + 'testJoinedData': Attributes.JoinedData + modelKey: 'testJoinedData' + jsonKey: 'test_joined_data' @json = 'id': '1234' @@ -126,6 +129,11 @@ describe "Model", -> @m.fromJSON(@json) expect(@m.daysOld).toBe(undefined) + it "should maintain empty string as empty strings", -> + expect(@m.accountId).toBe(undefined) + @m.fromJSON({account_id: ''}) + expect(@m.accountId).toBe('') + describe "Attributes.Number", -> it "should read number attributes and coerce them to numeric values", -> @m.fromJSON('test_number': 4) @@ -140,6 +148,17 @@ describe "Model", -> @m.fromJSON('test_number': 0) expect(@m.testNumber).toBe(0) + describe "Attributes.JoinedData", -> + it "should read joined data attributes and coerce them to string values", -> + @m.fromJSON('test_joined_data': null) + expect(@m.testJoinedData).toBe(null) + + @m.fromJSON('test_joined_data': '') + expect(@m.testJoinedData).toBe('') + + @m.fromJSON('test_joined_data': 'lolz') + expect(@m.testJoinedData).toBe('lolz') + describe "Attributes.Collection", -> it "should parse and inflate items", -> @m.fromJSON('test_collection': [{id: '123'}]) diff --git a/src/flux/attributes/attribute-joined-data.es6 b/src/flux/attributes/attribute-joined-data.es6 index 4d2607900..0eff63a5e 100644 --- a/src/flux/attributes/attribute-joined-data.es6 +++ b/src/flux/attributes/attribute-joined-data.es6 @@ -38,6 +38,14 @@ export default class AttributeJoinedData extends Attribute { this.modelTable = modelTable; } + toJSON(val) { + return val; + } + + fromJSON(val) { + return (val === null || val === undefined || val === false) ? null : `${val}`; + } + selectSQL() { // NullPlaceholder is necessary because if the LEFT JOIN returns nothing, it leaves the field // blank, and it comes through in the result row as "" rather than NULL