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