Module: ClosureTree::NumericOrderSupport::PostgreSQLAdapter

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/closure_tree/numeric_order_support.rb', line 33

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 <<-SQL.strip_heredoc
    UPDATE #{quoted_table_name}
    SET #{quoted_order_column(false)} = t.seq + #{minimum_sort_order_value.to_i - 1}
    FROM (
      SELECT #{quoted_id_column_name} AS id, row_number() OVER(ORDER BY #{order_by}) AS seq
      FROM #{quoted_table_name}
      WHERE #{where_eq(parent_column_name, parent_id)} #{min_where}
    ) AS t
    WHERE #{quoted_table_name}.#{quoted_id_column_name} = t.id
  SQL
end

#rows_updated(result) ⇒ Object



51
52
53
# File 'lib/closure_tree/numeric_order_support.rb', line 51

def rows_updated(result)
  result.cmd_status.sub(/\AUPDATE /, '').to_i
end