Skip to content

Commit

Permalink
Merge pull request #3885 from gabrielgrant/fix-model-blueprint
Browse files Browse the repository at this point in the history
fix model relationship name generation
  • Loading branch information
stefanpenner committed Nov 3, 2015
2 parents f9fc011 + 726fc1f commit f5e745d
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions blueprints/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@ module.exports = {

for (var name in entityOptions) {
var type = entityOptions[name] || '';
var foreignModel = name;
if (type.indexOf(':') > -1) {
foreignModel = type.split(':')[1];
type = type.split(':')[0];
}
var dasherizedName = stringUtils.dasherize(name);
var dasherizedNameSingular = inflection.singularize(dasherizedName);
var camelizedName = stringUtils.camelize(name);
var dasherizedType = stringUtils.dasherize(type);
var dasherizedForeignModel = stringUtils.dasherize(foreignModel);
var dasherizedForeignModelSingular = inflection.singularize(dasherizedForeignModel);

if (/has-many/.test(dasherizedType)) {
var camelizedNamePlural = inflection.pluralize(camelizedName);
attrs.push(camelizedNamePlural + ': ' + dsAttr(dasherizedName, dasherizedType));
var attr = dsAttr(dasherizedForeignModelSingular, dasherizedType);
attrs.push(camelizedNamePlural + ': ' + attr);
} else if (/belongs-to/.test(dasherizedType)) {
var attr = dsAttr(dasherizedForeignModel, dasherizedType)
attrs.push(camelizedName + ': ' + attr);
} else {
attrs.push(camelizedName + ': ' + dsAttr(dasherizedName, dasherizedType));
var attr = dsAttr(dasherizedName, dasherizedType)
attrs.push(camelizedName + ': ' + attr);
}

if (/has-many|belongs-to/.test(dasherizedType)) {
needs.push("'model:" + dasherizedNameSingular + "'");
needs.push("'model:" + dasherizedForeignModelSingular + "'");
}
}
var needsDeduplicated = needs.filter(function(need, i) {
return needs.indexOf(need) === i;
})

attrs = attrs.join(',' + EOL + ' ');
needs = ' needs: [' + needs.join(', ') + ']';
needs = ' needs: [' + needsDeduplicated.join(', ') + ']';

return {
attrs: attrs,
Expand All @@ -51,8 +66,7 @@ function dsAttr(name, type) {
case 'belongs-to':
return 'DS.belongsTo(\'' + name + '\')';
case 'has-many':
var singularizedName = inflection.singularize(name);
return 'DS.hasMany(\'' + singularizedName + '\')';
return 'DS.hasMany(\'' + name + '\')';
case '':
//"If you don't specify the type of the attribute, it will be whatever was provided by the server"
//http://emberjs.com/guides/models/defining-models/
Expand Down

0 comments on commit f5e745d

Please sign in to comment.