Skip to content

Commit

Permalink
Added support for create/update routing, for _method param override.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed May 5, 2010
1 parent 98475f4 commit 2b96485
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.markdown
Expand Up @@ -153,6 +153,9 @@ POST */snow_dogs[.extension]<br/>
GET */snow_dogs/:id[.extension]<br/> GET */snow_dogs/:id[.extension]<br/>
(SnowDogs controller, show action) (SnowDogs controller, show action)


GET */snow_dogs/:id[.extension];edit<br/>
(SnowDogs controller, edit action)

PUT */snow_dogs/:id[.extension]<br/> PUT */snow_dogs/:id[.extension]<br/>
(SnowDogs controller, update action) (SnowDogs controller, update action)


Expand Down
13 changes: 8 additions & 5 deletions geddy-core/lib/app.js
Expand Up @@ -36,7 +36,11 @@ var App = function (initData) {
// Split only on question mark -- using semicolon delimiter for // Split only on question mark -- using semicolon delimiter for
// edit-flag hack in resource-routes // edit-flag hack in resource-routes
var base = url.split(/\?|=/)[0]; var base = url.split(/\?|=/)[0];
var route = router.parse(base, req.method); var qs = url.split('?')[1] || '';
var qsParams = fleegix.url.qsToObject(qs);
var method = (req.method == 'POST' && qsParams._method) ?
qsParams._method : req.method;
var route = router.parse(base, method);


try { try {
// If the route is a match, run the matching controller/action // If the route is a match, run the matching controller/action
Expand All @@ -53,10 +57,9 @@ var App = function (initData) {


// Split only on question mark -- using semicolon delimiter for // Split only on question mark -- using semicolon delimiter for
// edit-flag hack in resource-routes // edit-flag hack in resource-routes
var qs = url.split('?')[1] || ''; var params;
var qsParams = fleegix.url.qsToObject(qs); params = util.meta.mixin({}, route.params);
var params = util.meta.mixin({}, route.params); params = util.meta.mixin(params, qsParams);
var params = util.meta.mixin(params, qsParams);


// Instantiate the matching controller from the registry // Instantiate the matching controller from the registry
var constructor = controllerRegistry[route.controller]; var constructor = controllerRegistry[route.controller];
Expand Down
12 changes: 8 additions & 4 deletions geddy-core/lib/controller.js
Expand Up @@ -47,7 +47,7 @@ var Controller = function (obj) {
this.respondsWith = ['text']; this.respondsWith = ['text'];
// The name, in lowercase_with_underscores, used for // The name, in lowercase_with_underscores, used for
// picking the template if any to attempt to render // picking the template if any to attempt to render
this.nameDeCamelized = null; this.nameDecamelized = null;
// Content to respond with -- can be an Object or String // Content to respond with -- can be an Object or String
this.content = ''; this.content = '';
// High-level set of options which can represent multiple // High-level set of options which can represent multiple
Expand All @@ -71,7 +71,7 @@ var Controller = function (obj) {
this[p] = obj[p]; this[p] = obj[p];
} }


this.nameDeCamelized = fleegix.string.deCamelize(this.name); this.nameDecamelized = util.string.decamelize(this.name);


}; };


Expand Down Expand Up @@ -148,8 +148,12 @@ Controller.prototype = new function () {
var contr = redir.controller || this.name; var contr = redir.controller || this.name;
var act = redir.action; var act = redir.action;
var ext = redir.extension || this.params.extension; var ext = redir.extension || this.params.extension;
var id = redir.id;
contr = util.string.decamelize(contr); contr = util.string.decamelize(contr);
url = '/' + contr + '/' + act + '.' + ext; url = '/' + contr;
url += act ? '/' + act : '';
url += id ? '/' + id : '';
url += '.' + ext;
} }
var r = new response.Response(this.response); var r = new response.Response(this.response);
var headers = {'Location': url}; var headers = {'Location': url};
Expand Down Expand Up @@ -346,7 +350,7 @@ Controller.prototype = new function () {
this.formatContent = function (format, content) { this.formatContent = function (format, content) {
if (format == 'html') { if (format == 'html') {
var _this = this; var _this = this;
var name = this.nameDeCamelized; var name = this.nameDecamelized;
var act = this.params.action; var act = this.params.action;
// E.g., app/views/snow_dogs/index.html.ejs // E.g., app/views/snow_dogs/index.html.ejs
var path = 'app/views/' + name + '/' + act + '.html.ejs'; var path = 'app/views/' + name + '/' + act + '.html.ejs';
Expand Down
1 change: 1 addition & 0 deletions geddy-core/lib/router.js
Expand Up @@ -100,6 +100,7 @@ var Router = function () {
delete route.params.__editflag__; delete route.params.__editflag__;
route.params.controller = route.controller; route.params.controller = route.controller;
route.params.action = route.action; route.params.action = route.action;
route.params.method = method;
return route; return route;
} }
} }
Expand Down
2 changes: 1 addition & 1 deletion geddy-core/scripts/Jakefile.js
Expand Up @@ -77,7 +77,7 @@ exports.tasks = {
// TODO: No fancy pluralization yet // TODO: No fancy pluralization yet
var namePlural = names[1] || nameSingular + 's'; var namePlural = names[1] || nameSingular + 's';


// Convert underscores to camelCase, e.g., 'neilPeart' // Convert underscores to camelCase, e.g., 'NeilPeart'
var nameSingularConverted = util.string.camelize(nameSingular, true); var nameSingularConverted = util.string.camelize(nameSingular, true);
var namePluralConverted = util.string.camelize(namePlural, true); var namePluralConverted = util.string.camelize(namePlural, true);


Expand Down
7 changes: 4 additions & 3 deletions geddy-core/scripts/gen/resource_controller.ejs
Expand Up @@ -11,8 +11,8 @@ var <%= namePlural %> = function () {
}; };


this.create = function (params) { this.create = function (params) {
// Save the resource, then redirect to index // Save the resource, then display index page
this.redirect({action: 'index'}); this.redirect({controller: this.name});
}; };


this.show = function (params) { this.show = function (params) {
Expand All @@ -24,7 +24,8 @@ var <%= namePlural %> = function () {
}; };


this.update = function (params) { this.update = function (params) {
this.respond({params: params}); // Save the resource, then display the item page
this.redirect({controller: this.name, id: params.id});
}; };


this.remove = function (params) { this.remove = function (params) {
Expand Down

0 comments on commit 2b96485

Please sign in to comment.