Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made action helper generator much more flexible, now creates

non-resource helpers, and support for more complex routes with more than
one param.
  • Loading branch information...
commit 3b00f35e8f456edbbd75756ad0b06dc49924c46a 1 parent fdc2bf1
@larzconwell larzconwell authored
View
2  lib/init/helpers.js
@@ -20,7 +20,7 @@ module.exports = new (function () {
// Create action helpers based on router
var i = router.routes.length;
while (--i >= 0) {
- actionHelpers.create(router.routes[i].params);
+ actionHelpers.create(router.routes[i]);
}
actionHelpers.add(helpers); // Add action helpers to helpers
View
154 lib/template/helpers/action.js
@@ -18,65 +18,151 @@
var helpersBase = require('../helpers')
, utils = require('utilities')
- , items = {};
+ , items = {}
+ , formatPathString;
-// Create a new helper based on a action and controller and add it a helpers list
-exports.create = function (options) {
- if (!options) {
+// Create a new helper from the given route object
+exports.create = function (data) {
+ if (!data || !data.params) {
return;
}
- // Controller name is plural, initial-cap, camel-case (e.g. NealPearts)
- var controllerName = options.controller
- , names = utils.string.getInflections(controllerName)
- , idActions = ['show', 'edit']
- , name, action;
-
- // Return if the action isn't one that should be added
- if (!utils.array.included(options.action, ['index', 'add', 'show', 'edit'])) {
+ // If action is one that shouldn't have a helper skip it
+ if (utils.array.included(data.params.action, ['create', 'update', 'destroy'])) {
return;
}
- // Default action function
- action = helpersBase.urlFor.action(options);
-
- // If action requires Id then change the default action
- if (utils.array.included(options.action, idActions)) {
- action = function (id) {
- options.id = id;
- return helpersBase.urlFor.action(options);
- };
- }
+ var controllerName = data.params.controller
+ , controllerNames = utils.string.getInflections(controllerName)
+ , name = ''
+ , helper = {name: '', action: ''};
// Set the helper names for each action
- switch (options.action) {
+ switch (data.params.action) {
case 'index':
// neilPeartsPath
- name = names.property.plural + 'Path';
+ name = controllerNames.property.plural + 'Path';
break;
case 'add':
// addNeilPeartPath
- name = 'add' + names.constructor.singular + 'Path';
+ name = 'add' + controllerNames.constructor.singular + 'Path';
break;
case 'show':
// neilPeartPath
- name = names.property.singular + 'Path';
+ name = controllerNames.property.singular + 'Path';
break;
case 'edit':
// editNeilPeartPath
- name = 'edit' + names.constructor.singular + 'Path';
+ name = 'edit' + controllerNames.constructor.singular + 'Path';
break;
default:
- // No default
+ // Unknown action name, so just add Path to it
+ name = data.params.action + 'Path';
+ break;
}
+ helper.name = name;
- items[name] = {
- name: name,
- action: action
- };
- return items[name];
-};
+ // Attempt to use the easy way to build the helper
+ if (data.parts) {
+ var args = []
+ , funcBody = 'return ""'
+ , func
+ , val;
+
+ for (var i in data.parts) {
+ val = data.parts[i];
+
+ if (typeof val === 'string') {
+ funcBody += ' + "'+ val + '"';
+ }
+ else if (typeof val === 'object') {
+ // Skip the item if it's just a format
+ if (val.path === '.:format') {
+ continue;
+ }
+
+ if (val.name) {
+ args.push(val.name);
+ funcBody += ' + ' + val.name;
+ }
+ }
+ }
+
+ func = new Function(args, funcBody);
+ if (args.length > 0) {
+ helper.action = func;
+ }
+ else {
+ helper.action = func();
+ }
+ }
+ // Fallback to using the path if it's given
+ else if (data.path) {
+ var path = data.path.replace('(.:format)', '')
+ , pathItems = (path.split(/\//g)).slice(1)
+ , args = []
+ , funcBody = 'return ""'
+ , func
+ , val;
+
+ for (var i in pathItems) {
+ val = pathItems[i];
+ // If the first char is : then it's a param
+ if (val[0] === ':') {
+ val = val.slice(1);
+
+ args.push(val);
+ funcBody += ' + "/" + ' + val;
+ }
+ else {
+ funcBody += ' + "/' + val + '"';
+ }
+ }
+
+ func = new Function(args, funcBody);
+ if (args.length > 0) {
+ helper.action = func;
+ }
+ else {
+ helper.action = func();
+ }
+ }
+ // Finally just use the controller and action params
+ else {
+ // Default action function
+ helper.action = helpersBase.urlFor.action(data.params);
+
+ // If action requires ID then change the default action
+ if (utils.array.included(data.params.action, ['show', 'edit'])) {
+ helper.action = function (id) {
+ data.params.id = id;
+ return helpersBase.urlFor.action(data.params);
+ };
+ }
+ }
+
+ items[name] = helper;
+ return helper;
+
+ /*
+ // Controller name is plural, initial-cap, camel-case (e.g. NealPearts)
+ var controllerName = options.controller
+ , names = utils.string.getInflections(controllerName)
+ , idActions = ['show', 'edit']
+ , name, action;
+
+ // Default action function
+ action = helpersBase.urlFor.action(options);
+
+ // If action requires Id then change the default action
+ if (utils.array.included(options.action, idActions)) {
+ action = function (id) {
+ options.id = id;
+ return helpersBase.urlFor.action(options);
+ };
+ }*/
+};
// Retrieve the list of current action helpers
exports.get = function () {
View
8 test/templates/helpers/action.js
@@ -31,19 +31,19 @@ var actionHelper = require('../../../lib/template/helpers/action')
routeData = [
{
name: 'tasksPath'
- , data: {controller: 'tasks', action: 'index'}
+ , data: {params: {controller: 'tasks', action: 'index'}, parts: ['/tasks', {path: '.:format'}]}
, expected: {name: 'tasksPath', action: '/tasks'}
}, {
name: 'addTaskPath'
- , data: {controller: 'tasks', action: 'add'}
+ , data: {params: {controller: 'tasks', action: 'add'}}
, expected: {name: 'addTaskPath', action: '/tasks/add'}
}, {
name: 'taskPath'
- , data: {controller: 'tasks', action: 'show'}
+ , data: {params: {controller: 'tasks', action: 'show'}, path: '/tasks/:id(.:format)'}
, expected: {name: 'taskPath', action: '/tasks/1'}
}, {
name: 'editTaskPath'
- , data: {controller: 'tasks', action: 'edit'}
+ , data: {params: {controller: 'tasks', action: 'edit'}, path: '/tasks/:id/edit(.:format)'}
, expected: {name: 'editTaskPath', action: '/tasks/1/edit'}
}
];
Please sign in to comment.
Something went wrong with that request. Please try again.