Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support embedding records using deserialize: 'records' #200

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions 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 Down
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'
}
}
});
3 changes: 1 addition & 2 deletions tests/dummy/app/tests/factories/employee.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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')
},
traits: {
default_name_setup: {
Expand Down
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