Krishna Dole
2006-Dec-29 20:12 UTC
[Betternestedset-talk] Exciting(?) changes to BetterNestedSet
Hi Everyone, For about a year I have wanted to write a nested set implementation that was as easy to use as acts_as_tree. Using BetterNestedSet I''ve taken my first shot at that. The idea is fairly simple: the model belongs_to :parent and has_many :children, and an after_save callback takes care of updating the left/right values. You can still use move_to_child_of, but you don''t need to: s = Set.new s.children.create # adds a child and saves it s.children << Set.new # add another child s.save # the child is saved to the database s2 = Set.new s.parent= s2 s.save s2.parent.create The tree would now look like this, and all the left/right values would be correct: root s2 s child1 child2 All of the other has_many and belongs_to methods are available as well, such as #children.delete(child), #children = [c1, ...], etc. This version of BetterNestedSet currently exists as a branch, so if you want to play with it: script/plugin install svn://rubyforge.org/var/svn/betternestedset/branches/ez-set I''m quite curious to hear what everyone thinks of this. It is pretty well tested, but could certainly use some real-world exposure. FYI, it does not have any of the deprecated methods. I haven''t updated the documentation yet. The trunk line of BetterNestedSet has been active as well. Aside from bug fixes, here''s a summary of the externally visible changes I''ve made since mid-November: - Renamed #children_count to #all_children_count (the old name was both misleading, inconsistent with other methods, and inconsistent with acts_as_tree) - Changed #move_to_child_of so it inserts children on the right. - Added class method #sql_for, wich generates an SQL fragment for use in queries - Documentation overhaul. - Made all left/right altering methods concurrency-safe by using transactions, calling object.reload, and by overriding ActiveRecord''s #update. - Fixed STI problem (http://opensource.symetrie.com/trac/better_nested_set/ticket/10) - Added scope enforcement for the move_to* methods (to prevent moving nodes between different trees). - Added methods to return or count leaf nodes (terminal children). - Added methods to re-index trees (for converting standard trees to nested sets, or repairing corrupted lft/rgt indexes). - Lots and lots of tests. Cheers, Krishna
Nick Pavlica
2006-Dec-30 15:53 UTC
[Betternestedset-talk] Exciting(?) changes to BetterNestedSet
Krishna, This looks like a excellent continuation of the great work that you have been doing with better_nested_sets. I was curious if this new branch would become a fork(ez-set), or if it would become the next version of better_nested_sets ? --Nick
Krishna Dole
2006-Dec-30 16:46 UTC
[Betternestedset-talk] Exciting(?) changes to BetterNestedSet
I would favor merging the changes into trunk at some point, but only if JCM and others agree. Right now it is just a new approach for people to test and think about. k On 12/30/06, Nick Pavlica <linicks at gmail.com> wrote:> Krishna, > This looks like a excellent continuation of the great work that you > have been doing with better_nested_sets. I was curious if this new > branch would become a fork(ez-set), or if it would become the next > version of better_nested_sets ? > > --Nick > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk >
Jean-Christophe Michel
2006-Dec-31 10:55 UTC
[Betternestedset-talk] Exciting(?) changes to BetterNestedSet
Hi Krishna, Le 29 d?c. 06, ? 21:12, Krishna Dole a ?crit :> For about a year I have wanted to write a nested set implementation > that was as easy to use as acts_as_tree. Using BetterNestedSet I''ve > taken my first shot at that. The idea is fairly simple: the model > belongs_to :parent and has_many :children, and an after_save callback > takes care of updating the left/right values. You can still use > move_to_child_of, but you don''t need to: > > s = Set.new > s.children.create # adds a child and saves it > s.children << Set.new # add another child > s.save # the child is saved to the database > > s2 = Set.new > s.parent= s2 > s.save > > s2.parent.create > > The tree would now look like this, and all the left/right values would > be correctGreat work. I''d like to see benchmarks though, since using associations could be slower, don''t you think ? Jean-Christophe Michel -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set
Krishna Dole
2007-Jan-02 17:34 UTC
[Betternestedset-talk] Exciting(?) changes to BetterNestedSet
Hi Jean-Christophe,> I''d like to see benchmarks though, since using associations could be > slower, don''t you think ?I''m pretty sure it is slower, but only (I think) on update. Benchmarking is on my to-do list, once I get the callback problem and some other things sorted out. Krishna