Chris@1115: = AwesomeNestedSet Chris@1115: Chris@1115: 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 more awesome. Chris@1115: Chris@1115: Version 2 supports Rails 3. Gem versions prior to 2.0 support Rails 2. Chris@1115: Chris@1115: == What makes this so awesome? Chris@1115: Chris@1115: 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@1115: Chris@1115: == Installation Chris@1115: Chris@1115: Add to your Gemfile: Chris@1115: Chris@1115: gem 'awesome_nested_set' Chris@1115: Chris@1115: == Usage Chris@1115: Chris@1115: To make use of awesome_nested_set, your model needs to have 3 fields: lft, rgt, and parent_id: Chris@1115: Chris@1115: class CreateCategories < ActiveRecord::Migration Chris@1115: def self.up Chris@1115: create_table :categories do |t| Chris@1115: t.string :name Chris@1115: t.integer :parent_id Chris@1115: t.integer :lft Chris@1115: t.integer :rgt Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: def self.down Chris@1115: drop_table :categories Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: Enable the nested set functionality by declaring acts_as_nested_set on your model Chris@1115: Chris@1115: class Category < ActiveRecord::Base Chris@1115: acts_as_nested_set Chris@1115: end Chris@1115: Chris@1115: Run `rake rdoc` to generate the API docs and see CollectiveIdea::Acts::NestedSet for more info. Chris@1115: Chris@1115: == Protecting attributes from mass assignment Chris@1115: Chris@1115: It's generally best to "white list" the attributes that can be used in mass assignment: Chris@1115: Chris@1115: class Category < ActiveRecord::Base Chris@1115: acts_as_nested_set Chris@1115: attr_accessible :name, :parent_id Chris@1115: end Chris@1115: Chris@1115: If for some reason that is not possible, you will probably want to protect the lft and rgt attributes: Chris@1115: Chris@1115: class Category < ActiveRecord::Base Chris@1115: acts_as_nested_set Chris@1115: attr_protected :lft, :rgt Chris@1115: end Chris@1115: Chris@1115: == Conversion from other trees Chris@1115: Chris@1115: Coming from acts_as_tree or another system where you only have a parent_id? No problem. Simply add the lft & rgt fields as above, and then run Chris@1115: Chris@1115: Category.rebuild! Chris@1115: Chris@1115: Your tree will be converted to a valid nested set. Awesome! Chris@1115: Chris@1115: == View Helper Chris@1115: Chris@1115: The view helper is called #nested_set_options. Chris@1115: Chris@1115: Example usage: Chris@1115: Chris@1115: <%= f.select :parent_id, nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" } %> Chris@1115: Chris@1115: <%= select_tag 'parent_id', options_for_select(nested_set_options(Category) {|i| "#{'-' * i.level} #{i.name}" } ) %> Chris@1115: Chris@1115: See CollectiveIdea::Acts::NestedSet::Helper for more information about the helpers. Chris@1115: Chris@1115: == References Chris@1115: Chris@1115: You can learn more about nested sets at: http://threebit.net/tutorials/nestedset/tutorial1.html Chris@1115: Chris@1115: == How to contribute Chris@1115: Chris@1115: If you find what you might think is a bug: Chris@1115: Chris@1115: 1. Check the GitHub issue tracker to see if anyone else has had the same issue. Chris@1115: http://github.com/collectiveidea/awesome_nested_set/issues/ Chris@1115: 2. If you don't see anything, create an issue with information on how to reproduce it. Chris@1115: Chris@1115: If you want to contribute an enhancement or a fix: Chris@1115: Chris@1115: 1. Fork the project on github. Chris@1115: http://github.com/collectiveidea/awesome_nested_set/ Chris@1115: 2. Make your changes with tests. Chris@1115: 3. Commit the changes without making changes to the Rakefile, VERSION, or any other files that aren't related to your enhancement or fix Chris@1115: 4. Send a pull request. Chris@1115: Chris@1115: Copyright ©2008 Collective Idea, released under the MIT license