Skip to content

Commit

Permalink
Convert view generator to new names
Browse files Browse the repository at this point in the history
  • Loading branch information
venables committed Nov 29, 2015
1 parent 0407c18 commit 5e85deb
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 95 deletions.
6 changes: 1 addition & 5 deletions generators/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ function replaceAllPlaceholdersWithAppName(appPath, appName, callback) {

function replacePlaceholderWithAppName(filename, appName, callback) {
var underscored = s.underscored(appName);
var camelCased = s.camelize(underscored);
var classCased = s.classify(underscored);
var dashed = s.dasherize(underscored);
var contents;

Expand All @@ -85,9 +83,7 @@ function replacePlaceholderWithAppName(filename, appName, callback) {
},
function writeFile(next) {
var replacedContent = contents
.replaceAll('KALE_CAMEL_CASED_NAME', camelCased)
.replaceAll('KALE_CLASS_CASED_NAME', classCased)
.replaceAll('KALE_UNDERSCORED_NAME', underscored)
.replaceAll('kale_records', underscored)
.replaceAll('KALE_APP_NAME', appName)
.replaceAll('KALE_DASHERIZED_NAME', dashed);

Expand Down
2 changes: 1 addition & 1 deletion generators/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function _controllerTemplate(className, singular, plural) {
let template = _template('controller.js');

return template
.replace(/KaleClass/g, className)
.replace(/KaleRecord/g, className)
.replace(/kaleRecords/g, plural)
.replace(/kaleRecord/g, singular);
}
Expand Down
2 changes: 1 addition & 1 deletion generators/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function _migrationTemplate(tableName) {

function _modelTemplate(className, tableName) {
return _template('model.js')
.replace(/KaleClass/g, className)
.replace(/KaleRecord/g, className)
.replace(/kale_records/g, tableName);
}

Expand Down
14 changes: 7 additions & 7 deletions generators/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ function replaceContents(filename, names, callback) {
},
function writeFile(next) {
var replacedContent = contents
.replaceAll('SINGULAR_NAME_CAPITALIZED', names.singular.capitalized)
.replaceAll('SINGULAR_NAME_LOWERCASE', names.singular.lowercase)
.replaceAll('PLURAL_NAME_CAPITALIZED', names.plural.capitalized)
.replaceAll('PLURAL_NAME_LOWERCASE_DASHED', names.plural.lowercaseDashed)
.replaceAll('PLURAL_NAME_LOWERCASE_CAMELIZED', names.plural.lowercaseCamelized)
.replaceAll('PLURAL_NAME_LOWERCASE', names.plural.lowercase);
.replaceAll('KaleRecord', names.singular.capitalized)
.replaceAll('kaleRecord', names.singular.lowercase)
.replaceAll('KaleRecords', names.plural.capitalized)
.replaceAll('kale-records', names.plural.lowercaseDashed)
.replaceAll('kaleRecords', names.plural.lowercaseCamelized)
.replaceAll('kaleRecords', names.plural.lowercase);

fs.writeFile(filename, replacedContent, next);
}
Expand Down Expand Up @@ -114,7 +114,7 @@ function installJavascript(names, callback) {
function installViews(names, callback) {
var templatePath = path.join(__dirname, '..', 'templates', 'view');
var viewsPath = path.join(templatePath, 'views');
var outputPath = path.join('.', 'app', 'assets', 'views', `${names.plural.lowercaseDashed}`);
var outputPath = path.join('.', 'app', 'assets', 'views', `${names.plural.lowercaseCamelized}`);

async.series([
function copyViews(next) {
Expand Down
4 changes: 2 additions & 2 deletions templates/app/bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if [ ! -f .env ]; then
fi

# Create and migrate the database
createuser -s KALE_UNDERSCORED_NAME
createdb KALE_UNDERSCORED_NAME_development -U KALE_UNDERSCORED_NAME
createuser -s kale_records
createdb kale_records_development -U kale_records

kale migrate
4 changes: 2 additions & 2 deletions templates/app/config/environments/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = {
db: {
connection: {
host: '127.0.0.1',
user: 'KALE_UNDERSCORED_NAME',
database: 'KALE_UNDERSCORED_NAME_development'
user: 'kale_records',
database: 'kale_records_development'
},
debug: true
},
Expand Down
16 changes: 8 additions & 8 deletions templates/controller/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const _ = require('lodash');

const index = (ctx) => {
return ctx.models.KaleClass.collection()
return ctx.models.KaleRecord.collection()
.query(function(knex) {
knex.limit(50).offset(0).orderBy('created_at', 'asc');
})
Expand All @@ -16,7 +16,7 @@ const index = (ctx) => {
};

const show = (ctx) => {
return _fetchKaleClass(ctx).then((kaleRecord) => {
return _fetchKaleRecord(ctx).then((kaleRecord) => {
ctx.body = {
kaleRecord
};
Expand All @@ -25,7 +25,7 @@ const show = (ctx) => {

const create = (ctx) => {
let params = _kaleRecordParams(ctx.request.body);
let kaleRecord = new ctx.models.KaleClass(params);
let kaleRecord = new ctx.models.KaleRecord(params);

return kaleRecord.save().then((kaleRecord) => {
ctx.status = 201;
Expand All @@ -36,7 +36,7 @@ const create = (ctx) => {
};

const update = (ctx) => {
return _fetchKaleClass(ctx).then((kaleRecord) => {
return _fetchKaleRecord(ctx).then((kaleRecord) => {
let params = _kaleRecordParams(ctx.request.body);

return kaleRecord.save(params, { patch: true });
Expand All @@ -49,19 +49,19 @@ const update = (ctx) => {
};

const destroy = (ctx) => {
return _fetchKaleClass(ctx).then((kaleRecord) => {
return _fetchKaleRecord(ctx).then((kaleRecord) => {
return kaleRecord.destroy();
}).then(() => {
ctx.status = 204;
});
};

function _fetchKaleClass(ctx) {
return ctx.models.KaleClass.forge({ id: ctx.params.id }).fetch({ require: true });
function _fetchKaleRecord(ctx) {
return ctx.models.KaleRecord.forge({ id: ctx.params.id }).fetch({ require: true });
}

function _kaleRecordParams(body) {
// TODO: Whitelist params for creating & updating a KaleClass.
// TODO: Whitelist params for creating & updating a KaleRecord.
return _.pick(body, '');
}

Expand Down
4 changes: 2 additions & 2 deletions templates/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const bookshelf = require('../../db');

const KaleClass = bookshelf.Model.extend({
const KaleRecord = bookshelf.Model.extend({
tableName: 'kale_records',
hasTimestamps: true
});

module.exports = bookshelf.model('KaleClass', KaleClass);
module.exports = bookshelf.model('KaleRecord', KaleRecord);
8 changes: 4 additions & 4 deletions templates/view/javascripts/controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('./PLURAL_NAME_LOWERCASE_CAMELIZED/edit');
require('./PLURAL_NAME_LOWERCASE_CAMELIZED/index');
require('./PLURAL_NAME_LOWERCASE_CAMELIZED/new');
require('./PLURAL_NAME_LOWERCASE_CAMELIZED/show');
require('./kaleRecords/edit');
require('./kaleRecords/index');
require('./kaleRecords/new');
require('./kaleRecords/show');
10 changes: 5 additions & 5 deletions templates/view/javascripts/controller/edit.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

angular.module('app.controllers')
.controller('PLURAL_NAME_CAPITALIZEDEditController', ['$scope', '$state', 'SINGULAR_NAME_CAPITALIZED', 'SINGULAR_NAME_LOWERCASE',
function($scope, $state, SINGULAR_NAME_CAPITALIZED, SINGULAR_NAME_LOWERCASE) {
$scope.SINGULAR_NAME_LOWERCASE = SINGULAR_NAME_LOWERCASE;
.controller('KaleRecordsEditController', ['$scope', '$state', 'KaleRecord', 'kaleRecord',
function($scope, $state, KaleRecord, kaleRecord) {
$scope.kaleRecord = kaleRecord;

$scope.update = function() {
new SINGULAR_NAME_CAPITALIZED($scope.SINGULAR_NAME_LOWERCASE).$update(function() {
$state.go('PLURAL_NAME_LOWERCASE.index');
new KaleRecord($scope.kaleRecord).$update(function() {
$state.go('kaleRecords.index');
});
};
}
Expand Down
10 changes: 5 additions & 5 deletions templates/view/javascripts/controller/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

angular.module('app.controllers')
.controller('PLURAL_NAME_CAPITALIZEDIndexController', ['$scope', '$state', 'SINGULAR_NAME_CAPITALIZED', 'PLURAL_NAME_LOWERCASE',
function($scope, $state, SINGULAR_NAME_CAPITALIZED, PLURAL_NAME_LOWERCASE) {
$scope.PLURAL_NAME_LOWERCASE = PLURAL_NAME_LOWERCASE;
.controller('KaleRecordsIndexController', ['$scope', '$state', 'KaleRecord', 'kaleRecords',
function($scope, $state, KaleRecord, kaleRecords) {
$scope.kaleRecords = kaleRecords;

$scope.destroy = function(SINGULAR_NAME_LOWERCASE) {
SINGULAR_NAME_CAPITALIZED.delete({ id: SINGULAR_NAME_LOWERCASE.id }, function() {
$scope.destroy = function(kaleRecord) {
KaleRecord.delete({ id: kaleRecord.id }, function() {
$state.go($state.current, {}, { reload: true });
});
};
Expand Down
10 changes: 5 additions & 5 deletions templates/view/javascripts/controller/new.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

angular.module('app.controllers')
.controller('PLURAL_NAME_CAPITALIZEDNewController', ['$scope', '$state', '$stateParams', 'SINGULAR_NAME_CAPITALIZED',
function($scope, $state, $stateParams, SINGULAR_NAME_CAPITALIZED) {
$scope.SINGULAR_NAME_LOWERCASE = new SINGULAR_NAME_CAPITALIZED();
.controller('KaleRecordsNewController', ['$scope', '$state', '$stateParams', 'KaleRecord',
function($scope, $state, $stateParams, KaleRecord) {
$scope.kaleRecord = new KaleRecord();

$scope.create = function() {
$scope.SINGULAR_NAME_LOWERCASE.$save(function() {
$state.go('PLURAL_NAME_LOWERCASE.index');
$scope.kaleRecord.$save(function() {
$state.go('kaleRecords.index');
});
};
}
Expand Down
6 changes: 3 additions & 3 deletions templates/view/javascripts/controller/show.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

angular.module('app.controllers')
.controller('PLURAL_NAME_CAPITALIZEDShowController', ['$scope', 'SINGULAR_NAME_LOWERCASE',
function($scope, SINGULAR_NAME_LOWERCASE) {
$scope.SINGULAR_NAME_LOWERCASE = SINGULAR_NAME_LOWERCASE;
.controller('KaleRecordsShowController', ['$scope', 'kaleRecord',
function($scope, kaleRecord) {
$scope.kaleRecord = kaleRecord;
}
]);
60 changes: 30 additions & 30 deletions templates/view/javascripts/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ angular.module('app')
.config([ '$stateProvider',
function($stateProvider) {
$stateProvider
.state('PLURAL_NAME_LOWERCASE_DASHED', {
.state('kaleRecords', {
abstract: true,
templateUrl: '/assets/views/layouts/application.html'
})
.state('PLURAL_NAME_LOWERCASE_DASHED.index', {
url: '/PLURAL_NAME_LOWERCASE_DASHED',
templateUrl: '/assets/views/PLURAL_NAME_LOWERCASE_DASHED/index.html',
controller: 'PLURAL_NAME_CAPITALIZEDIndexController',
.state('kaleRecords.index', {
url: '/kale-records',
templateUrl: '/assets/views/kaleRecords/index.html',
controller: 'KaleRecordsIndexController',
resolve: {
PLURAL_NAME_LOWERCASE: ['$q', 'SINGULAR_NAME_CAPITALIZED', function($q, SINGULAR_NAME_CAPITALIZED) {
kaleRecords: ['$q', 'KaleRecord', function($q, KaleRecord) {
var deferred = $q.defer();

SINGULAR_NAME_CAPITALIZED.get(function(body) {
deferred.resolve(body.PLURAL_NAME_LOWERCASE);
KaleRecord.get(function(body) {
deferred.resolve(body.kaleRecords);
}, function() {
deferred.resolve([]);
});
Expand All @@ -26,46 +26,46 @@ angular.module('app')
}]
}
})
.state('PLURAL_NAME_LOWERCASE_DASHED.new', {
url: '/PLURAL_NAME_LOWERCASE_DASHED/new',
templateUrl: '/assets/views/PLURAL_NAME_LOWERCASE_DASHED/new.html',
controller: 'PLURAL_NAME_CAPITALIZEDNewController'
.state('kaleRecords.new', {
url: '/kale-records/new',
templateUrl: '/assets/views/kaleRecords/new.html',
controller: 'KaleRecordsNewController'
})
.state('PLURAL_NAME_LOWERCASE_DASHED.show', {
url: '/PLURAL_NAME_LOWERCASE_DASHED/:id',
templateUrl: '/assets/views/PLURAL_NAME_LOWERCASE_DASHED/show.html',
controller: 'PLURAL_NAME_CAPITALIZEDShowController',
.state('kaleRecords.show', {
url: '/kale-records/:id',
templateUrl: '/assets/views/kaleRecords/show.html',
controller: 'KaleRecordsShowController',
resolve: {
SINGULAR_NAME_LOWERCASE: ['$q', '$state', '$stateParams', 'SINGULAR_NAME_CAPITALIZED',
function($q, $state, $stateParams, SINGULAR_NAME_CAPITALIZED) {
kaleRecord: ['$q', '$state', '$stateParams', 'KaleRecord',
function($q, $state, $stateParams, KaleRecord) {
var deferred = $q.defer();

SINGULAR_NAME_CAPITALIZED.get({ id: $stateParams.id }, function(body) {
deferred.resolve(body.SINGULAR_NAME_LOWERCASE);
KaleRecord.get({ id: $stateParams.id }, function(body) {
deferred.resolve(body.kaleRecord);
}, function() {
deferred.reject();
$state.go('PLURAL_NAME_LOWERCASE');
$state.go('kaleRecords');
});

return deferred.promise;
}
]
}
})
.state('PLURAL_NAME_LOWERCASE_DASHED.edit', {
url: '/PLURAL_NAME_LOWERCASE_DASHED/:id/edit',
templateUrl: '/assets/views/PLURAL_NAME_LOWERCASE_DASHED/edit.html',
controller: 'PLURAL_NAME_CAPITALIZEDEditController',
.state('kaleRecords.edit', {
url: '/kale-records/:id/edit',
templateUrl: '/assets/views/kaleRecords/edit.html',
controller: 'KaleRecordsEditController',
resolve: {
SINGULAR_NAME_LOWERCASE: ['$q', '$state', '$stateParams', 'SINGULAR_NAME_CAPITALIZED',
function($q, $state, $stateParams, SINGULAR_NAME_CAPITALIZED) {
kaleRecord: ['$q', '$state', '$stateParams', 'KaleRecord',
function($q, $state, $stateParams, KaleRecord) {
var deferred = $q.defer();

SINGULAR_NAME_CAPITALIZED.get({ id: $stateParams.id }, function(body) {
deferred.resolve(body.SINGULAR_NAME_LOWERCASE);
KaleRecord.get({ id: $stateParams.id }, function(body) {
deferred.resolve(body.kaleRecord);
}, function() {
deferred.reject();
$state.go('PLURAL_NAME_LOWERCASE');
$state.go('kaleRecords');
});

return deferred.promise;
Expand Down
4 changes: 2 additions & 2 deletions templates/view/javascripts/service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

angular.module('app.services')
.factory('SINGULAR_NAME_CAPITALIZED', ['$resource', function($resource) {
return $resource('/api/v1/PLURAL_NAME_LOWERCASE_DASHED/:id', { id: '@id' }, {
.factory('KaleRecord', ['$resource', function($resource) {
return $resource('/api/v1/kale-records/:id', { id: '@id' }, {
update: {
method: 'PUT'
}
Expand Down
2 changes: 1 addition & 1 deletion templates/view/views/_form.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" ng-model="SINGULAR_NAME_LOWERCASE.name" class="form-control" id="name" placeholder="name">
<input type="text" ng-model="kaleRecord.name" class="form-control" id="name" placeholder="name">
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion templates/view/views/edit.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<form class="form-horizontal" role="form" ng-submit="update()">
<div ng-include="'/assets/views/PLURAL_NAME_LOWERCASE_DASHED/_form.html'"></div>
<div ng-include="'/assets/views/kaleRecords/_form.html'"></div>
</form>
14 changes: 7 additions & 7 deletions templates/view/views/index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<h1>PLURAL_NAME_CAPITALIZED</h1>
<h1>KaleRecords</h1>

<table class="table">
<tr ng-repeat="SINGULAR_NAME_LOWERCASE in PLURAL_NAME_LOWERCASE">
<td>{{ SINGULAR_NAME_LOWERCASE.id }}</td>
<tr ng-repeat="kaleRecord in kaleRecords">
<td>{{ kaleRecord.id }}</td>
<td>
<a ui-sref="PLURAL_NAME_LOWERCASE_DASHED.show({ id: SINGULAR_NAME_LOWERCASE.id })">View</a>
<a ui-sref="PLURAL_NAME_LOWERCASE_DASHED.edit({ id: SINGULAR_NAME_LOWERCASE.id })">Edit</a>
<a ng-click="destroy(SINGULAR_NAME_LOWERCASE)">Delete</a>
<a ui-sref="kaleRecords.show({ id: kaleRecord.id })">View</a>
<a ui-sref="kaleRecords.edit({ id: kaleRecord.id })">Edit</a>
<a ng-click="destroy(kaleRecord)">Delete</a>
</td>
</tr>
</table>

<a ui-sref="PLURAL_NAME_LOWERCASE_DASHED.new">New SINGULAR_NAME_CAPITALIZED</a>
<a ui-sref="kaleRecords.new">New KaleRecord</a>
2 changes: 1 addition & 1 deletion templates/view/views/new.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<form class="form-horizontal" role="form" ng-submit="create()">
<div ng-include="'/assets/views/PLURAL_NAME_LOWERCASE_DASHED/_form.html'"></div>
<div ng-include="'/assets/views/kaleRecords/_form.html'"></div>
</form>
Loading

0 comments on commit 5e85deb

Please sign in to comment.