Awesome Nested Set Cheat Sheet

太难找了,帖到这里:
http://wiki.github.com/collectiveidea/awesome_nested_set/awesome-nested-set-cheat-sheet

 

 

Installation

Install as a plugin:

  script/plugin install git://github.com/collectiveidea/awesome_nested_set.git

Usage

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

  class CreateCategories < ActiveRecord::Migration
    def self.up
      create_table :categories do |t|
        t.string :name
        t.integer :parent_id
        t.integer :lft
        t.integer :rgt
      end
    end

    def self.down
      drop_table :categories
    end
  end

Enable the nested set functionality by declaring acts_as_nested_set on your model

  class Category < ActiveRecord::Base
    acts_as_nested_set
  end

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

View Helper

The view helper is called #nested_set_options.

Example usage:

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

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

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

 

Basic Usage

  • Create a root node: science = Category.create!(:name => ‘Science’)
  • Put a new thing inside this root node: physics = Category.create!(:name => ‘Physics’) physics.move_to_child_of(science)
  • Put another inside this “physics” node: gravity = Category.create!(:name => ‘Gravity’) gravity.move_to_child_of(physics)
  • Reload your root node: @science.reload!

Now you should have something that resembles this: + science |__ physics |____ gravity

Accessing the data:

Class methods:
Category.root the first root node
Category.roots all root nodes
Instance methods
my_cat.root root for this node.
my_cat.level the level of this object in the tree. root = 0
my_cat.parent the node’s immediate parent
my_cat.children array of immediate children (just those in the next level).
my_cat.ancestors array of all parents, parents’ parents, etc, excluding self.
my_cat.self_and_ancestors array of all parents, parents’ parents, etc, including self.
my_cat.siblings array of brothers and sisters (all at that level), excluding self.
my_cat.self_and_siblings array of brothers and sisters (all at that level), including self.
my_cat.descendants array of all children, childrens’ children, etc., excluding self.
my_cat.self_and_descendants array of all children, childrens’ children, etc., including self.
my_cat.leaves array of all descendants that have no children.
Instance methods: Tests (these don’t need to hit the DB to respond)
my_cat.root? true if this is a root node
my_cat.child? true if this is a child node. It has a parent.
my_cat.is_ancestor_of?(obj) true if nested by any obj
my_cat.is_or_is_ancestor_of?(obj) true if nested by any obj or self is obj
my_cat.is_descendent_of?(obj) true if self is nested under obj
my_cat.is_or_is_descendent_of?(obj) true if self is nested under obj or self is obj
my_cat.leaf?

true if this is a leaf node. It has no children

你可能感兴趣的:(F#,git,UP,ActiveRecord)