@@ -13,38 +13,48 @@ def self.adapter_for_connection(connection)
1313 end
1414
1515 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 )
1717 return if parent_id . nil? && dont_order_roots
1818 min_where = if minimum_sort_order_value
1919 "AND #{ quoted_order_column } >= #{ minimum_sort_order_value } "
2020 else
2121 ""
2222 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
2328 connection . execute 'SET @i = 0'
2429 connection . execute <<-SQL . squish
2530 UPDATE #{ quoted_table_name }
2631 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 }
2833 ORDER BY #{ nulls_last_order_by }
2934 SQL
3035 end
3136 end
3237
3338 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 )
3540 return if parent_id . nil? && dont_order_roots
3641 min_where = if minimum_sort_order_value
3742 "AND #{ quoted_order_column } >= #{ minimum_sort_order_value } "
3843 else
3944 ""
4045 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
4151 connection . execute <<-SQL . squish
4252 UPDATE #{ quoted_table_name }
4353 SET #{ quoted_order_column ( false ) } = t.seq + #{ minimum_sort_order_value . to_i - 1 }
4454 FROM (
4555 SELECT #{ quoted_id_column_name } AS id, row_number() OVER(ORDER BY #{ order_by } ) AS seq
4656 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 }
4858 ) AS t
4959 WHERE #{ quoted_table_name } .#{ quoted_id_column_name } = t.id and
5060 #{ 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)
5767 end
5868
5969 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 )
6171 return if parent_id . nil? && dont_order_roots
6272 scope = model_class .
6373 where ( parent_column_sym => parent_id ) .
6474 order ( nulls_last_order_by )
6575 if minimum_sort_order_value
6676 scope = scope . where ( "#{ quoted_order_column } >= #{ minimum_sort_order_value } " )
6777 end
78+ scope = scope . where ( belong_to_name => belong_to_id ) if belong_to_id
6879 scope . each_with_index do |ea , idx |
6980 ea . update_order_value ( idx + minimum_sort_order_value . to_i )
7081 end
0 commit comments