@@ -13,38 +13,48 @@ def self.adapter_for_connection(connection)
13
13
end
14
14
15
15
module MysqlAdapter
16
- def reorder_with_parent_id ( parent_id , minimum_sort_order_value = nil )
16
+ def reorder_with_parent_id ( parent_id : , minimum_sort_order_value : nil , belong_to_name : nil , belong_to_id : nil )
17
17
return if parent_id . nil? && dont_order_roots
18
18
min_where = if minimum_sort_order_value
19
19
"AND #{ quoted_order_column } >= #{ minimum_sort_order_value } "
20
20
else
21
21
""
22
22
end
23
+ belong_to_scope = if parent_id . nil? && belong_to_id
24
+ "AND #{ quoted_table_name } .#{ belong_to_name } = #{ belong_to_id } "
25
+ else
26
+ ""
27
+ end
23
28
connection . execute 'SET @i = 0'
24
29
connection . execute <<-SQL . squish
25
30
UPDATE #{ quoted_table_name }
26
31
SET #{ quoted_order_column } = (@i := @i + 1) + #{ minimum_sort_order_value . to_i - 1 }
27
- WHERE #{ where_eq ( parent_column_name , parent_id ) } #{ min_where }
32
+ WHERE #{ where_eq ( parent_column_name , parent_id ) } #{ min_where } #{ belong_to_scope }
28
33
ORDER BY #{ nulls_last_order_by }
29
34
SQL
30
35
end
31
36
end
32
37
33
38
module PostgreSQLAdapter
34
- def reorder_with_parent_id ( parent_id , minimum_sort_order_value = nil )
39
+ def reorder_with_parent_id ( parent_id : , minimum_sort_order_value : nil , belong_to_name : nil , belong_to_id : nil )
35
40
return if parent_id . nil? && dont_order_roots
36
41
min_where = if minimum_sort_order_value
37
42
"AND #{ quoted_order_column } >= #{ minimum_sort_order_value } "
38
43
else
39
44
""
40
45
end
46
+ belong_to_scope = if parent_id . nil? && belong_to_id
47
+ "AND #{ quoted_table_name } .#{ belong_to_name } = #{ belong_to_id } "
48
+ else
49
+ ""
50
+ end
41
51
connection . execute <<-SQL . squish
42
52
UPDATE #{ quoted_table_name }
43
53
SET #{ quoted_order_column ( false ) } = t.seq + #{ minimum_sort_order_value . to_i - 1 }
44
54
FROM (
45
55
SELECT #{ quoted_id_column_name } AS id, row_number() OVER(ORDER BY #{ order_by } ) AS seq
46
56
FROM #{ quoted_table_name }
47
- WHERE #{ where_eq ( parent_column_name , parent_id ) } #{ min_where }
57
+ WHERE #{ where_eq ( parent_column_name , parent_id ) } #{ min_where } #{ belong_to_scope }
48
58
) AS t
49
59
WHERE #{ quoted_table_name } .#{ quoted_id_column_name } = t.id and
50
60
#{ quoted_table_name } .#{ quoted_order_column ( false ) } is distinct from t.seq + #{ minimum_sort_order_value . to_i - 1 }
@@ -57,14 +67,15 @@ def rows_updated(result)
57
67
end
58
68
59
69
module GenericAdapter
60
- def reorder_with_parent_id ( parent_id , minimum_sort_order_value = nil )
70
+ def reorder_with_parent_id ( parent_id : , minimum_sort_order_value : nil , belong_to_name : nil , belong_to_id : nil )
61
71
return if parent_id . nil? && dont_order_roots
62
72
scope = model_class .
63
73
where ( parent_column_sym => parent_id ) .
64
74
order ( nulls_last_order_by )
65
75
if minimum_sort_order_value
66
76
scope = scope . where ( "#{ quoted_order_column } >= #{ minimum_sort_order_value } " )
67
77
end
78
+ scope = scope . where ( belong_to_name => belong_to_id ) if belong_to_id
68
79
scope . each_with_index do |ea , idx |
69
80
ea . update_order_value ( idx + minimum_sort_order_value . to_i )
70
81
end
0 commit comments