Skip to content

Commit

Permalink
Embed records which use the attrs setting
Browse files Browse the repository at this point in the history
  • Loading branch information
patocallaghan committed May 11, 2016
1 parent 0a86ca7 commit 84c6275
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 5 deletions.
2 changes: 1 addition & 1 deletion addon/converter/fixture-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default class {
isEmbeddedRelationship(modelName, attr) {
let serializer = this.store.serializerFor(modelName);
var option = this.attrsOption(serializer, attr);
return option && option.embedded === 'always';
return option && (option.embedded === 'always' || option.deserialize === 'records');
}

attrsOption(serializer, attr) {
Expand Down
5 changes: 4 additions & 1 deletion tests/dummy/app/models/employee.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany, belongsTo } from 'ember-data/relationships';
import {
array,
fragment,
Expand All @@ -12,5 +13,7 @@ export default Model.extend({
gender : attr('string'),
birthDate: attr('date'),
position: attr(),
departmentEmployments : fragmentArray('department-employment')
departmentEmployments : fragmentArray('department-employment'),
salary: belongsTo('salary'),
reviews: hasMany('review')
});
9 changes: 9 additions & 0 deletions tests/dummy/app/models/manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Model from 'ember-data/model';
import { hasMany, belongsTo } from 'ember-data/relationships';
import { fragment } from 'model-fragments/attributes';

export default Model.extend({
name : fragment('name'),
salary: belongsTo('salary'),
reviews: hasMany('review')
});
6 changes: 6 additions & 0 deletions tests/dummy/app/models/review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import DS from 'ember-data';

export default DS.Model.extend({
rating: DS.attr('number'),
date: DS.attr('date')
});
7 changes: 7 additions & 0 deletions tests/dummy/app/models/salary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import DS from 'ember-data';
import { array } from 'model-fragments/attributes';

export default DS.Model.extend({
income: DS.attr('number'),
benefits: array('string')
});
14 changes: 14 additions & 0 deletions tests/dummy/app/serializers/manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,{
attrs: {
salary: {
serialize: true,
deserialize: 'records'
},
reviews: {
serialize: true,
deserialize: 'records'
}
}
});
11 changes: 9 additions & 2 deletions tests/dummy/app/tests/factories/employee.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import FactoryGuy from 'ember-data-factory-guy';
import { make } from 'ember-data-factory-guy';

FactoryGuy.define('employee', {
default: {
name: FactoryGuy.belongsTo('name'),
titles: ['Mr.', 'Dr.'],
gender: 'Male',
birthDate: new Date()
birthDate: new Date('2016-05-01'),
salary: FactoryGuy.belongsTo('salary'),
reviews: FactoryGuy.hasMany('review')
},
traits: {
default_name_setup: {
Expand All @@ -23,6 +24,12 @@ FactoryGuy.define('employee', {
},
with_department_employments: {
departmentEmployments: FactoryGuy.hasMany('department-employment', 2),
},
with_salary: {
salary: FactoryGuy.belongsTo('salary')
},
with_reviews: {
reviews: FactoryGuy.hasMany('review', 2)
}
}
});
18 changes: 18 additions & 0 deletions tests/dummy/app/tests/factories/manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import FactoryGuy from 'ember-data-factory-guy';

FactoryGuy.define('manager', {
default: {
name: FactoryGuy.belongsTo('name')
},
traits: {
default_name_setup: {
name: {}
},
with_salary: {
salary: FactoryGuy.belongsTo('salary')
},
with_reviews: {
reviews: FactoryGuy.hasMany('review', 2)
}
}
});
8 changes: 8 additions & 0 deletions tests/dummy/app/tests/factories/review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FactoryGuy from 'ember-data-factory-guy';

FactoryGuy.define('review', {
default: {
rating: FactoryGuy.generate((num)=> num),
date: new Date('2015-05-01')
}
});
8 changes: 8 additions & 0 deletions tests/dummy/app/tests/factories/salary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FactoryGuy from 'ember-data-factory-guy';

FactoryGuy.define('salary', {
default: {
income: 90000,
benefits: ['health', 'company car', 'dental']
}
});
2 changes: 1 addition & 1 deletion tests/helpers/utility-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let theUsualSetup = function (adapterType) {
// comic book will always be REST style serializer
store.serializerFor = function(modelName) {
let originalSerializer = findSerializer(modelName);
if (modelName.match(/(comic-book|name|department|address|department-employment)/)) {
if (modelName.match(/(comic-book|name|department|address|department-employment|manager)/)) {
return originalSerializer;
}
return serializer;
Expand Down
109 changes: 109 additions & 0 deletions tests/unit/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,115 @@ test("embeds hasMany records passed as prebuilt ( buildList ) json when serializ
deepEqual(buildJson, expectedJson);
});

test("embeds belongsTo record when serializer attrs => deserialize: 'records' ", function () {

let buildJson = build('manager', 'with_salary');
buildJson.unwrap();

let expectedJson = {
manager: {
id: 1,
name: {
firstName: "Tyrion",
id: 1,
lastName: "Lannister"
},
salary: {
id: 1,
income: 90000,
benefits: ['health', 'company car', 'dental']
}
}
};

deepEqual(buildJson, expectedJson);
});

test("embeds belongsTo record passed as prebuilt ( build ) json when serializer attrs => deserialize: 'records' ", function () {
let salary = build('salary');
let buildJson = build('manager', {salary: salary});
buildJson.unwrap();

let expectedJson = {
manager: {
id: 1,
name: {
firstName: "Tyrion",
id: 1,
lastName: "Lannister"
},
salary: {
id: 1,
income: 90000,
benefits: ['health', 'company car', 'dental']
}
}
};

deepEqual(buildJson, expectedJson);
});

test("embeds hasMany records when serializer attrs => deserialize: 'records'", function () {

let buildJson = build('manager', 'with_reviews');
buildJson.unwrap();

let expectedJson = {
manager: {
id: 1,
name: {
firstName: "Tyrion",
id: 1,
lastName: "Lannister"
},
reviews: [
{
id: 1,
rating: 1,
date: "2015-05-01T00:00:00.000Z"
},
{
id: 2,
rating: 2,
date: "2015-05-01T00:00:00.000Z"
}
]
}
};

deepEqual(buildJson, expectedJson);
});

test("embeds hasMany records passed as prebuilt ( buildList ) json when serializer attrs => deserialize: 'records'", function () {
let reviews = buildList('review', 2);
let buildJson = build('manager', {reviews: reviews});
buildJson.unwrap();

let expectedJson = {
manager: {
id: 1,
name: {
firstName: "Tyrion",
id: 1,
lastName: "Lannister"
},
reviews: [
{
id: 1,
rating: 1,
date: "2015-05-01T00:00:00.000Z"
},
{
id: 2,
rating: 2,
date: "2015-05-01T00:00:00.000Z"
}
]
}
};

deepEqual(buildJson, expectedJson);
});

module(title(adapter, 'FactoryGuy#buildList custom'), inlineSetup(App, adapterType));

Expand Down

0 comments on commit 84c6275

Please sign in to comment.