How to invoke a controller action from another controller in-memory?
I have a rails server that serves mobile clients(native) and a browser based client. Most of the trafic comes from the browser based client. The browser based client goes through an intermediary rails server to get the views. The central server exposed REST based services.
[ Iphone +---------------+
Blackberry | |
Android ]<-------------------------------------->| Central Rails|
| Server |
| |
[ Browser ]<------->[intermediary server]<-------->| |
+---------------+
System is in production for last 8 months. Recently, during one of the performance tuning exercises, we noticed that XML serialization/de-serialization costs are very high for ActiveResource communications. So we are trying to co-locate the intermediary server and the central rails server controllers in rails s开发者_如何转开发erver instance.
I can rewrite the Intermediary controllers to directly use the models. This approach requires rewriting lot of code and I will loose all the authorization checks enforced through the central controllers.
I am trying to figure out how to invoke the central server controllers from the intermediary server controller without incurring the serialization/deserialization costs. Ideally, I would like the existing central controllers to return an in memory object rather than a XML string.
Eg:
Central controller:
class ActiveRecord::Base
def to_ar
self
end
end
class UserController
def show
respond_to do |format|
format.xml { render :xml => @user }
format.json { render :json => @user }
format.ar { render :ar => @user }
end
end
end
Central controller:
app.get '/users/1.ar'
@user = app.response.body
Essentially, I am trying to mimic the behavior of ActionController::IntegrationTest classes with actual ActiveRecord objects as body.
Any pointers/advises will be highly appreciated.
PS: I am on Rails 2.3.9.
精彩评论