Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add MU support for model and model-test blueprints #5491

Merged
merged 2 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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