Module: ClosureTree::NumericOrderSupport::MysqlAdapter

Defined in:
lib/closure_tree/numeric_order_support.rb

Instance Method Summary collapse

Instance Method Details

#reorder_with_parent_id(parent_id, minimum_sort_order_value = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/closure_tree/numeric_order_support.rb', line 16

def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil)
  min_where = if minimum_sort_order_value
    "AND #{quoted_order_column} >= #{minimum_sort_order_value}"
  else
    ""
  end
  connection.execute 'SET @i = 0'
  connection.execute <<-SQL.strip_heredoc
    UPDATE #{quoted_table_name}
      SET #{quoted_order_column} = (@i := @i + 1) + #{minimum_sort_order_value.to_i - 1}
    WHERE #{where_eq(parent_column_name, parent_id)} #{min_where}
    ORDER BY #{nulls_last_order_by}
  SQL
end