Skip to content

Commit

Permalink
Merge pull request #4640 from emberjs/enhance-attribute-debug-info
Browse files Browse the repository at this point in the history
build ember-data attribute debug info dynamically
  • Loading branch information
fivetanley authored Nov 11, 2016
2 parents aa3a3d5 + d83de4f commit 7b45290
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 23 deletions.
47 changes: 24 additions & 23 deletions addon/-private/system/debug/debug-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,40 @@ export default Ember.Mixin.create({
@private
*/
_debugInfo() {
var attributes = ['id'];
var relationships = { belongsTo: [], hasMany: [] };
var expensiveProperties = [];
let attributes = ['id'];
let relationships = { };
let expensiveProperties = [];

this.eachAttribute((name, meta) => attributes.push(name));

this.eachRelationship((name, relationship) => {
relationships[relationship.kind].push(name);
expensiveProperties.push(name);
});

var groups = [
let groups = [
{
name: 'Attributes',
properties: attributes,
expand: true
},
{
name: 'Belongs To',
properties: relationships.belongsTo,
expand: true
},
{
name: 'Has Many',
properties: relationships.hasMany,
expand: true
},
{
name: 'Flags',
properties: ['isLoaded', 'hasDirtyAttributes', 'isSaving', 'isDeleted', 'isError', 'isNew', 'isValid']
}
];

this.eachRelationship((name, relationship) => {
let properties = relationships[relationship.kind];

if (properties === undefined) {
properties = relationships[relationship.kind] = [];
groups.push({
name: relationship.name,
properties,
expand: true
});
}
properties.push(name);
expensiveProperties.push(name);
});

groups.push({
name: 'Flags',
properties: ['isLoaded', 'hasDirtyAttributes', 'isSaving', 'isDeleted', 'isError', 'isNew', 'isValid']
});

return {
propertyInfo: {
// include all other mixins / properties (not just the grouped ones)
Expand Down
1 change: 1 addition & 0 deletions addon/-private/system/relationship-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function relationshipFromMeta(meta) {
kind: meta.kind,
type: typeForRelationshipMeta(meta),
options: meta.options,
name: meta.name,
parentType: meta.parentType,
isRelationship: true
};
Expand Down
1 change: 1 addition & 0 deletions addon/-private/system/relationships/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default function belongsTo(modelName, options) {
isRelationship: true,
options: opts,
kind: 'belongsTo',
name: 'Belongs To',
key: null
};

Expand Down
1 change: 1 addition & 0 deletions addon/-private/system/relationships/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default function hasMany(type, options) {
isRelationship: true,
options: options,
kind: 'hasMany',
name: 'Has Many',
key: null
};

Expand Down
81 changes: 81 additions & 0 deletions tests/unit/debug-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,84 @@ test("_debugInfo groups the attributes and relationships correctly", function(as
assert.deepEqual(propertyInfo.groups[1].properties, ['maritalStatus']);
assert.deepEqual(propertyInfo.groups[2].properties, ['posts']);
});


test("_debugInfo supports arbitray relationship types", function(assert) {
let MaritalStatus = DS.Model.extend({
name: DS.attr('string')
});

let Post = DS.Model.extend({
title: DS.attr('string')
});

let User = DS.Model.extend({
name: DS.attr('string'),
isDrugAddict: DS.attr('boolean'),
maritalStatus: DS.belongsTo('marital-status', { async: false }),
posts: Ember.computed(() => [1, 2, 3] )
.readOnly().meta({
options: { inverse: null },
isRelationship: true,
kind: 'customRelationship',
name: 'Custom Relationship'
})
});

let store = createStore({
adapter: TestAdapter.extend(),
maritalStatus: MaritalStatus,
post: Post,
user: User
});

let record = run(() => store.createRecord('user'));

let propertyInfo = record._debugInfo().propertyInfo;

assert.deepEqual(propertyInfo, {
includeOtherProperties: true,
groups: [
{
name: 'Attributes',
properties: [
'id',
'name',
'isDrugAddict'
],
expand: true
},
{
name: 'Belongs To',
properties: [
'maritalStatus'
],
expand: true
},
{
name: 'Custom Relationship',
properties: [
'posts'
],
expand: true
},
{
name: 'Flags',
properties: [
'isLoaded',
'hasDirtyAttributes',
'isSaving',
'isDeleted',
'isError',
'isNew',
'isValid'
]
}
],
expensiveProperties: [
'maritalStatus',
'posts'
]
}
)
});

0 comments on commit 7b45290

Please sign in to comment.