@ -14,7 +14,7 @@
require 'active_record'
ActiveSupport :: Deprecation . silenced = true
module ActiveRecord
class Base
include Redmine :: I18n
@ -286,3 +286,48 @@ module CollectiveIdea
end
end
end
module CollectiveIdea #:nodoc:
module Acts #:nodoc:
module NestedSet #:nodoc:
module Model
module ClassMethods
# 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?
scope = lambda { | node | }
if acts_as_nested_set_options [ :scope ]
scope = lambda { | node |
scope_column_names . inject ( " " ) { | str , column_name |
str << " AND #{ connection . quote_column_name ( column_name ) } = #{ connection . quote ( node . send ( column_name . to_sym ) ) } "
}
}
end
indices = { }
set_left_and_rights = lambda do | node |
# set left
node [ left_column_name ] = indices [ scope . call ( node ) ] += 1
# find
where ( [ " #{ quoted_parent_column_name } = ? #{ scope . call ( node ) } " , node ] ) . order ( " #{ quoted_left_column_name } , #{ quoted_right_column_name } , #{ acts_as_nested_set_options [ :order ] || 'id' } " ) . each { | n | set_left_and_rights . call ( n ) }
# set right
node [ right_column_name ] = indices [ scope . call ( node ) ] += 1
node . save! ( :validate = > validate_nodes )
end
# Find root node(s)
root_nodes = where ( " #{ quoted_parent_column_name } IS NULL " ) . order ( " #{ quoted_left_column_name } , #{ quoted_right_column_name } , #{ acts_as_nested_set_options [ :order ] || 'id' } " ) . each do | root_node |
# setup index for this scope
indices [ scope . call ( root_node ) ] || = 0
set_left_and_rights . call ( root_node )
end
end
end
end
end
end
end