ShorturlRoutes enhances the Rails 3 routing DSL by providing the shorturl method.
This method makes it dead simple to create routes that are at the root of your nameserver, often known as vanity urls.
These urls make it much easier for your users to type out the URL, write it down, and transmit them over twitter because they are easy to remember and shorter than the normal site/controller/name.
Just add
gem 'shorturl_routes', :git => git://github.com/jamuraa/shorturl_routes.git'
to your Gemfile and run bundle install to get set up.
If you have for example a model Group, with the attribute name, you can route all of the names to their show pages like this:
shorturl "/:name", :to => "groups#show", :model => "Group", :attribute => :name
If the object doesn’t exist in the database, then it will fail and fall through like a normal routing rule. This means that you can put them one after the other and the top one takes priority.
shorturl "/:name", :to => "users#show", :model => "User", :attribute => :name shorturl "/:name", :to => "groups#show", :model => "Group", :attribute => :name
ShorturlRoutes uses where and first from Arel, so if your model doesn’t support those methods, you will have to write them. It probably fails miserably on anything that’s not ActiveRecord right now. This is something I’ll be looking at fixing in the future.
ShorturlRoutes uses a hack to get the route working, and therefore doesn’t test well. I’m trying to figure out why this is the case. If you have some time and know how Rails 3 routing works, take a look at how the tests are failing and let me know.
There are probably a lot of them. This is my first foray into both the rails routing DSL and creating gems. If you want to yell at me, feel free to email jamuraa AT gmail DOT com.