annotate lib/plugins/awesome_nested_set/README.rdoc @ 1295:622f24f53b42 redmine-2.3

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