PathFinder
A Rails plugin which extends ActiveRecord using the decorator pattern to allow self-referential models (eg. acts_as_tree) to maintain a textual path representing itself and its ancestors.
Install
gem install path_finder
Example
In association with a catch-all route you can find a record using the URL:
Hierarchy → Generated Path
- Categories → /categories
- Books → /categories/books
- Hardback → /categories/books/hardback
- Paperback → /categories/books/paperback
- Websites → /categories/websites
- Books → /categories/books
Example URL: http://mywebsite.co.uk/categories/books/hardback
category_controller.rb
def show @category = Category.find_by_path(request.path) end
category.rb
class Category < ActiveRecord::Base acts_as_tree path_finder :column => ‘path’, :uid => ‘to_param’, :deliminator => ‘/’
def to_param permalink end end
Notes
The model must respond_to ‘children’
Copyright © 2010 Kris Leech, released under the MIT license