Skip to content

Commit

Permalink
Merge pull request emberjs#4070 from HeroicEric/use-modules-in-bluepr…
Browse files Browse the repository at this point in the history
…ints

Update blueprints to import modules directly
  • Loading branch information
bmac committed Jan 19, 2016
2 parents 440b67e + bcab3b5 commit 1be6c62
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 18 deletions.
4 changes: 2 additions & 2 deletions blueprints/adapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module.exports = {

locals: function(options) {
var adapterName = options.entity.name;
var baseClass = 'DS.JSONAPIAdapter';
var importStatement = 'import DS from \'ember-data\';';
var baseClass = 'JSONAPIAdapter';
var importStatement = 'import JSONAPIAdapter from \'ember-data/adapters/json-api\';';
var isAddon = options.inRepoAddon || options.project.isEmberCLIAddon();
var relativePath = pathUtil.getRelativePath(options.entity.name);

Expand Down
4 changes: 2 additions & 2 deletions blueprints/model/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DS from 'ember-data';
<%= importStatements %>

export default DS.Model.extend({
export default Model.extend({
<%= attrs %>
});
30 changes: 26 additions & 4 deletions blueprints/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module.exports = {
var attrs = [];
var needs = [];
var entityOptions = options.entity.options;
var importStatements = ['import Model from \'ember-data/model\';'];
var shouldImportAttr = false;
var shouldImportBelongsTo = false;
var shouldImportHasMany = false;

for (var name in entityOptions) {
var type = entityOptions[name] || '';
Expand All @@ -35,26 +39,44 @@ module.exports = {
var camelizedNamePlural = inflection.pluralize(camelizedName);
attr = dsAttr(dasherizedForeignModelSingular, dasherizedType);
attrs.push(camelizedNamePlural + ': ' + attr);
shouldImportHasMany = true;
} else if (/belongs-to/.test(dasherizedType)) {
attr = dsAttr(dasherizedForeignModel, dasherizedType);
attrs.push(camelizedName + ': ' + attr);
shouldImportBelongsTo = true;
} else {
attr = dsAttr(dasherizedName, dasherizedType);
attrs.push(camelizedName + ': ' + attr);
shouldImportAttr = true;
}

if (/has-many|belongs-to/.test(dasherizedType)) {
needs.push("'model:" + dasherizedForeignModelSingular + "'");
}
}

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

if (shouldImportAttr) {
importStatements.push('import attr from \'ember-data/attr\';');
}

if (shouldImportBelongsTo && shouldImportHasMany) {
importStatements.push('import { belongsTo, hasMany } from \'ember-data/relationships\';');
} else if (shouldImportBelongsTo) {
importStatements.push('import { belongsTo } from \'ember-data/relationships\';');
} else if (shouldImportHasMany) {
importStatements.push('import { hasMany } from \'ember-data/relationships\';');
}

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

return {
importStatements: importStatements,
attrs: attrs,
needs: needs
};
Expand All @@ -64,14 +86,14 @@ module.exports = {
function dsAttr(name, type) {
switch (type) {
case 'belongs-to':
return 'DS.belongsTo(\'' + name + '\')';
return 'belongsTo(\'' + name + '\')';
case 'has-many':
return 'DS.hasMany(\'' + name + '\')';
return '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/
return 'DS.attr()';
return 'attr()';
default:
return 'DS.attr(\'' + type + '\')';
return 'attr(\'' + type + '\')';
}
}
4 changes: 2 additions & 2 deletions blueprints/serializer/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DS from 'ember-data';
import JSONAPISerializer from 'ember-data/serializers/json-api';

export default DS.JSONAPISerializer.extend({
export default JSONAPISerializer.extend({
});
4 changes: 2 additions & 2 deletions blueprints/transform/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import DS from 'ember-data';
import Transform from 'ember-data/transform';

export default DS.Transform.extend({
export default Transform.extend({
deserialize(serialized) {
return serialized;
},
Expand Down
49 changes: 49 additions & 0 deletions node-tests/blueprints/adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,55 @@ describe('Acceptance: generate and destroy adapter blueprints', function() {
});
});

it('adapter with --base-class', function() {
return generateAndDestroy(['adapter', 'foo', '--base-class=bar'], {
files: [
{
file: 'app/adapters/foo.js',
contains: [
'import BarAdapter from \'./bar\';',
'export default BarAdapter.extend({'
]
},
{
file: 'tests/unit/adapters/foo-test.js',
contains: [
'moduleFor(\'adapter:foo\''
]
}
]
});
});

it('adapter throws when --base-class is same as name', function() {
return generateAndDestroy(['adapter', 'application', '--base-class=application'], {
throws: {
message: /Adapters cannot extend from themself/,
type: 'SilentError'
}
});
});

it('adapter when is named "application"', function() {
return generateAndDestroy(['adapter', 'application'], {
files: [
{
file: 'app/adapters/application.js',
contains: [
'import JSONAPIAdapter from \'ember-data/adapters/json-api\';',
'export default JSONAPIAdapter.extend({'
]
},
{
file: 'tests/unit/adapters/application-test.js',
contains: [
'moduleFor(\'adapter:application\''
]
}
]
});
});

it('adapter-test', function() {
return generateAndDestroy(['adapter-test', 'foo'], {
files: [
Expand Down
131 changes: 129 additions & 2 deletions node-tests/blueprints/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ describe('Acceptance: generate and destroy model blueprints', function() {
{
file: 'app/models/foo.js',
contains: [
'import DS from \'ember-data\';',
'export default DS.Model.extend('
'import Model from \'ember-data/model\';',
'export default Model.extend('
],
doesNotContain: [
'import attr from \'ember-data/attr\';',
'import { belongsTo } from \'ember-data/relationships\';',
'import { hasMany } from \'ember-data/relationships\';',
'import { belongsTo, hasMany } from \'ember-data/relationships\';'
]
},
{
Expand All @@ -25,6 +31,127 @@ describe('Acceptance: generate and destroy model blueprints', function() {
});
});

it('model with attrs', function() {
return generateAndDestroy([
'model',
'foo',
'misc',
'skills:array',
'isActive:boolean',
'birthday:date',
'someObject:object',
'age:number',
'name:string',
'customAttr:custom-transform'
], {
files: [
{
file: 'app/models/foo.js',
contains: [
'import Model from \'ember-data/model\';',
'import attr from \'ember-data/attr\';',
'export default Model.extend(',
'misc: attr()',
'skills: attr(\'array\')',
'isActive: attr(\'boolean\')',
'birthday: attr(\'date\')',
'someObject: attr(\'object\')',
'age: attr(\'number\')',
'name: attr(\'string\')',
'customAttr: attr(\'custom-transform\')'
],
doesNotContain: [
'import { belongsTo } from \'ember-data/relationships\';',
'import { hasMany } from \'ember-data/relationships\';',
'import { belongsTo, hasMany } from \'ember-data/relationships\';'
]
},
{
file: 'tests/unit/models/foo-test.js',
contains: [
'moduleForModel(\'foo\''
]
}
]
});
});

it('model with belongsTo', function() {
return generateAndDestroy(['model', 'comment', 'post:belongs-to', 'author:belongs-to:user'], {
files: [
{
file: 'app/models/comment.js',
contains: [
'import Model from \'ember-data/model\';',
'import { belongsTo } from \'ember-data/relationships\';',
'export default Model.extend(',
'post: belongsTo(\'post\')',
'author: belongsTo(\'user\')',
],
doesNotContain: [
'import attr from \'ember-data/attr\';',
'import { hasMany } from \'ember-data/relationships\';',
'import { belongsTo, hasMany } from \'ember-data/relationships\';'
]
},
{
file: 'tests/unit/models/comment-test.js',
contains: [
'moduleForModel(\'comment\'',
'needs: [\'model:post\', \'model:user\']'
]
}
]
});
});

it('model with hasMany', function() {
return generateAndDestroy(['model', 'post', 'comments:has-many', 'otherComments:has-many:comment'], {
files: [
{
file: 'app/models/post.js',
contains: [
'import Model from \'ember-data/model\';',
'import { hasMany } from \'ember-data/relationships\';',
'export default Model.extend(',
'comments: hasMany(\'comment\')',
'otherComments: hasMany(\'comment\')',
],
doesNotContain: [
'import attr from \'ember-data/attr\';',
'import { belongsTo } from \'ember-data/relationships\';',
'import { belongsTo, hasMany } from \'ember-data/relationships\';'
]
},
{
file: 'tests/unit/models/post-test.js',
contains: [
'moduleForModel(\'post\'',
'needs: [\'model:comment\']'
]
}
]
});
});

it('model with belongsTo and hasMany has both imports', function() {
return generateAndDestroy(['model', 'post', 'comments:has-many', 'user:belongs-to'], {
files: [
{
file: 'app/models/post.js',
contains: [
'import { belongsTo, hasMany } from \'ember-data/relationships\';'
],
doesNotContain: [
'import attr from \'ember-data/attr\';',
'import { belongsTo } from \'ember-data/relationships\';',
'import { hasMany } from \'ember-data/relationships\';'
]
}
]
});
});

it('model-test', function() {
return generateAndDestroy(['model-test', 'foo'], {
files: [
Expand Down
4 changes: 2 additions & 2 deletions node-tests/blueprints/serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('Acceptance: generate and destroy serializer blueprints', function() {
{
file: 'app/serializers/foo.js',
contains: [
'import DS from \'ember-data\';',
'export default DS.JSONAPISerializer.extend('
'import JSONAPISerializer from \'ember-data/serializers/json-api\';',
'export default JSONAPISerializer.extend('
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions node-tests/blueprints/transform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('Acceptance: generate and destroy transform blueprints', function() {
{
file: 'app/transforms/foo.js',
contains: [
'import DS from \'ember-data\';',
'export default DS.Transform.extend(',
'import Transform from \'ember-data/transform\';',
'export default Transform.extend(',
'deserialize(serialized) {',
'serialize(deserialized) {'
]
Expand Down

0 comments on commit 1be6c62

Please sign in to comment.