From d83de4f7cfb44fdc7bc3ce945e62754f6488c22a Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Thu, 10 Nov 2016 19:36:24 -0800 Subject: [PATCH] build ember-data attribute debug info dynamically * allow other relationship types to be first-class and not crash the inspector * improve tests --- addon/-private/system/debug/debug-info.js | 47 +++++------ addon/-private/system/relationship-meta.js | 1 + .../system/relationships/belongs-to.js | 1 + .../-private/system/relationships/has-many.js | 1 + tests/unit/debug-test.js | 81 +++++++++++++++++++ 5 files changed, 108 insertions(+), 23 deletions(-) diff --git a/addon/-private/system/debug/debug-info.js b/addon/-private/system/debug/debug-info.js index f570f261de0..bebc89c38e7 100644 --- a/addon/-private/system/debug/debug-info.js +++ b/addon/-private/system/debug/debug-info.js @@ -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) diff --git a/addon/-private/system/relationship-meta.js b/addon/-private/system/relationship-meta.js index fcfeb19488e..356a155c120 100644 --- a/addon/-private/system/relationship-meta.js +++ b/addon/-private/system/relationship-meta.js @@ -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 }; diff --git a/addon/-private/system/relationships/belongs-to.js b/addon/-private/system/relationships/belongs-to.js index 59d9ca468f4..15a9d875409 100644 --- a/addon/-private/system/relationships/belongs-to.js +++ b/addon/-private/system/relationships/belongs-to.js @@ -97,6 +97,7 @@ export default function belongsTo(modelName, options) { isRelationship: true, options: opts, kind: 'belongsTo', + name: 'Belongs To', key: null }; diff --git a/addon/-private/system/relationships/has-many.js b/addon/-private/system/relationships/has-many.js index 8cf881730d3..cd2848cf386 100644 --- a/addon/-private/system/relationships/has-many.js +++ b/addon/-private/system/relationships/has-many.js @@ -137,6 +137,7 @@ export default function hasMany(type, options) { isRelationship: true, options: options, kind: 'hasMany', + name: 'Has Many', key: null }; diff --git a/tests/unit/debug-test.js b/tests/unit/debug-test.js index e74ef60f79b..97db31f7898 100644 --- a/tests/unit/debug-test.js +++ b/tests/unit/debug-test.js @@ -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' + ] + } + ) +});