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

ember-data should provide its blueprints #3813

Merged
merged 7 commits into from
Dec 18, 2015
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
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ bower.json
Brocfile.js
testem.json
*.gem
node-tests/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ script:
- ./bin/lint-features
- npm run-script test
- npm run-script test:optional-features
- npm run-script node-tests
after_success:
- npm run-script production
- "./bin/bower-ember-data-build"
Expand Down
12 changes: 12 additions & 0 deletions blueprints/adapter-test/files/tests/unit/__path__/__test__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('adapter:<%= dasherizedModuleName %>', '<%= friendlyTestDescription %>', {
// Specify the other units that are required for this test.
// needs: ['serializer:foo']
});

// Replace this with your real tests.
test('it exists', function(assert) {
let adapter = this.subject();
assert.ok(adapter);
});
12 changes: 12 additions & 0 deletions blueprints/adapter-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*jshint node:true*/

var testInfo = require('ember-cli-test-info');

module.exports = {
description: 'Generates an ember-data adapter unit test',
locals: function(options) {
return {
friendlyTestDescription: testInfo.description(options.entity.name, "Unit", "Adapter")
};
}
};
4 changes: 4 additions & 0 deletions blueprints/adapter/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= importStatement %>

export default <%= baseClass %>.extend({
});
44 changes: 44 additions & 0 deletions blueprints/adapter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*jshint node:true*/

var stringUtil = require('ember-cli-string-utils');
var SilentError = require('silent-error');
var pathUtil = require('ember-cli-path-utils');

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

availableOptions: [
{ name: 'base-class', type: String }
],

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

if (options.pod && options.podPath) {
relativePath = pathUtil.getRelativePath(options.podPath + options.entity.name);
}

if (!isAddon && !options.baseClass && adapterName !== 'application') {
options.baseClass = 'application';
}

if (options.baseClass === adapterName) {
throw new SilentError('Adapters cannot extend from themself. To resolve this, remove the `--base-class` option or change to a different base-class.');
}

if (options.baseClass) {
baseClass = stringUtil.classify(options.baseClass.replace('\/', '-'));
baseClass = baseClass + 'Adapter';
importStatement = 'import ' + baseClass + ' from \'' + relativePath + options.baseClass + '\';';
}

return {
importStatement: importStatement,
baseClass: baseClass
};
}
};
12 changes: 12 additions & 0 deletions blueprints/model-test/files/tests/unit/__path__/__test__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { moduleForModel, test } from 'ember-qunit';

moduleForModel('<%= dasherizedModuleName %>', '<%= friendlyDescription %>', {
// Specify the other units that are required for this test.
<%= typeof needs !== 'undefined' ? needs : '' %>
});

test('it exists', function(assert) {
let model = this.subject();
// let store = this.store();
assert.ok(!!model);
});
16 changes: 16 additions & 0 deletions blueprints/model-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*jshint node:true*/

var ModelBlueprint = require('../model');
var testInfo = require('ember-cli-test-info');

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

locals: function(options) {
var result = ModelBlueprint.locals.apply(this, arguments);

result.friendlyDescription = testInfo.description(options.entity.name, "Unit", "Model");

return result;
}
};
25 changes: 25 additions & 0 deletions blueprints/model/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<grey>You may generate models with as many attrs as you would like to pass. The following attribute types are supported:</grey>
<yellow><attr-name></yellow>
<yellow><attr-name></yellow>:array
<yellow><attr-name></yellow>:boolean
<yellow><attr-name></yellow>:date
<yellow><attr-name></yellow>:object
<yellow><attr-name></yellow>:number
<yellow><attr-name></yellow>:string
<yellow><attr-name></yellow>:your-custom-transform
<yellow><attr-name></yellow>:belongs-to:<yellow><model-name></yellow>
<yellow><attr-name></yellow>:has-many:<yellow><model-name></yellow>

For instance: <green>\`ember generate model taco filling:belongs-to:protein toppings:has-many:toppings name:string price:number misc\`</green>
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()
});
```
5 changes: 5 additions & 0 deletions blueprints/model/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import DS from 'ember-data';

export default DS.Model.extend({
<%= attrs %>
});
77 changes: 77 additions & 0 deletions blueprints/model/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*jshint node:true*/

var inflection = require('inflection');
var stringUtils = require('ember-cli-string-utils');
var EOL = require('os').EOL;

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

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

