annotate .svn/pristine/12/1274b8f28c749e735c8c75f99681e8f1e536c19a.svn-base @ 1466:834828a14f2b bug_598

Close obsolete branch bug_598
author Chris Cannam
date Fri, 21 Jun 2013 14:51:55 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 = AwesomeNestedSet
Chris@1296 2
Chris@1296 3 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@1296 4
Chris@1296 5 Version 2 supports Rails 3. Gem versions prior to 2.0 support Rails 2.
Chris@1296 6
Chris@1296 7 == What makes this so awesome?
Chris@1296 8
Chris@1296 9 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@1296 10
Chris@1296 11 == Installation
Chris@1296 12
Chris@1296 13 Add to your Gemfile:
Chris@1296 14
Chris@1296 15 gem 'awesome_nested_set'
Chris@1296 16
Chris@1296 17 == Usage
Chris@1296 18
Chris@1296 19 To make use of awesome_nested_set, your model needs to have 3 fields: lft, rgt, and parent_id:
Chris@1296 20
Chris@1296 21 class CreateCategories < ActiveRecord::Migration
Chris@1296 22 def self.up
Chris@1296 23 create_table :categories do |t|
Chris@1296 24 t.string :name
Chris@1296 25 t.integer :parent_id
Chris@1296 26 t.integer :lft
Chris@1296 27 t.integer :rgt
Chris@1296 28 end
Chris@1296 29 end
Chris@1296 30
Chris@1296 31 def self.down
Chris@1296 32 drop_table :categories
Chris@1296 33 end
Chris@1296 34 end
Chris@1296 35
Chris@1296 36 Enable the nested set functionality by declaring acts_as_nested_set on your model
Chris@1296 37
Chris@1296 38 class Category < ActiveRecord::Base
Chris@1296 39 acts_as_nested_set
Chris@1296 40 end
Chris@1296 41
Chris@1296 42 Run `rake rdoc` to generate the API docs and see CollectiveIdea::Acts::NestedSet for more info.
Chris@1296 43
Chris@1296 44 == Protecting attributes from mass assignment
Chris@1296 45
Chris@1296 46 It's generally best to "white list" the attributes that can be used in mass assignment:
Chris@1296 47
Chris@1296 48 class Category < ActiveRecord::Base
Chris@1296 49 acts_as_nested_set
Chris@1296 50 attr_accessible :name, :parent_id
Chris@1296 51 end
Chris@1296 52
Chris@1296 53 If for some reason that is not possible, you will probably want to protect the lft and rgt attributes:
Chris@1296 54
Chris@1296 55 class Category < ActiveRecord::Base
Chris@1296 56 acts_as_nested_set
Chris@1296 57 attr_protected :lft, :rgt
Chris@1296 58 end
Chris@1296 59
Chris@1296 60 == Conversion from other trees
Chris@1296 61
Chris@1296 62 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@1296 63
Chris@1296 64 Category.rebuild!
Chris@1296 65
Chris@1296 66 Your tree will be converted to a valid nested set. Awesome!
Chris@1296 67
Chris@1296 68 == View Helper
Chris@1296 69
Chris@1296 70 The view helper is called #nested_set_options.
Chris@1296 71
Chris@1296 72 Example usage:
Chris@1296 73
Chris@1296 74 <%= f.select :parent_id, nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" } %>
Chris@1296 75
Chris@1296 76 <%= select_tag 'parent_id', options_for_select(nested_set_options(Category) {|i| "#{'-' * i.level} #{i.name}" } ) %>
Chris@1296 77
Chris@1296 78 See CollectiveIdea::Acts::NestedSet::Helper for more information about the helpers.
Chris@1296 79
Chris@1296 80 == References
Chris@1296 81
Chris@1296 82 You can learn more about nested sets at: http://threebit.net/tutorials/nestedset/tutorial1.html
Chris@1296 83
Chris@1296 84 == How to contribute
Chris@1296 85
Chris@1296 86 If you find what you might think is a bug:
Chris@1296 87
Chris@1296 88 1. Check the GitHub issue tracker to see if anyone else has had the same issue.
Chris@1296 89 http://github.com/collectiveidea/awesome_nested_set/issues/
Chris@1296 90 2. If you don't see anything, create an issue with information on how to reproduce it.
Chris@1296 91
Chris@1296 92 If you want to contribute an enhancement or a fix:
Chris@1296 93
Chris@1296 94 1. Fork the project on github.
Chris@1296 95 http://github.com/collectiveidea/awesome_nested_set/
Chris@1296 96 2. Make your changes with tests.
Chris@1296 97 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@1296 98 4. Send a pull request.
Chris@1296 99
Chris@1296 100 Copyright ©2008 Collective Idea, released under the MIT license