Permalink
Browse files

need to get named tokens from routes into params so we can get show-t…

…ype actions working
  • Loading branch information...
1 parent f514546 commit 7a28b1a8d727d0090a9c49a5f03f435177e9f0f4 @remi committed Dec 2, 2009
Showing with 56 additions and 23 deletions.
  1. +33 −19 rails/rails.js
  2. +13 −1 spec/rails/app/controllers/dogs_controller.js
  3. +5 −1 spec/rails/rails_spec.rb
  4. +5 −2 spec/rails/routes.js
View
@@ -5,23 +5,6 @@ req(uire('util/makeClass'));
req(uire('sinatra/haml'));
req(uire('sinatra/sinatra')); // we really just want the Sinatra.Router ...
-// GLOBAL FUNCTIONS
-function render_view(view, variables){
- //var text = File.read(this.app.root + '/app/views/' + view + '.haml');
- //var response = [200, {}, [ Haml.to_html(Haml.parse.call(variables, text)) ]];
-
- var response = [200, {}, [ "hello, this: " + this ]];
- if (arguments.callee.caller != null) // called from within function
- return response;
- else
- return function(){ return response };
-}
-
-function params(key){
- write('params');
- return 1;
-}
-
// global Rails namespace
var Rails = {};
@@ -39,7 +22,23 @@ Rails.Controller.prototype = {
return [200, {}, [text]];
},
- call_action: function(action){
+ render_view: function(view, variables){
+ return this.app.render_view(view, variables, arguments);
+ },
+
+ params: function(key){
+ var params = coll2hash(Request.Form());
+ each(this.env.QUERY_STRINGS, function(key, value){
+ params[key] = value;
+ });
+ if (key != null)
+ return params[key];
+ else
+ return params;
+ },
+
+ call_action: function(action, env){
+ this.env = env;
var action = this.actions[action];
if (action != null)
return action.call(this);
@@ -63,6 +62,9 @@ Rails.Application.prototype = {
// initialize controllers
this.controllers = [];
+
+ // hack ... set some variables here so they're available 'globally' to controllers
+ var render_view = function(view, vars){ return function(){ return this.render_view(view, vars); }; };
req(uire(this.root + '/app/controllers/dogs_controller')); // make this dynamic!
// initialize routes
@@ -78,7 +80,7 @@ Rails.Application.prototype = {
if (route != null){
var controller = this.controllers[ route.data.controller.toLowerCase() ];
- return controller.call_action(route.data.action);
+ return controller.call_action(route.data.action, env);
} else
return [ 404, {}, ['Could not find route for ' + method + ' ' + path] ];
@@ -90,6 +92,18 @@ Rails.Application.prototype = {
controller: function(name, actions){
this.controllers[name.toLowerCase()] = new Rails.Controller(this, name, actions);
+ },
+
+ render_view: function(view, variables, args){
+ var text = File.read(this.root + '/app/views/' + view + '.haml');
+ var response = [200, {}, [ Haml.to_html(Haml.parse.call(variables, text)) ]];
+
+ if (args == null) args = arguments;
+ if (args.callee.caller == null){ // called from within function
+ return function(){ return response };
+ } else {
+ return response;
+ }
}
};
@@ -1,6 +1,18 @@
this.controller( 'Dogs', {
- index: render_view('dogs/index', { dogs: Dog.all() })
+ index: function(){
+ return this.render_view('dogs/index', { dogs: Dog.all() })
+ },
+
+ index2: this.render_view('dogs/index', { dogs: Dog.all() }),
+
+ index3: render_view('dogs/index', { dogs: Dog.all() }),
+
+ // index4: render_view({ dogs: Dog.all() })
+
+ show: function(){
+ return [200, {}, ['hello there! params: ' + JSON.stringify(this.params())]];
+ }
// show: render_view('dogs/show', { dog: Dog.get(params('id')) })
View
@@ -5,7 +5,11 @@
before(:all){ setup :rails }
it 'should be able to render text for an arbitrary controller action' do
- get('/dogs').should == 'Hello from DogsController index action'
+ get('/dogs').should include('Dogs Index')
+
+ # a few alternative syntaxes
+ get('/dogs2').should include('Dogs Index')
+ get('/dogs3').should include('Dogs Index')
end
it 'should be able to render a template for an arbitrary controller action'
View
@@ -2,7 +2,10 @@ this.routes(function(map){
// map.resources('dogs');
- map.get('/dogs', { controller: 'dogs', action: 'index' });
- map.get('/dogs/:id', { controller: 'dogs', action: 'show' });
+ map.get('/dogs', { controller: 'dogs', action: 'index' });
+ map.get('/dogs2', { controller: 'dogs', action: 'index2' });
+ map.get('/dogs3', { controller: 'dogs', action: 'index3' });
+
+ map.get('/dogs/:id', { controller: 'dogs', action: 'show' });
});

0 comments on commit 7a28b1a

Please sign in to comment.