Skip to content

Commit

Permalink
Merge pull request #4331 from pangratz/manage-model-modules
Browse files Browse the repository at this point in the history
[BUGFIX beta] Overhaul attr/relationships imports in model blueprint
  • Loading branch information
bmac committed Apr 15, 2016
2 parents 9cbad2b + 2f511a8 commit 9a5e796
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
18 changes: 11 additions & 7 deletions blueprints/model/HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ For instance: <green>\`ember generate model taco filling:belongs-to:protein topp
would result in the following model:

```js
import DS from 'ember-data';
export default DS.Model.extend({
filling: DS.belongsTo('protein'),
toppings: DS.hasMany('topping'),
name: DS.attr('string'),
price: DS.attr('number'),
misc: DS.attr()
import Model from 'ember-data/model';

import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';

export default Model.extend({
filling: belongsTo('protein'),
toppings: hasMany('topping'),
name: attr('string'),
price: attr('number'),
misc: attr()
});
```
6 changes: 6 additions & 0 deletions blueprints/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,20 @@ module.exports = {

if (shouldImportAttr) {
importStatements.push('import attr from \'ember-data/attr\';');
} else {
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\';');
importStatements.push('// import { hasMany } from \'ember-data/relationships\';');
} else if (shouldImportHasMany) {
importStatements.push('// import { belongsTo } from \'ember-data/relationships\';');
importStatements.push('import { hasMany } from \'ember-data/relationships\';');
} else {
importStatements.push('// import { belongsTo, hasMany } from \'ember-data/relationships\';');
}

importStatements = importStatements.join(EOL);
Expand Down
20 changes: 10 additions & 10 deletions node-tests/blueprints/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ describe('Acceptance: generate and destroy model blueprints', function() {
expect(_file('app/models/foo.js'))
.to.contain('import Model from \'ember-data/model\';')
.to.contain('export default Model.extend(')
.to.not.contain('import attr from \'ember-data/attr\';')
.to.contain('// import attr from \'ember-data/attr\';')
.to.contain('// import { belongsTo, hasMany } from \'ember-data/relationships\';')
.to.not.contain('import { belongsTo } from \'ember-data/relationships\';')
.to.not.contain('import { hasMany } from \'ember-data/relationships\';')
.to.not.contain('import { belongsTo, hasMany } from \'ember-data/relationships\';');
.to.not.contain('import { hasMany } from \'ember-data/relationships\';');

expect(_file('tests/unit/models/foo-test.js'))
.to.contain('moduleForModel(\'foo\'');
Expand Down Expand Up @@ -56,9 +56,9 @@ describe('Acceptance: generate and destroy model blueprints', function() {
.to.contain('age: attr(\'number\')')
.to.contain('name: attr(\'string\')')
.to.contain('customAttr: attr(\'custom-transform\')')
.to.contain('// import { belongsTo, hasMany } from \'ember-data/relationships\';')
.to.not.contain('import { belongsTo } from \'ember-data/relationships\';')
.to.not.contain('import { hasMany } from \'ember-data/relationships\';')
.to.not.contain('import { belongsTo, hasMany } from \'ember-data/relationships\';');
.to.not.contain('import { hasMany } from \'ember-data/relationships\';');

expect(_file('tests/unit/models/foo-test.js'))
.to.contain('moduleForModel(\'foo\'');
Expand All @@ -76,8 +76,8 @@ describe('Acceptance: generate and destroy model blueprints', function() {
.to.contain('export default Model.extend(')
.to.contain('post: belongsTo(\'post\')')
.to.contain('author: belongsTo(\'user\')')
.to.not.contain('import attr from \'ember-data/attr\';')
.to.not.contain('import { hasMany } from \'ember-data/relationships\';')
.to.contain('// import attr from \'ember-data/attr\';')
.to.contain('// import { hasMany } from \'ember-data/relationships\';')
.to.not.contain('import { belongsTo, hasMany } from \'ember-data/relationships\';');

expect(_file('tests/unit/models/comment-test.js'))
Expand All @@ -97,8 +97,8 @@ describe('Acceptance: generate and destroy model blueprints', function() {
.to.contain('export default Model.extend(')
.to.contain('comments: hasMany(\'comment\')')
.to.contain('otherComments: hasMany(\'comment\')')
.to.not.contain('import attr from \'ember-data/attr\';')
.to.not.contain('import { belongsTo } from \'ember-data/relationships\';')
.to.contain('// import attr from \'ember-data/attr\';')
.to.contain('// import { belongsTo } from \'ember-data/relationships\';')
.to.not.contain('import { belongsTo, hasMany } from \'ember-data/relationships\';');

expect(_file('tests/unit/models/post-test.js'))
Expand All @@ -114,7 +114,7 @@ describe('Acceptance: generate and destroy model blueprints', function() {
.then(() => emberGenerateDestroy(args, _file => {
expect(_file('app/models/post.js'))
.to.contain('import { belongsTo, hasMany } from \'ember-data/relationships\';')
.to.not.contain('import attr from \'ember-data/attr\';')
.to.contain('// import attr from \'ember-data/attr\';')
.to.not.contain('import { belongsTo } from \'ember-data/relationships\';')
.to.not.contain('import { hasMany } from \'ember-data/relationships\';');
}));
Expand Down

0 comments on commit 9a5e796

Please sign in to comment.