To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / plugins / awesome_nested_set / README.rdoc @ 1298:4f746d8966dd

History | View | Annotate | Download (3.01 KB)

1
= AwesomeNestedSet
2

    
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.
4

    
5
Version 2 supports Rails 3. Gem versions prior to 2.0 support Rails 2.
6

    
7
== What makes this so awesome?
8

    
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.
10

    
11
== Installation
12

    
13
  Add to your Gemfile:
14

    
15
  gem 'awesome_nested_set'
16

    
17
== Usage
18

    
19
To make use of awesome_nested_set, your model needs to have 3 fields: lft, rgt, and parent_id:
20

    
21
  class CreateCategories < ActiveRecord::Migration
22
    def self.up
23
      create_table :categories do |t|
24
        t.string :name
25
        t.integer :parent_id
26
        t.integer :lft
27
        t.integer :rgt
28
      end
29
    end
30

    
31
    def self.down
32
      drop_table :categories
33
    end
34
  end
35

    
36
Enable the nested set functionality by declaring acts_as_nested_set on your model
37

    
38
  class Category < ActiveRecord::Base
39
    acts_as_nested_set
40
  end
41

    
42
Run `rake rdoc` to generate the API docs and see CollectiveIdea::Acts::NestedSet for more info.
43

    
44
== Protecting attributes from mass assignment
45

    
46
It's generally best to "white list" the attributes that can be used in mass assignment:
47

    
48
  class Category < ActiveRecord::Base
49
    acts_as_nested_set
50
    attr_accessible :name, :parent_id
51
  end
52

    
53
If for some reason that is not possible, you will probably want to protect the lft and rgt attributes:
54

    
55
  class Category < ActiveRecord::Base
56
    acts_as_nested_set
57
    attr_protected :lft, :rgt
58
  end
59

    
60
== Conversion from other trees
61

    
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
63

    
64
  Category.rebuild!
65

    
66
Your tree will be converted to a valid nested set. Awesome!
67

    
68
== View Helper
69

    
70
The view helper is called #nested_set_options.
71

    
72
Example usage:
73

    
74
  <%= f.select :parent_id, nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" } %>
75

    
76
  <%= select_tag 'parent_id', options_for_select(nested_set_options(Category) {|i| "#{'-' * i.level} #{i.name}" } ) %>
77

    
78
See CollectiveIdea::Acts::NestedSet::Helper for more information about the helpers.
79

    
80
== References
81

    
82
You can learn more about nested sets at: http://threebit.net/tutorials/nestedset/tutorial1.html
83

    
84
== How to contribute
85

    
86
If you find what you might think is a bug:
87

    
88
1. Check the GitHub issue tracker to see if anyone else has had the same issue.
89
   http://github.com/collectiveidea/awesome_nested_set/issues/
90
2. If you don't see anything, create an issue with information on how to reproduce it.
91

    
92
If you want to contribute an enhancement or a fix:
93

    
94
1. Fork the project on github.
95
   http://github.com/collectiveidea/awesome_nested_set/
96
2. Make your changes with tests.
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
98
4. Send a pull request.
99

    
100
Copyright ©2008 Collective Idea, released under the MIT license