Skip to content

Commit 8550170

Browse files
authored
Merge pull request #11 from eventtus/sort_list_slow_workaround
create slow sort method to temporarily sort data until sort_list bug …
2 parents 3fc672e + 356867d commit 8550170

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/active_sorting/model.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ def sort_list(new_list, id_column = :id)
3939
active_sorting_make_changes(old_list, new_list, changes, id_column)
4040
end
4141

42+
def sort_list_slow(new_list, id_column = :id)
43+
raise ArgumentError, "Sortable list should not be empty" if new_list.empty?
44+
conditions = { id_column => new_list }
45+
old_list = unscoped.active_sorting_default_scope.where(conditions)
46+
old_list_ids = old_list.pluck(id_column)
47+
48+
raise Exceptions::RecordsNotFound, "Sortable list should be persisted to database with #{name} Model" if old_list_ids.empty?
49+
raise Exceptions::InvalidListSize, "Sortable new and old lists should be of the same length" if old_list.count != old_list_ids.count
50+
51+
weight_lists = old_list.map{|speaker| speaker.weight}
52+
53+
new_list.each_with_index do |id, index|
54+
current_record = active_sorting_find_by(id_column, id)
55+
if current_record.active_sorting_value != weight_lists[index]
56+
current_record.active_sorting_value = weight_lists[index]
57+
current_record.save!
58+
end
59+
end
60+
end
61+
4262
# Default sorting options
4363
def active_sorting_default_options
4464
{

0 commit comments

Comments
 (0)