Dynamic restful routes

Restul routes are awesome. All you have to do is add one line to routes file map.resources :users and you are is done.

How about the case where the names of models are not know before hand and ,at run time, you need to handle such cases. Lets take an example. If you have two restful routes for users and houses then they would be defined like this.

 
map.resources :houses
map.resources :users

In this case there is one declaration for each resource. How would you design the routes if the names of resources (houses and users) is supplied at run time and not at development time.

While developing plugin admin_data such a case was handled. This plugin reads the models folder at run time and then it needs to handle each model as a restful resource. It is not a good idea to do map.resources :resource_x for each of the models.

Here is snippet of routes file from admin_data plugin.

 
 admin_data.resources  :on_k,
                       :as => ':klass',
                       :path_prefix => 'admin_data',
                       :controller => 'main'

Now if I want the url for the index method for model user then I would say

 
#index 
admin_data_on_k_index_path(:klass => klass.name.underscore) 

#show
admin_data_on_k_path(:klass => klass.name.underscore, :id => record.id)

#new
new_admin_data_on_k_path(:klass => klass.name.underscore)

They key thing is that by looking at the routes documentation for as you would not guess that it could take a variable.

Not directly related to this blog but admin_data also uses namespaces resources .

Scout Improves App Monitoring for Rails Developers | Rails Fire

Scout Improves App Monitoring for Rails Developers

scoutparts.pngBack in early 2008 I wrote about Scout on Ruby Inside, announcing it as a new "Ruby powered Web monitoring and reporting service." This is still true, except for the "new" bit! I've stayed in touch with the Scout team and they've not slowed down in their work on the system. They've improved the service and added some cool new features for Rails app developers in particular - even going as far as ripping out three months of development work to make the system better!

Scout has matured a lot since last year and now pitches itself as a general server and application monitoring service, though there's a sidefocus on Rails app monitoring. It's a commercial service but the entry plan is only $19 a month and there's a thirty day trial available.

Compared to other services, Scout is interesting because no Rails plugin installation is required and all analysis is separate from the request cycle. Redeployment isn't even necessary. Scout takes a more passive background role while still collecting all of the data it needs.

One of Scout's new features is the incorporation of the Request Log Analyzer by Willem van Bergen and Bart ten Brinke. This tool analyzes Rails log files and produces performance reports based around several metrics: request times, mean request time, process blockers, database and rendering times, HTTP methods used, HTTP statuses, Rails action cache stats, and similar. You can couple this with Scout's regular graphs and e-mail alert systems to get notified about "interesting" occurrences with your applications right as they happen.

Lastly, Scout has a strong plugin ethos, so it's not like it's just a Rails service - though that's what I'm focusing on here. You can write your own plugins to monitor whatever you like.. so even if you do monitor a Rails app, if you want to monitor another service "on the side" and you're willing to write a plugin, you'll be good to go, all on the same account.

Disclaimer: Scout has no relationship with Ruby/Rails Inside, other than having run a job ad on our jobs board a little while back. I promised to write about them while talking with them at RailsConf 2009, however, and genuinely think it's a service worth checking out :-)

Post to Twitter Tweet This Post