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

Add 'helper' blueprint #240

Merged
merged 2 commits into from
Oct 26, 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
3 changes: 3 additions & 0 deletions app/components/file-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default Ember.Component.extend({
addComponent() {
this.attrs.addComponent();
},
addHelper() {
this.attrs.addHelper();
},
addFile(type) {
this.attrs.addFile(type);
},
Expand Down
18 changes: 18 additions & 0 deletions app/gist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@ export default Ember.Controller.extend({
});
},

addHelper() {
let type = 'helper';
let fileProperties = this.get('emberCli').buildProperties(type);
let filePath = prompt('File path', fileProperties.filePath);
let splitFilePath = filePath.split('/');
let file = splitFilePath[splitFilePath.length - 1];
let name = file.replace('.js', '').camelize();

fileProperties = this.get('emberCli').buildProperties(type, {
camelizedModuleName: name
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see the addFile method being more like this in the future.


if (this.isPathInvalid(type, filePath)) {
return;
}
this.createFile(filePath, fileProperties);
},

/**
* Add a new file to the model
* @param {String|null} type Blueprint name or null for empty file
Expand Down
3 changes: 2 additions & 1 deletion app/gist/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
activeEditorCol=activeEditorCol
activeFile=activeFile
addFile=(action "addFile")
addHelper=(action "addHelper")
addComponent=(action "addComponent")
renameFile=(action "renameFile")
removeFile=(action "removeFile")
deleteGist=(action "deleteGist")
saveGist="saveGist"
fork="fork"
copy="copy"
deleteGist=(action "deleteGist")
signInViaGithub="signInViaGithub"}}

{{editor-mode-menu setKeyMap=(action "setEditorKeyMap")}}
Expand Down
18 changes: 16 additions & 2 deletions app/services/ember-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const availableBlueprints = {
blueprint: 'model',
filePath: 'models/my-model.js',
},
'helper': {
blueprint: 'helper',
filePath: 'helpers/my-helper.js'
},
'route': {
blueprint: 'route',
filePath: 'my-route/route.js',
Expand Down Expand Up @@ -108,13 +112,23 @@ export default Ember.Service.extend({
return this.store.createRecord('gistFile', this.buildProperties(type));
},

buildProperties(type) {
buildProperties(type, replacements) {
if (type in availableBlueprints) {
let blueprint = availableBlueprints[type];
let content = blueprints[blueprint.blueprint];

if (replacements) {
Object.keys(replacements).forEach(key => {
let token = `<%= ${key} %>`;
let value = replacements[key];

content = content.replace(new RegExp(token, 'g'), value);
});
}

return {
filePath: blueprint.filePath,
content: blueprints[blueprint.blueprint].replace(/<\%\=(.*)\%\>/gi,'')
content: content.replace(/<\%\=(.*)\%\>/gi,'')
};
}
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should get a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

Expand Down
1 change: 1 addition & 0 deletions app/templates/components/file-menu.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<li><a {{action 'addFile' 'route'}}>Route</a></li>
<li><a {{action 'addFile' 'template'}} class="test-template-action">Template</a></li>
<li><a {{action 'addFile' 'service'}} class="test-add-service-link">Service</a></li>
<li><a {{action 'addHelper' 'helper'}} class="test-add-helper-link">Helper</a></li>
<li><a {{action 'addFile' 'router'}}>Router</a></li>
<li><a {{action 'addFile' 'css'}}>CSS</a></li>
</ul>
Expand Down
1 change: 1 addition & 0 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function getEmberCLIBlueprints() {
'router': 'app/files/app/router.js',
'service': 'service/files/__root__/__path__/__name__.js',
'template': 'template/files/__root__/__path__/__name__.hbs',
'helper': 'helper/files/__root__/helpers/__name__.js'
};

for (var blueprintName in cliBlueprintFiles) {
Expand Down
26 changes: 26 additions & 0 deletions tests/acceptance/gist-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,32 @@ test('can add service', function(assert){
});
});

test('can add helper', function(assert){
assert.expect(3);

let origFileCount;
promptValue = 'helpers/my-helper.js';
visit('/');
andThen(function(){
origFileCount = find(firstFilePickerFiles).length;
});

click(fileMenu);
click('.test-add-helper-link');
click(firstFilePicker);

andThen(function() {
let numFiles = find(firstFilePickerFiles).length;
assert.equal(numFiles, origFileCount + 1, 'Added helper file');

let fileNames = findMapText(`${firstFilePickerFiles} a`);
assert.equal(fileNames[3], promptValue, 'Added the file with the right name');

let columnFiles = findMapText(displayedFiles);
assert.ok(columnFiles.contains(promptValue), 'Added file is displayed');
});
});


test('unsaved indicator', function(assert) {
const indicator = ".test-unsaved-indicator";
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/services/ember-cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,29 @@ test("updateDependencyVersion() updates the version of ember-template-compiler i
assert.equal(parsed.dependencies['ember-template-compiler'], 'release');
});
});

test("buildProperties() works as expected without replacements", function (assert) {
assert.expect(3);

var service = this.subject();
var props = service.buildProperties('helper');

assert.equal(props.filePath, 'helpers/my-helper.js', 'filePath set');
assert.ok(props.content, 'has content');
assert.ok(props.content.indexOf('<%=') === -1, 'No replacement tags in content');
});

test("buildProperties() works as expected with replacements", function (assert) {
assert.expect(5);

var service = this.subject();
var props = service.buildProperties('helper', {
camelizedModuleName: 'myHelper'
});

assert.equal(props.filePath, 'helpers/my-helper.js', 'filePath set');
assert.ok(props.content, 'has content');
assert.ok(props.content.indexOf('<%=') === -1, 'No replacement tags in content');
assert.ok(props.content.indexOf('myHelper(params') !== -1, 'Replacements worked');
assert.ok(props.content.indexOf('helper(myHelper)') !== -1, 'Replacements worked if multiple');
});