README.md in closure_tree-2.0.0.beta1 vs README.md in closure_tree-2.0.0
- old
+ new
@@ -8,17 +8,17 @@
[Models for hierarchical data presentation](http://www.slideshare.net/billkarwin/models-for-hierarchical-data)
for a description of different tree storage algorithms.
## Setup
-Note that closure_tree is being developed for Rails 3.1.0.rc1
+Note that closure_tree is being developed for Rails 3.1.x
1. Add this to your Gemfile: ```gem 'closure_tree'```
2. Run ```bundle install```
-3. Add ```acts_as_tree``` to your hierarchical model(s) (see the <a href="#options">available options</a>).
+3. Add ```acts_as_tree``` to your hierarchical model(s) (see the <em>Available options</em> section below for details).
4. Add a migration to add a ```parent_id``` column to the model you want to act_as_tree.
Note that if the column is null, the tag will be considered a root node.
@@ -65,27 +65,33 @@
### Creation
Create a root node:
```ruby
- grandparent = Tag.create!(:name => 'Grandparent')
+ grandparent = Tag.create(:name => 'Grandparent')
```
-There are two equivalent ways to add children. Either use the ```add_child``` method:
+Child nodes are created by appending to the children collection:
```ruby
- parent = Tag.create!(:name => 'Parent')
- grandparent.add_child parent
+ child = parent.children.create(:name => 'Child')
```
-Or append to the ```children``` collection:
+You can also append to the children collection:
```ruby
- child = Tag.create!(:name => 'Child')
+ child = Tag.create(:name => 'Child')
parent.children << child
```
-
+
+Or call the "add_child" method:
+
+ ```ruby
+ parent = Tag.create(:name => 'Parent')
+ grandparent.add_child parent
+ ```
+
Then:
```ruby
puts grandparent.self_and_descendants.collect{ |t| t.name }.join(" > ")
"grandparent > parent > child"
@@ -97,11 +103,11 @@
### <code>find_or_create_by_path</code>
We can do all the node creation and add_child calls from the prior section with one method call:
```ruby
- child = Tag.find_or_create_by_path "grandparent", "parent", "child"
+ child = Tag.find_or_create_by_path("grandparent", "parent", "child")
```
You can ```find``` as well as ```find_or_create``` by "ancestry paths". Ancestry paths may be built using any column in your model. The default column is ```name```, which can be changed with the :name_column option provided to ```acts_as_tree```.
Note that the other columns will be null if nodes are created, other than auto-generated columns like ID and created_at timestamp. Only the specified column will receive the path element value.
@@ -109,17 +115,17 @@
### Available options
<a id="options" />
When you include ```acts_as_tree``` in your model, you can provide a hash to override the following defaults:
-* ```:parent_column_name``` to override the column name of the parent foreign key in the model's table
+* ```:parent_column_name``` to override the column name of the parent foreign key in the model's table. This defaults to "parent_id".
* ```:hierarchy_table_name``` to override the hierarchy table name. This defaults to the singular name of the model + "_hierarchies".
-* ```:name_column``` used by #```find_or_create_by_path```, #```find_by_path```, and ```ancestry_path``` instance methods. This is primarily useful if the model only has one required field (like a "tag").
* ```:dependent``` determines what happens when a node is destroyed. Defaults to ```nil```.
- * ```nil``` will simply set the parent column to null. Each child node will be considered a "root" node
+ * ```:nullify``` will simply set the parent column to null. Each child node will be considered a "root" node. This is the default.
* ```:delete_all``` will delete all descendant nodes (which circumvents the destroy hooks)
* ```:destroy``` will destroy all descendant nodes (which runs the destroy hooks on each child node)
+* ```:name_column``` used by #```find_or_create_by_path```, #```find_by_path```, and ```ancestry_path``` instance methods. This is primarily useful if the model only has one required field (like a "tag").
## Accessing Data
### Class methods
@@ -141,19 +147,17 @@
* ``` tag.self_and_ancestors``` returns an array of self, parent, grandparent, great grandparent, etc.
* ``` tag.siblings``` returns an array of brothers and sisters (all at that level), excluding self.
* ``` tag.self_and_siblings``` returns an array of brothers and sisters (all at that level), including self.
* ``` tag.descendants``` returns an array of all children, childrens' children, etc., excluding self.
* ``` tag.self_and_descendants``` returns an array of all children, childrens' children, etc., including self.
-* ``` tag.reparent``` lets you move a node (and all it's children) to a new parent.
-* ``` tag.destroy``` will destroy a node as well as possibly all of its children. See the ```:dependent``` option passed to ```acts_as_tree```.
+* ``` tag.destroy``` will destroy a node and do <em>something</em> to its children, which is determined by the ```:dependent``` option passed to ```acts_as_tree```.
## Changelog
### 2.0.0.beta1
* Had to increment the major version, as rebuild! will need to be called by prior consumers to support the new ```leaves``` class and instance methods.
* Tag deletion is supported now along with ```:dependent => :destroy``` and ```:dependent => :delete_all```
-* Added new instance method ```reparent```
* Switched from default rails plugin directory structure to rspec
## Thanks to
* https://github.com/collectiveidea/awesome_nested_set