Permalink
Browse files

Cherry pick of row locking logic from vamosa's fork plus extention t…

…o set_default_left_and_right case (ANS issue #70)
  • Loading branch information...
1 parent cf8eed9 commit 08684057143a3b9ab78360f8bbe4ced38faf345f @MarkusQ MarkusQ committed Jul 18, 2011
Showing with 9 additions and 1 deletion.
  1. +9 −1 lib/awesome_nested_set/awesome_nested_set.rb
@@ -406,7 +406,8 @@ def move_to_new_parent
# on creation, set automatically lft and rgt to the end of the tree
def set_default_left_and_right
- maxright = nested_set_scope.maximum(right_column_name) || 0
+ highest_right_row = nested_set_scope.find(:first, :order => "#{quoted_right_column_name} desc", :limit => 1,:lock => true )
+ maxright = highest_right_row ? highest_right_row[right_column_name] : 0
# adds the new node to the right of all existing nodes
self[left_column_name] = maxright + 1
self[right_column_name] = maxright + 2
@@ -490,6 +491,13 @@ def move_to(target, position)
# so sorting puts both the intervals and their boundaries in order
a, b, c, d = [self[left_column_name], self[right_column_name], bound, other_bound].sort
+ # select the rows in the model between a and d, and apply a lock
+ self.class.base_class.find(:all,
+ :select => "id",
+ :conditions => ["#{quoted_left_column_name} >= :a and #{quoted_right_column_name} <= :d", {:a => a, :d => d}],
+ :lock => true
+ )
+
new_parent = case position
when :child; target.id
when :root; nil

0 comments on commit 0868405

Please sign in to comment.