Skip to content

Commit

Permalink
Merge pull request #4805 from emberjs/cleanup-from-stef
Browse files Browse the repository at this point in the history
Don’t redefine findPossibleInverses for each _findInverseFor
  • Loading branch information
fivetanley authored Feb 16, 2017
2 parents d4772a1 + 20c5e29 commit ac659ce
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions addon/-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@ const {
@module ember-data
*/


function findPossibleInverses(type, inverseType, name, relationshipsSoFar) {
let possibleRelationships = relationshipsSoFar || [];

let relationshipMap = get(inverseType, 'relationships');
if (!relationshipMap) { return possibleRelationships; }

let relationships = relationshipMap.get(type.modelName).filter(relationship => {
let optionsForRelationship = inverseType.metaForProperty(relationship.name).options;

if (!optionsForRelationship.inverse) {
return true;
}

return name === optionsForRelationship.inverse;
});

if (relationships) {
possibleRelationships.push.apply(possibleRelationships, relationships);
}

//Recurse to support polymorphism
if (type.superclass) {
findPossibleInverses(type.superclass, inverseType, name, possibleRelationships);
}

return possibleRelationships;
}

function intersection (array1, array2) {
let result = [];
array1.forEach((element) => {
Expand Down Expand Up @@ -1320,7 +1349,7 @@ Model.reopenClass({
});
}

let possibleRelationships = findPossibleInverses(this, inverseType);
let possibleRelationships = findPossibleInverses(this, inverseType, name);

if (possibleRelationships.length === 0) { return null; }

Expand All @@ -1345,36 +1374,6 @@ Model.reopenClass({
inverseKind = possibleRelationships[0].kind;
}

function findPossibleInverses(type, inverseType, relationshipsSoFar) {
let possibleRelationships = relationshipsSoFar || [];

let relationshipMap = get(inverseType, 'relationships');
if (!relationshipMap) { return possibleRelationships; }

let relationships = relationshipMap.get(type.modelName);

relationships = relationships.filter((relationship) => {
let optionsForRelationship = inverseType.metaForProperty(relationship.name).options;

if (!optionsForRelationship.inverse) {
return true;
}

return name === optionsForRelationship.inverse;
});

if (relationships) {
possibleRelationships.push.apply(possibleRelationships, relationships);
}

//Recurse to support polymorphism
if (type.superclass) {
findPossibleInverses(type.superclass, inverseType, possibleRelationships);
}

return possibleRelationships;
}

return {
type: inverseType,
name: inverseName,
Expand Down

0 comments on commit ac659ce

Please sign in to comment.