Skip to content

Commit

Permalink
Update blueprints to import modules directly
Browse files Browse the repository at this point in the history
- Import JSONAPIAdapter module directly in blueprint
- Import JSONAPISerializer module directly in blueprint
- Import Transform module directly in blueprint
- Import attr module directly in model blueprints when needed
- Import belongsTo and hasMany modules directly in blueprints when needed
  • Loading branch information
HeroicEric committed Jan 15, 2016
1 parent fb9f4f1 commit bcab3b5
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 38 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 %>

This comment has been minimized.

Copy link
@samchrisinger

samchrisinger May 3, 2016

I think I get the general motivation here (and recognizing the desire not to create jshint errors with a blueprint) but this feels like a less useful default to me than just:
import DS from 'ember-data';

Maybe I'm just not pro enough, but I tend to just:
ember g model my-model
which gives me
import Model from 'ember-data/model';
I then consistently change the code back to the old default.

Is there a use case for a DS.Model without any DS.attrs?


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
4 changes: 2 additions & 2 deletions node-tests/blueprints/adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ describe('Acceptance: generate and destroy adapter blueprints', function() {
{
file: 'app/adapters/application.js',
contains: [
'import DS from \'ember-data\';',
'export default DS.JSONAPIAdapter.extend({'
'import JSONAPIAdapter from \'ember-data/adapters/json-api\';',
'export default JSONAPIAdapter.extend({'
]
},
{
Expand Down
82 changes: 62 additions & 20 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 Down Expand Up @@ -42,16 +48,22 @@ describe('Acceptance: generate and destroy model blueprints', function() {
{
file: 'app/models/foo.js',
contains: [
'import DS from \'ember-data\';',
'export default DS.Model.extend(',
'misc: DS.attr()',
'skills: DS.attr(\'array\')',
'isActive: DS.attr(\'boolean\')',
'birthday: DS.attr(\'date\')',
'someObject: DS.attr(\'object\')',
'age: DS.attr(\'number\')',
'name: DS.attr(\'string\')',
'customAttr: DS.attr(\'custom-transform\')'
'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\';'
]
},
{
Expand All @@ -70,10 +82,16 @@ describe('Acceptance: generate and destroy model blueprints', function() {
{
file: 'app/models/comment.js',
contains: [
'import DS from \'ember-data\';',
'export default DS.Model.extend(',
'post: DS.belongsTo(\'post\')',
'author: DS.belongsTo(\'user\')',
'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\';'
]
},
{
Expand All @@ -93,10 +111,16 @@ describe('Acceptance: generate and destroy model blueprints', function() {
{
file: 'app/models/post.js',
contains: [
'import DS from \'ember-data\';',
'export default DS.Model.extend(',
'comments: DS.hasMany(\'comment\')',
'otherComments: DS.hasMany(\'comment\')',
'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\';'
]
},
{
Expand All @@ -110,6 +134,24 @@ describe('Acceptance: generate and destroy model blueprints', function() {
});
});

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 bcab3b5

Please sign in to comment.