Chris@0: = AwesomeNestedSet Chris@0: Chris@0: Awesome Nested Set is an implementation of the nested set pattern for ActiveRecord models. It is replacement for acts_as_nested_set and BetterNestedSet, but awesomer. Chris@0: Chris@0: == What makes this so awesome? Chris@0: Chris@0: This is a new implementation of nested set based off of BetterNestedSet that fixes some bugs, removes tons of duplication, adds a few useful methods, and adds STI support. Chris@0: Chris@0: == Installation Chris@0: Chris@0: If you are on Rails 2.1 or later: Chris@0: Chris@0: script/plugin install git://github.com/collectiveidea/awesome_nested_set.git Chris@0: Chris@0: == Usage Chris@0: Chris@0: To make use of awesome_nested_set, your model needs to have 3 fields: lft, rgt, and parent_id: Chris@0: Chris@0: class CreateCategories < ActiveRecord::Migration Chris@0: def self.up Chris@0: create_table :categories do |t| Chris@0: t.string :name Chris@0: t.integer :parent_id Chris@0: t.integer :lft Chris@0: t.integer :rgt Chris@0: end Chris@0: end Chris@0: Chris@0: def self.down Chris@0: drop_table :categories Chris@0: end Chris@0: end Chris@0: Chris@0: Enable the nested set functionality by declaring acts_as_nested_set on your model Chris@0: Chris@0: class Category < ActiveRecord::Base Chris@0: acts_as_nested_set Chris@0: end Chris@0: Chris@0: Run `rake rdoc` to generate the API docs and see CollectiveIdea::Acts::NestedSet::SingletonMethods for more info. Chris@0: Chris@0: == View Helper Chris@0: Chris@0: The view helper is called #nested_set_options. Chris@0: Chris@0: Example usage: Chris@0: Chris@0: <%= f.select :parent_id, nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" } %> Chris@0: Chris@0: <%= select_tag 'parent_id', options_for_select(nested_set_options(Category) {|i| "#{'-' * i.level} #{i.name}" } ) %> Chris@0: Chris@0: See CollectiveIdea::Acts::NestedSet::Helper for more information about the helpers. Chris@0: Chris@0: == References Chris@0: Chris@0: You can learn more about nested sets at: Chris@0: Chris@0: http://www.dbmsmag.com/9603d06.html Chris@0: http://threebit.net/tutorials/nestedset/tutorial1.html Chris@0: http://api.rubyonrails.com/classes/ActiveRecord/Acts/NestedSet/ClassMethods.html Chris@0: http://opensource.symetrie.com/trac/better_nested_set/ Chris@0: Chris@0: Chris@0: Copyright (c) 2008 Collective Idea, released under the MIT license