Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JustinTArthur/9357534 to your computer and use it in GitHub Desktop.
Save JustinTArthur/9357534 to your computer and use it in GitHub Desktop.
indices[scope.call(root_node)] ||= 0
set_left_and_rights.call(root_node)
end
+
+ # Set depths based on left/right nesting:
+ if column_names.map(&:to_s).include?(depth_column_name.to_s)
+ get_depths_sql = "SELECT #{quoted_table_name}.#{primary_key}, COUNT(parent.#{primary_key}) -1 AS #{quoted_depth_column_name}
+ FROM #{quoted_table_name}, #{quoted_table_name} AS parent
+ WHERE #{quoted_table_name}.#{quoted_left_column_name} BETWEEN parent.#{quoted_left_column_name} AND parent.#{quoted_right_column_name}"
+
+ if acts_as_nested_set_options[:scope]
+ scope_column_names.each do |scope_column|
+ get_depths_sql << " AND parent.#{connection.quote_column_name(scope_column)} = #{quoted_table_name}.#{connection.quote_column_name(scope_column)}"
+ end
+ end
+
+ get_depths_sql << "
+ GROUP BY #{quoted_table_name}.#{primary_key}
+ ORDER BY #{quoted_table_name}.#{quoted_left_column_name}"
+
+ connection.select_rows(get_depths_sql).each do |node_id, node_depth|
+ connection.update("UPDATE #{quoted_table_name} SET #{quoted_depth_column_name} = #{node_depth} WHERE #{primary_key} = #{node_id}")
+ end
+ end
+
end
# Iterates over tree elements and determines the current level in the tree.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment