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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f53d45737033dd11d048178d1c6cbdbe8b4d0a4
4
- data.tar.gz: 8cefb6d832edbfd09409aaa6a0093fbe9d3ccfa5
3
+ metadata.gz: 463b48951a19bd71e5e868e6d5ef31e95d55c29d
4
+ data.tar.gz: d34ce47ee5756fbead1eabd47080e599832b2ea6
5
5
  SHA512:
6
- metadata.gz: 0afc298783787e50c15f37931570a2f9b802e661c885a492bc7e4acb73620c0131fe258b25cda2dbf059bb7f8d602d5861f30df6f172c5723c7b57118855eefb
7
- data.tar.gz: 9651b49576f93aa5376b581618aac10f9309fc11c00ae7f5860503b25735ddf56310b998b91390a0316092f3a6d3bfd4d34d019d089d0fa3cda4d863bfce87b4
6
+ metadata.gz: 19e97e2679c79cc42f76d37fb64822a642197e8ff3a176d9b743fbf085879cfa3d15221f60ec4188b4e243a2f53310298abd43082ef8548aed6e753a47b85821
7
+ data.tar.gz: eb854d49e2a8e6394cf03fbcee88ac45fcfd8145f2a350d86aa8507510a4c42b999c7abe2efb32c3c22ba3119a78c11c30a5cd7575c29b79e8dbf0474f6b8481
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activesorting (0.7.0)
4
+ activesorting (0.7.1)
5
5
  activerecord (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activesorting (0.7.0)
4
+ activesorting (0.7.1)
5
5
  activerecord (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activesorting (0.7.0)
4
+ activesorting (0.7.1)
5
5
  activerecord (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activesorting (0.7.0)
4
+ activesorting (0.7.1)
5
5
  activerecord (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module ActiveSorting
2
- VERSION = '0.7.1'.freeze
2
+ VERSION = '0.8.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesorting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Omar Abdel-Wahab