Skip to content

Commit

Permalink
fix(relations): fixes hasMany and hasOne failing when inline property…
Browse files Browse the repository at this point in the history
… is null.
  • Loading branch information
iobaixas committed Oct 24, 2015
1 parent 848c39f commit 05ab547
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/module/extended/builder-relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
if(_source || _url) this.attrMap(_attr, _source || _url);

this.attrDecoder(_attr, function(_raw) {
this[_attr].$reset().$decode(_raw);
this[_attr].$reset().$decode(_raw || []);
})
.attrMask(_attr, Utils.WRITE_MASK)
.attrMeta(_attr, { relation: 'has_many' });
Expand Down Expand Up @@ -158,7 +158,7 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
if(_source || _url) this.attrMap(_attr, _source || _url);

this.attrDecoder(_attr, function(_raw) {
this[_attr].$decode(_raw);
this[_attr].$decode(_raw || {}); // TODO: null _raw should clear the object properties
})
.attrMask(_attr, Utils.WRITE_MASK)
.attrMeta(_attr, { relation: 'has_one' });
Expand Down
10 changes: 10 additions & 0 deletions test/relation-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ describe('Restmod model relation:', function() {
expect(bike.activity.length).toEqual(2);
});

it('should clear collection if inlined content is null', function() {
var bike = Bike.$new(1).$decode({ rides: null });
expect(bike.activity.length).toEqual(0);
});

it('should reset collection content if new inline content is fed', function() {
var bike = Bike.$new(1).$decode({ rides: [{ id: 1 }, { id: 2 }] });
bike.$decode({ rides: [{ id: 3 }] });
Expand Down Expand Up @@ -213,6 +218,11 @@ describe('Restmod model relation:', function() {
expect(bike.serialNo.value).toEqual('SERIAL');
});

it('should do nothing if inline content is null', function() {
var bike = Bike.$new(1).$decode({ serial: null });
expect(bike.serialNo).not.toBeNull();
});

it('should bubble child events to child type', function() {
var spy = jasmine.createSpy();
$injector.get('SerialNo').mix({
Expand Down

0 comments on commit 05ab547

Please sign in to comment.