Skip to content

Commit

Permalink
Merge pull request #5491 from dcyriller/mu-model-blueprint
Browse files Browse the repository at this point in the history
[Feature] Add MU support for model and model-test blueprints
  • Loading branch information
runspired authored Sep 26, 2018
2 parents d45e7f0 + c085b10 commit 67c123d
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 108 deletions.
38 changes: 33 additions & 5 deletions blueprints/model-test/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
var ModelBlueprint = require('../model');
var testInfo = require('ember-cli-test-info');
var useTestFrameworkDetector = require('../test-framework-detector');
const ModelBlueprint = require('../model');
const testInfo = require('ember-cli-test-info');
const useTestFrameworkDetector = require('../test-framework-detector');
const isModuleUnificationProject = require('../../lib/utilities/module-unification')
.isModuleUnificationProject;
const path = require('path');

module.exports = useTestFrameworkDetector({
description: 'Generates a model unit test.',

locals: function(options) {
var result = ModelBlueprint.locals.apply(this, arguments);
fileMapTokens(options) {
if (isModuleUnificationProject(this.project)) {
return {
__root__() {
return 'src';
},
__path__(options) {
return path.join('data', 'models', options.dasherizedModuleName);
},
__test__() {
return 'model-test';
},
};
} else {
return {
__root__() {
return 'tests';
},
__path__() {
return path.join('unit', 'models');
},
};
}
},

locals(options) {
const result = ModelBlueprint.locals.apply(this, arguments);

result.friendlyTestDescription = testInfo.description(options.entity.name, 'Unit', 'Model');

Expand Down
55 changes: 37 additions & 18 deletions blueprints/model/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
var inflection = require('inflection');
var stringUtils = require('ember-cli-string-utils');
var EOL = require('os').EOL;
const inflection = require('inflection');
const stringUtils = require('ember-cli-string-utils');
const EOL = require('os').EOL;
const isModuleUnificationProject = require('../../lib/utilities/module-unification')
.isModuleUnificationProject;
const path = require('path');

module.exports = {
description: 'Generates an ember-data model.',

anonymousOptions: ['name', 'attr:type'],

locals: function(options) {
var attrs = [];
var needs = [];
var entityOptions = options.entity.options;
fileMapTokens(options) {
if (isModuleUnificationProject(this.project)) {
return {
__root__() {
return 'src';
},
__path__(options) {
return path.join('data', 'models', options.dasherizedModuleName);
},
__name__() {
return 'model';
},
};
}
},

locals(options) {
let attrs = [];
let needs = [];
let entityOptions = options.entity.options;

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

var attr;
let attr;
if (/has-many/.test(dasherizedType)) {
var camelizedNamePlural = inflection.pluralize(camelizedName);
let camelizedNamePlural = inflection.pluralize(camelizedName);
attr = dsAttr(dasherizedForeignModelSingular, dasherizedType);
attrs.push(camelizedNamePlural + ': ' + attr);
} else if (/belongs-to/.test(dasherizedType)) {
Expand All @@ -43,7 +62,7 @@ module.exports = {
}
}

var needsDeduplicated = needs.filter(function(need, i) {
let needsDeduplicated = needs.filter(function(need, i) {
return needs.indexOf(need) === i;
});

Expand Down
Loading

0 comments on commit 67c123d

Please sign in to comment.