PolyPath

PolyPath is a helper for use in your controllers and views for specifying paths to nested polymorphic resources.

Example:

if you have the following three models:
 1 class User < ActiveRecord::Base
 2   has_many :photos, :as => :photoable
 3 end
 4 
 5 class Event < ActiveRecord::Base
 6   has_many :photos, :as => :photoable
 7 end
 8 
 9 class Photo < ActiveRecord::Base
10   belongs :to => :photoable, :polymorphic => true
11 end
And the following route:
1 map.resources :photos, :path_prefix => "/:photoable_type/:photoable_id"
You can use the helper like this:
1 photo_url(poly_path(photo, :photoable))
This will allow you to easily construct URLs that end like: /users/1/photos/5 and /events/2/photos/6

There are currenltly no tests for this plugin as it was a five minute proof of concept. If there is interest, there are plenty of improvments that can be made.

Tags

You need to Login to tag this item.