locals: function(options) {
var attrs = [];
var needs = [];
var entityOptions = options.entity.options;

for (var name in entityOptions) {
var type = entityOptions[name] || '';
var 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);

var attr;
if (/has-many/.test(dasherizedType)) {
var camelizedNamePlural = inflection.pluralize(camelizedName);
attr = dsAttr(dasherizedForeignModelSingular, dasherizedType);
attrs.push(camelizedNamePlural + ': ' + attr);
} else if (/belongs-to/.test(dasherizedType)) {
attr = dsAttr(dasherizedForeignModel, dasherizedType);
attrs.push(camelizedName + ': ' + attr);
} else {
attr = dsAttr(dasherizedName, dasherizedType);
attrs.push(camelizedName + ': ' + attr);
}

if (/has-many|belongs-to/.test(dasherizedType)) {
needs.push("'model:" + dasherizedForeignModelSingular + "'");
}
}
var needsDeduplicated = needs.filter(function(need, i) {
return needs.indexOf(need) === i;
});

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

return {
attrs: attrs,
needs: needs
};
}
};

function dsAttr(name, type) {
switch (type) {
case 'belongs-to':
return 'DS.belongsTo(\'' + name + '\')';
case 'has-many':
return 'DS.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()';
default:
return 'DS.attr(\'' + type + '\')';
}
}
15 changes: 15 additions & 0 deletions blueprints/serializer-test/files/tests/unit/__path__/__test__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { moduleForModel, test } from 'ember-qunit';

moduleForModel('<%= dasherizedModuleName %>', '<%= friendlyTestDescription %>', {
// Specify the other units that are required for this test.
needs: ['serializer:<%= dasherizedModuleName %>']
});

// Replace this with your real tests.
test('it serializes records', function(assert) {
let record = this.subject();

let serializedRecord = record.serialize();

assert.ok(serializedRecord);
});
12 changes: 12 additions & 0 deletions blueprints/serializer-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*jshint node:true*/

var testInfo = require('ember-cli-test-info');

module.exports = {
description: 'Generates a serializer unit test.',
locals: function(options) {
return {
friendlyTestDescription: testInfo.description(options.entity.name, "Unit", "Serializer")
};
},
};
4 changes: 4 additions & 0 deletions blueprints/serializer/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import DS from 'ember-data';

export default DS.RESTSerializer.extend({
});
5 changes: 5 additions & 0 deletions blueprints/serializer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*jshint node:true*/

module.exports = {
description: 'Generates an ember-data serializer.'
};
12 changes: 12 additions & 0 deletions blueprints/transform-test/files/tests/unit/__path__/__test__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('transform:<%= dasherizedModuleName %>', '<%= friendlyTestDescription %>', {
// Specify the other units that are required for this test.
// needs: ['serializer:foo']
});

// Replace this with your real tests.
test('it exists', function(assert) {
let transform = this.subject();
assert.ok(transform);
});
12 changes: 12 additions & 0 deletions blueprints/transform-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*jshint node:true*/

var testInfo = require('ember-cli-test-info');

module.exports = {
description: 'Generates a transform unit test.',
locals: function(options) {
return {
friendlyTestDescription: testInfo.description(options.entity.name, "Unit", "Transform")
};
},
};
11 changes: 11 additions & 0 deletions blueprints/transform/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import DS from 'ember-data';

export default DS.Transform.extend({
deserialize(serialized) {
return serialized;
},

serialize(deserialized) {
return deserialized;
}
});
5 changes: 5 additions & 0 deletions blueprints/transform/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*jshint node:true*/

module.exports = {
description: 'Generates an ember-data value transform.'
};
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* jshint node: true */
'use strict';

var path = require('path');

module.exports = {
name: 'ember-data',

Expand Down Expand Up @@ -37,6 +39,10 @@ module.exports = {
}
},

blueprintsPath: function() {
return path.join(__dirname, 'blueprints');
},

treeForAddon: function(dir) {
if (this._forceBowerUsage) {
// Fakes an empty broccoli tree
Expand Down
40 changes: 40 additions & 0 deletions node-tests/blueprints/adapter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var setupTestHooks = require('ember-cli-blueprint-test-helpers/lib/helpers/setup');
var BlueprintHelpers = require('ember-cli-blueprint-test-helpers/lib/helpers/blueprint-helper');
var generateAndDestroy = BlueprintHelpers.generateAndDestroy;

describe('Acceptance: generate and destroy adapter blueprints', function() {
setupTestHooks(this);

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

it('adapter-test', function() {
return generateAndDestroy(['adapter-test', 'foo'], {
files: [
{
file: 'tests/unit/adapters/foo-test.js',
contains: [
'moduleFor(\'adapter:foo\''
]
}
]
});
});
});
Loading