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, scope_conditions = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/closure_tree/numeric_order_support.rb', line 17

def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil, scope_conditions = {})
  return if parent_id.nil? && dont_order_roots

  min_where = if minimum_sort_order_value
                "AND #{quoted_order_column} >= #{minimum_sort_order_value}"
              else
                ''
              end

  scope_where = build_scope_where_clause(scope_conditions)

  connection.execute 'SET @i = 0'
  connection.execute <<-SQL.squish
    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}#{scope_where}
    ORDER BY #{nulls_last_order_by}
  SQL
end