Commit 6bb5bf07 authored by Bertrand PINEL's avatar Bertrand PINEL

First working version (not mentioning relationship handling). Still an error...

First working version (not mentioning relationship handling). Still an error to fix when saving new data with Mirage
parent 7772bd37
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
headers: {
"Content-Type": "application/json; charset=utf-8"
}
});
export { default } from 'ember-aws-ehipster/adapters/application';
......@@ -4,6 +4,6 @@ import {
} from 'ember-cli-mirage';
export default Factory.extend({
<%= fact.length ? '//default generated factory\n\t' + fact + '\n' : '' %>
<%= factories.length ? '//default generated factory\n\t' + factories + '\n' : '' %>
})
\ No newline at end of file
import Controller from '@ember/controller';
import { computed } from '@ember/object';
import { A } from '@ember/array';
import EmberObject from '@ember/object';
export default Controller.extend({
isAddingEntry: false,
<%=ctrlVars%>
<%=entity%>TableColumns: computed(function() {
<%=singularEntityName%>TableColumns: computed(function() {
var col = A([
<%=cols%>
<%=tableCols%>
]);
return col;
}),
<%=entity%>TableContent: computed(function() {
<%=singularEntityName%>TableContent: computed(function() {
return this.get("model");
}),
actions: {
submit() {
this.set('addEntryModal', false);
console.log("Adding new entry");
},
event.preventDefault();
let newEntity = this.store.createRecord('<%=singularEntityName%>',<%=toCreateEntity%>);
newEntity.save();
this.set('addEntryModal', false).then((entry) => {
console.log("new entry of id "+entry.get('id')+" created");
});
}
}
});
\ No newline at end of file
......@@ -2,7 +2,7 @@ import Route from '@ember/routing/route';
export default Route.extend({
model() {
return this.store.findAll('<%=entity%>');
return this.store.findAll('<%=singularEntityName%>');
}
});
\ No newline at end of file
{{outlet}}
<div class="container-fluid">
<h2>List of <%=entity%></h2>
<h2>List of <%=singularEntityName%> Entities</h2>
{{models-table
data=<%=entity%>TableContent
columns=<%=entity%>TableColumns}}
data=<%=singularEntityName%>TableContent
columns=<%=singularEntityName%>TableColumns}}
{{#bs-button onClick=(action (mut addEntryModal) true)}}
Add new entry
......@@ -14,7 +14,6 @@
{{#modal.header}}
<h4 class="modal-title">
Add a new Entry
<div class="badge">2</div>
</h4>
{{/modal.header}}
{{#modal.body}}
......
......@@ -3,6 +3,7 @@ const stringUtils = require('ember-cli-string-utils');
const EOL = require('os').EOL;
const path = require('path');
const fs = require('fs');
const nbFakeData = 8;
module.exports = {
description: 'Generates an ember-data model and the associated Mirage couterpart and screens.',
......@@ -37,15 +38,30 @@ module.exports = {
},
locals(options) {
let attrs = [], cols = [], needs = [], fact = [], form = [], imports = {}, ctrlVars= [], paramObject = [];
// Used in app/models/__name__.js
let attrs = []; // list the entity attributes with their types
// Used in app/controllers/entity-factories/__name__.js
let tableCols = []; // list the colums to be display in the table
let ctrlVars= []; // list the attributes to be mapped to the form for creating new entity
let toCreateEntity = []; // Map attribute to value collected in the form
// Used in app/controllers/entity-factories/__name__.js and app/routes/__name__.js
let singularEntityName = options.entity.name;
// Used in app/templates/__name__hbs
let form = ['{{#bs-form formLayout="vertical" model=this onSubmit=(action "submit") as |form|}}'];
//let form = [];
// Used in mirage/factories/__name__.js
let factories = [];
// Used in mirage/models/__name__.js
let imports = {};
let entityOptions = options.entity.options;
let entityName = options.entity.name;
let capitalizeEntityName = entityName.capitalize() ;
const rel = Object.keys(entityOptions).filter(key =>
(/has-many/.test(entityOptions[key]) || /belongs-to/.test(entityOptions[key])))
.map(key => '\n\t\t' + stringUtils.camelize(key) + relFunction(entityOptions[key]))
const rel = Object.keys(entityOptions)
.filter(key => (/has-many/.test(entityOptions[key]) || /belongs-to/.test(entityOptions[key])))
.map(key => '\n\t\t' + stringUtils.camelize(key) + relFunction(entityOptions[key]))
let needs = []; //
Object.values(entityOptions).forEach(value => {
if (/has-many/.test(value)) {
imports.hasMany = true
......@@ -79,34 +95,40 @@ module.exports = {
} else {
attr = dsAttr(dasherizedName, dasherizedType);
attrs.push(camelizedName + ': ' + attr);
cols.push("Ember.Object.create({\n\tpropertyName: \""+camelizedName+"\",\n\ttitle: \""+name+"\"\n}),");
tableCols.push("EmberObject.create({\n\tpropertyName: \""+camelizedName+"\",\n\ttitle: \""+name+"\"\n}),");
}
if (/has-many|belongs-to/.test(dasherizedType)) {
needs.push("'model:" + dasherizedForeignModelSingular + "'");
}
if (!/has-many/.test(dasherizedType) && !/belongs-to/.test(dasherizedType)) {
fact.push('\n\t' + camelizedName + fakerize(dasherizedType));
factories.push('\n\t' + camelizedName + fakerize(dasherizedType));
// todo Generate input form for creating entity (without any relationship)
form.push('{{input type="text" name="'+camelizedName+'" value='+camelizedName+' placeholder="'+camelizedName+'"}}');
let buttonType = "text";
if (type === 'boolean') {
buttonType = "checkbox";
}
form.push('{{form.element controlType="'+buttonType+'" label="'+camelizedName+'" value='+camelizedName+' onChange=(action (mut '+camelizedName+')) placeholder="'+camelizedName+'"}}');
ctrlVars.push(camelizedName+': null,');
toCreateEntity.push(camelizedName+": this."+camelizedName);
}
}
form.push('{{/bs-form}}');
let needsDeduplicated = needs.filter(function(need, i) {
return needs.indexOf(need) === i;
});
attrs = attrs.join(',' + EOL + ' ');
needs = ' needs: [' + needsDeduplicated.join(', ') + ']';
cols = cols.join(EOL);
tableCols = tableCols.join(EOL);
toCreateEntity = '{'+toCreateEntity.join(',')+'}';
return {
entity: entityName,
singularEntityName: singularEntityName,
attrs: attrs,
cols: cols,
needs: needs,
fact: fact,
tableCols: tableCols,
toCreateEntity: toCreateEntity,
factories: factories,
rel: rel,
ctrlVars: ctrlVars.join(EOL),
form: form.join(EOL),
......@@ -128,7 +150,7 @@ module.exports = {
// Complete /mirage/scenarios/default.js
let mirageScenarioPath = (options.dummy) ? "tests/dummy/mirage/scenarios/default.js" : "mirage/scenarios/default.js";
let scenarioLine = '\tserver.createList\(\''+entityName+'\', 15\);\n';
let scenarioLine = '\tserver.createList\(\''+entityName+'\', '+nbFakeData+'\);\n';
if (!fs.existsSync(mirageScenarioPath)) {
this.ui.writeLine("Creating file "+mirageScenarioPath);
let scenarioContent = "export default function(server) {\n"+scenarioLine+"}\n";
......@@ -139,13 +161,18 @@ module.exports = {
// Complete /mirage/config.js
let mirageConfigPath = (options.dummy) ? "tests/dummy/mirage/config.js" : "mirage/config.js";
let configLine = "this.get('/"+inflection.pluralize(entityName)+"', '"+inflection.pluralize(entityName)+"');\n"
let configLine = "\t\tthis.get('/"+inflection.pluralize(entityName)+"', '"+inflection.pluralize(entityName)+"');"
if (!fs.existsSync(mirageConfigPath)) {
this.ui.writeLine("Creating file "+mirageConfigPath);
let configContent = "export default function() {\n\tthis.namespace = '';\n"+configLine+"}\n";
let configContent = "import ENV from './../config/environment';\n"+
"export default function() {\n"+
"\tthis.namespace = '';\n"+
"\tif (!ENV.APP.proxy) {\n"+configLine+"\n\t} else {\n"+
"\t\tthis.passthrough();\n\t}\n}\n";
fs.writeFileSync(mirageConfigPath, configContent, 'utf-8','w+');
} else {
addLineToFile(this, mirageConfigPath, /this\.namespace = \'\';/, configLine);
//addLineToFile(this, mirageConfigPath, /this\.namespace = \'\';/, configLine);
addLineToFile(this, mirageConfigPath, /if \(ENV\.APP\.proxy\) {/, configLine);
}
// Add route in router.js for the entity page
......
{
"name": "ember-aws-ehipster",
"version": "0.0.4",
"version": "0.0.6",
"description": "The default blueprint for ember-cli addons.",
"keywords": [
"ember-addon"
......
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Adapter | application', function(hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function(assert) {
let adapter = this.owner.lookup('adapter:application');
assert.ok(adapter);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment