Postings
| Browse in : |
All
> subjects
> rails
|
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xd and user-summary-[publicationtype].xd. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles The templates will get the extension .xt there.
Title: List methods for nested sets
Author: linoj
Date: February 21, 2008 3:55:00 PM or Thu, 21 February 2008 15:55:00
Summary: Unfortunately better_nested_set (and acts_as_nested_set) lacks the list methods provided by acts_as_list. Here's a module that provides them.
Body:
I had been using acts_as_tree together with acts_as_list to make ordered nested lists. Recently I replaced acts_as_tree with the better_nested_set plugin, a formidable replacement that is much more efficient in database queries (it pulls the whole tree from the database at once). The nested set is also ordered, so there's no need for acts_as_list.
Unfortunately better_nested_set lacks the list methods provided by acts_as_list. Here's a module that provides them. It treats each branch of a tree as its own list, thus these methods are scoped by the local root.
I haven't bothered to make this a plugin. Just create a file lib/nested_set_list.rb and paste this code:
module NestedSetList
def first?
parent.nil? or parent.lft==self.lft-1
end
def last?
parent.nil? or parent.rgt==self.rgt+1
end
def higher_item
list = self_and_siblings
i = list.index(self)
i==0 ? self : list[ i-1 ]
end
def lower_item
list = self_and_siblings
i = list.index(self)
i==list.size-1 ? self : list[ i+1 ]
end
def move_higher
move_to_left_of( higher_item ) if higher_item
end
def move_lower
move_to_right_of( lower_item ) if lower_item
end
def move_to_top
move_to_left_of( self_and_siblings.first )
end
def move_to_bottom
move_to_right_of( self_and_siblings.last )
end
end
In your model, be sure to include it
acts_as_nested_set
include NestedSetList
Notes:
More fields may be available via dynamicdata ..
Last modified on Feb 21, 2008 4:22:23 PM by linoj
Note: Comments are owned by the poster. We are not responsible for their content.
Post a new comment
Keywords :
List methods for nested sets
Posted by: Anonymous on March 10, 2008 10:57 AM#