activesorting 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/gemfiles/40.gemfile.lock +1 -1
- data/gemfiles/41.gemfile.lock +1 -1
- data/gemfiles/42.gemfile.lock +1 -1
- data/gemfiles/50.gemfile.lock +1 -1
- data/lib/active_sorting/model.rb +22 -6
- data/lib/active_sorting/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 463b48951a19bd71e5e868e6d5ef31e95d55c29d
|
4
|
+
data.tar.gz: d34ce47ee5756fbead1eabd47080e599832b2ea6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19e97e2679c79cc42f76d37fb64822a642197e8ff3a176d9b743fbf085879cfa3d15221f60ec4188b4e243a2f53310298abd43082ef8548aed6e753a47b85821
|
7
|
+
data.tar.gz: eb854d49e2a8e6394cf03fbcee88ac45fcfd8145f2a350d86aa8507510a4c42b999c7abe2efb32c3c22ba3119a78c11c30a5cd7575c29b79e8dbf0474f6b8481
|
data/gemfiles/40.gemfile.lock
CHANGED
data/gemfiles/41.gemfile.lock
CHANGED
data/gemfiles/42.gemfile.lock
CHANGED
data/gemfiles/50.gemfile.lock
CHANGED
data/lib/active_sorting/model.rb
CHANGED
@@ -24,6 +24,22 @@ module ActiveSorting
|
|
24
24
|
before_validation :active_sorting_callback_before_validation
|
25
25
|
end
|
26
26
|
|
27
|
+
# Sorts and updates the database with the given list of items
|
28
|
+
# in the given order.
|
29
|
+
#
|
30
|
+
# +new_list+ List of ids of records in the desired order
|
31
|
+
# +id_column+ the field used for fetching records from the databse,
|
32
|
+
# defaults to :id
|
33
|
+
def sort_list(new_list, id_column = :id)
|
34
|
+
raise ArgumentError, "Sortable list should not be empty" unless new_list.count
|
35
|
+
conditions = {}
|
36
|
+
conditions[id_column] = new_list
|
37
|
+
old_list = unscoped.where(conditions).pluck(:id)
|
38
|
+
raise ArgumentError, "Sortable list should be persisted to database" unless old_list.count
|
39
|
+
changes = active_sorting_changes_required(old_list, new_list)
|
40
|
+
active_sorting_make_changes(new_list, changes)
|
41
|
+
end
|
42
|
+
|
27
43
|
# Default sorting options
|
28
44
|
def active_sorting_default_options
|
29
45
|
{
|
@@ -65,7 +81,7 @@ module ActiveSorting
|
|
65
81
|
end
|
66
82
|
proposal1 = active_sorting_calculate_changes(old_list.dup, new_list.dup)
|
67
83
|
if proposal1.count >= (new_list.count / 4)
|
68
|
-
proposal2 = active_sorting_calculate_changes(old_list.reverse, new_list.reverse)
|
84
|
+
proposal2 = active_sorting_calculate_changes(old_list.dup.reverse, new_list.dup.reverse)
|
69
85
|
changes = proposal1.count < proposal2.count ? proposal1 : proposal2
|
70
86
|
else
|
71
87
|
changes = proposal1
|
@@ -98,17 +114,17 @@ module ActiveSorting
|
|
98
114
|
# We're moving an item to last position,
|
99
115
|
# increase the count of last item's position
|
100
116
|
# by the step
|
101
|
-
n1 = find(index.pred).active_sorting_value
|
117
|
+
n1 = find(new_list[index.pred]).active_sorting_value
|
102
118
|
n2 = n1 + active_sorting_step
|
103
119
|
elsif index == 0
|
104
120
|
# We're moving an item to first position
|
105
121
|
# Calculate the gap between following 2 items
|
106
|
-
n1 = find(index.next).active_sorting_value
|
107
|
-
n2 = find(index.next.next).active_sorting_value
|
122
|
+
n1 = find(new_list[index.next]).active_sorting_value
|
123
|
+
n2 = find(new_list[index.next.next]).active_sorting_value
|
108
124
|
else
|
109
125
|
# We're moving a non-terminal item
|
110
|
-
n1 = find(index.pred).active_sorting_value
|
111
|
-
n2 = find(index.next).active_sorting_value
|
126
|
+
n1 = find(new_list[index.pred]).active_sorting_value
|
127
|
+
n2 = find(new_list[index.next]).active_sorting_value
|
112
128
|
end
|
113
129
|
find(id).active_sorting_center_item(n1, n2)
|
114
130
|
end
|