Skip to content

Commit

Permalink
Add support for saving without validation for rebuild
Browse files Browse the repository at this point in the history
Add support for saving without validation for rebuild
  • Loading branch information
mguymon authored and danielmorrison committed May 4, 2011
1 parent 6789220 commit 6570b03
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lib/awesome_nested_set/awesome_nested_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ def each_root_valid?(roots_to_validate)
end
end

# Rebuilds the left & rights if unset or invalid. Also very useful for converting from acts_as_tree.
def rebuild!
# Rebuilds the left & rights if unset or invalid.
# Also very useful for converting from acts_as_tree.
def rebuild!(validate_nodes = true)
# Don't rebuild a valid tree.
return true if valid?

Expand All @@ -169,7 +170,7 @@ def rebuild!
where(["#{quoted_parent_column_name} = ? #{scope.call(node)}", node]).order("#{quoted_left_column_name}, #{quoted_right_column_name}, id").each{|n| set_left_and_rights.call(n) }
# set right
node[right_column_name] = indices[scope.call(node)] += 1
node.save!
node.save!(:validate => validate_nodes)
end

# Find root node(s)
Expand Down
27 changes: 22 additions & 5 deletions spec/awesome_nested_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,23 @@
assert_equal Category.roots.last.to_text, output
end

it "should be able to rebuild without validating each record" do
root1 = Category.create(:name => 'Root1')
root2 = Category.create(:name => 'Root2')
root3 = Category.create(:name => 'Root3')

root2.move_to_child_of root1
root3.move_to_child_of root1

root2.name = nil
root2.save!(:validate => false)

output = Category.roots.last.to_text
Category.update_all('lft = null, rgt = null')
Category.rebuild!(false)

assert_equal Category.roots.last.to_text, output
end

it "valid_with_null_lefts" do
assert Category.valid?
Expand Down Expand Up @@ -540,10 +557,10 @@
end

it "moving_dirty_objects_doesnt_invalidate_tree" do
r1 = Category.create
r2 = Category.create
r3 = Category.create
r4 = Category.create
r1 = Category.create :name => "Test 1"
r2 = Category.create :name => "Test 2"
r3 = Category.create :name => "Test 3"
r4 = Category.create :name => "Test 4"
nodes = [r1, r2, r3, r4]

r2.move_to_child_of(r1)
Expand Down Expand Up @@ -774,4 +791,4 @@ def check_structure(entries, structure)
categories(:child_3).save.should be_true
end
end
end
end
4 changes: 3 additions & 1 deletion spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class RenamedColumns < ActiveRecord::Base

class Category < ActiveRecord::Base
acts_as_nested_set


validates_presence_of :name

# Setup a callback that we can switch to true or false per-test
set_callback :move, :before, :custom_before_move
cattr_accessor :test_allows_move
Expand Down

0 comments on commit 6570b03

Please sign in to comment.