Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

A lazy way of doing singular resources #9

Closed
wants to merge 4 commits into from

3 participants

Nigel Chapman TJ Holowaychuk Andrey Mereskin
Nigel Chapman

I'm not 100% sure this works in all cases, but exporting id as the empty string from a resource with only the singular actions, plus a bit of special-casing on that, and it seems to fall out.

Please take a look when you have a moment.

Nigel Chapman

I just merged a load of upstream changes, and some things broke. I don't have a lot of time to look for the problem, so if you are not going to use this approach to singular resources, maybe I should close this request. If you do want to use the code, I'll look into it further.

TJ Holowaychuk
Owner
tj commented

thought I had something similar already, I'll have to take a closer look

Andrey Mereskin mereskin closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 20, 2011
  1. Nigel Chapman

    trace routes

    macavon authored
  2. Nigel Chapman

    singular resources?

    macavon authored
  3. Nigel Chapman
Commits on Apr 22, 2011
  1. Nigel Chapman
This page is out of date. Refresh to see the latest.
2  .gitignore
View
@@ -0,0 +1,2 @@
+*.tmproj
+.DS_Store
5 index.js
View
@@ -29,9 +29,10 @@ var Resource = module.exports = function Resource(name, actions, app) {
this.app = app;
this.routes = {};
actions = actions || {};
+ if (name && lingo.en.isSingular(name)) actions.id = (actions.id || '');
this.format = actions.format;
- this.id = actions.id || this.defaultId;
- this.param = ':' + this.id;
+ this.id = actions.id == null? this.defaultId: actions.id;
+ this.param = this.id == ''? '': ':' + this.id;
// default actions
for (var key in actions) {
27 test/fixtures/profile.js
View
@@ -0,0 +1,27 @@
+
+exports.new = function(req, res){
+ res.send('new profile');
+};
+
+exports.create = function(req, res){
+ res.send('create profile');
+};
+
+exports.show = function(req, res){
+ res.send('show profile');
+};
+
+exports.edit = function(req, res){
+ res.send('edit profile');
+};
+
+exports.update = function(req, res){
+ res.send('update profile');
+};
+
+exports.destroy = function(req, res){
+ res.send('destroy profile');
+};
+
+// exports.id = '';
+
14 test/resource.test.js
View
@@ -199,10 +199,10 @@ module.exports = {
}};
actions.load = Forum.get;
- var forum = app.resource('forum', actions);
+ var forum = app.resource('forums', actions);
assert.response(app,
- { url: '/forum/12' },
+ { url: '/forums/12' },
{ body: 'Ferrets' });
},
@@ -215,13 +215,13 @@ module.exports = {
res.end(req.forum.title + ': ' + req.thread.title);
}};
- var forum = app.resource('forum', { load: Forum.get });
- var threads = app.resource('thread', actions, { load: Thread.get });
+ var forum = app.resource('forums', { load: Forum.get });
+ var threads = app.resource('threads', actions, { load: Thread.get });
forum.add(threads);
assert.response(app,
- { url: '/forum/12/thread/1' },
+ { url: '/forums/12/threads/1' },
{ body: 'Ferrets: Tobi rules' });
},
@@ -233,11 +233,11 @@ module.exports = {
res.end(req.forum.title);
}};
- var forum = app.resource('forum', actions);
+ var forum = app.resource('forums', actions);
forum.load(Forum.get);
assert.response(app,
- { url: '/forum/12' },
+ { url: '/forums/12' },
{ body: 'Ferrets' });
},
76 test/singular.test.js
View
@@ -0,0 +1,76 @@
+
+/**
+ * Module dependencies.
+ */
+var assert = require('assert')
+ , express = require('express')
+ , Resource = require('../');
+
+module.exports = {
+ 'test app.singular_resource()': function(){
+ var app = express.createServer();
+
+ var ret = app.resource('profile', require('./fixtures/profile'));
+ assert.ok(ret instanceof Resource);
+
+ assert.response(app,
+ { url: '/profiles' },
+ { status: 404 });
+
+ assert.response(app,
+ { url: '/profile/new' },
+ { body: 'new profile' });
+
+ assert.response(app,
+ { url: '/profile', method: 'POST' },
+ { body: 'create profile' });
+
+ assert.response(app,
+ { url: '/profile' },
+ { body: 'show profile' });
+
+ assert.response(app,
+ { url: '/profile/edit' },
+ { body: 'edit profile' });
+
+ assert.response(app,
+ { url: '/profile', method: 'PUT' },
+ { body: 'update profile' });
+
+ assert.response(app,
+ { url: '/profile', method: 'DELETE' },
+ { body: 'destroy profile' });
+ },
+
+ 'test app.singular_resource() for top level': function(){
+ var app = express.createServer();
+
+ var ret = app.resource(require('./fixtures/profile'));
+ assert.ok(ret instanceof Resource);
+
+ assert.response(app,
+ { url: '/new' },
+ { body: 'new profile' });
+
+ assert.response(app,
+ { url: '/', method: 'POST' },
+ { body: 'create profile' });
+
+ assert.response(app,
+ { url: '/' },
+ { body: 'show profile' });
+
+ assert.response(app,
+ { url: '/edit' },
+ { body: 'edit profile' });
+
+ assert.response(app,
+ { url: '/', method: 'PUT' },
+ { body: 'update profile' });
+
+ assert.response(app,
+ { url: '/', method: 'DELETE' },
+ { body: 'destroy profile' });
+ }
+
+};
31 test/trace_routes.js
View
@@ -0,0 +1,31 @@
+var assert = require('assert')
+ , express = require('express')
+ , Resource = require('../');
+
+var app = express.createServer();
+
+var ret = app.resource('profile', require('./fixtures/profile'));
+
+
+Resource.prototype.list_routes = function() {
+ var rs = '';
+ for (var method in this.routes) {
+ rs += method + '>>\n';
+
+ var routes = this.routes[method];
+ for (var key in routes) {
+ var r = routes[key];
+ rs += r.method + ': ' + r.path + '\n';
+ }
+ }
+ return rs;
+};
+
+console.log(ret.list_routes());
+
+
+module.exports = {
+ 'dummy': function() {
+ assert.equal(1, 1);
+ }
+}
Something went wrong with that request. Please try again.