Skip to content

Commit

Permalink
add suport for pluralize resource routes ref wejs/we#160
Browse files Browse the repository at this point in the history
  • Loading branch information
albertosouza committed Jan 1, 2017
1 parent 921b73b commit f97b377
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"moment": "2.17.1",
"morgan": "1.7.0",
"node-uuid": "1.4.7",
"pluralize": "^3.1.0",
"request": "2.79.0",
"sanitize-html": "1.13.0",
"sequelize": "3.27.0",
Expand Down
4 changes: 3 additions & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ module.exports = function loadPlugin (projectPath, Plugin) {
}
}
},
router: {},
router: {
pluralize: false
},
JSONApi: {
sendSubRecordAttributes: false
},
Expand Down
5 changes: 4 additions & 1 deletion src/Router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const _ = require('lodash'),
cors = require('cors'),
S = require('string'),
mime = require('mime'),
pluralize = require('pluralize'),
// absolute url regex tester
absoluteUrlRegex = new RegExp('^(?:[a-z]+:)?//', 'i');
/**
Expand Down Expand Up @@ -153,6 +154,7 @@ Router.prototype.bindResource = function bindResource (opts) {

let we = this.we,
paramIdNamePrefix = S(opts.name).camelize().s,
pluralizedName = pluralize.plural(opts.name),
router = this;

// get related Model
Expand All @@ -161,12 +163,13 @@ Router.prototype.bindResource = function bindResource (opts) {
if (!Model) throw new Error('Resource Model '+opts.name+' not found and is required in bind resource');
// set default options
_.defaults(opts, {
pluralizedName: pluralizedName,
paramIdName: paramIdNamePrefix+'Id',
routeId: ':'+ paramIdNamePrefix +(opts.idFormat || 'Id([0-9]+)'),
namePrefix: '',
templateFolderPrefix: '',
itemTitleHandler: 'i18n',
rootRoute: '/' + opts.name
rootRoute: '/' + ((we.config.router.pluralize)? pluralizedName: opts.name)
});

if (opts.namespace) opts.rootRoute = opts.namespace + opts.rootRoute;
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ We.prototype = {
if (!cb) cb = function(){}; // cb is optional

let we = this;

we.hooks.trigger('we:server:before:start' ,we ,function afterRunBeforeServerStart (err) {
if (err) return cb(err);
/**
Expand Down
1 change: 0 additions & 1 deletion test/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ before(function (callback) {
});
});


// prepare we.js core and load app features:
before(function (callback) {
this.slow(100);
Expand Down

0 comments on commit f97b377

Please sign in to comment.