Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #7 from olalonde/master

Added singleton resources functionality
  • Loading branch information...
commit 384f578e90bb57b1488d97526a70e46ab288451f 2 parents 6f05378 + 832f869
Anatoliy Chakkaev authored October 04, 2012
20  README.md
Source Rendered
@@ -58,6 +58,26 @@ In that case your handler should be:
58 58
 
59 59
 http://railwayjs.com/routing.html
60 60
 
  61
+### Singleton resources
  62
+
  63
+To create a singleton resource, set the singleton_for property of the
  64
+params hash to the resource you want the singleton resource to map to.
  65
+
  66
+Example usage:
  67
+
  68
+    map.resources('users');
  69
+    map.resources('account', { 
  70
+      singleton_for: 'users', 
  71
+      middleware: function(req, res, next) {
  72
+        // Set the id parameter since we don't get it from the URL
  73
+        // like is usually the case for normal resources.
  74
+        if(req.user)
  75
+          req.params.id = req.user.id;
  76
+        else 
  77
+          return res.redirect('/login');
  78
+        next(); 
  79
+    }});
  80
+
61 81
 ## Example app
62 82
 
63 83
 Check out [example app][1] to deal with middleware, route handling, and generic routes:
15  lib/railway_routes.js
@@ -260,12 +260,14 @@ Map.prototype.resources = function (name, params, actions) {
260 260
             // params.path setting allows to override common path component
261 261
             var effectivePath = (params.path || name) + path;
262 262
 
  263
+            var controller = (params.singleton_for) ? params.singleton_for : name;
  264
+
263 265
             // and call map.{get|post|update|delete}
264 266
             // with the path, controller, middleware and options
265 267
             this[method.toLowerCase()].call(
266 268
                 this,
267 269
                 effectivePath,
268  
-                name + '#' + action,
  270
+                controller + '#' + action,
269 271
                 skipMiddleware ? [] : params.middleware,
270 272
                 getParams(action, params)
271 273
             );
@@ -283,8 +285,19 @@ Map.prototype.resources = function (name, params, actions) {
283 285
             ,   'destroy': 'DELETE  /:id'
284 286
             ,   'update':  'PUT     /:id'
285 287
             ,   'show':    'GET     /:id'
  288
+            },
  289
+            availableRoutesSingleton = 
  290
+            {   'show':    'GET     /'
  291
+            ,   'create':  'POST    /'
  292
+            ,   'new':     'GET     /new'
  293
+            ,   'edit':    'GET     /edit'
  294
+            ,   'destroy': 'DELETE  /'
  295
+            ,   'update':  'PUT     /'
286 296
             };
287 297
 
  298
+        if(params.singleton_for)
  299
+          availableRoutes = availableRoutesSingleton;
  300
+
288 301
         // 1. only
289 302
         if (params.only) {
290 303
             if (typeof params.only == 'string') {

0 notes on commit 384f578

Please sign in to comment.
Something went wrong with that request. Please try again.