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

  1. Categories → /categories
    1. Books → /categories/books
      1. Hardback → /categories/books/hardback
      2. Paperback → /categories/books/paperback
    2. Websites → /categories/websites

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