activesorting 0.8.0 → 0.8.1

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: 463b48951a19bd71e5e868e6d5ef31e95d55c29d
4
- data.tar.gz: d34ce47ee5756fbead1eabd47080e599832b2ea6
3
+ metadata.gz: 25b153abfd8dcdce3eb5441f7c4cb69b7c1de7c9
4
+ data.tar.gz: 9820bbde81b0b13ec2eadcace27aea620f3899fb
5
5
  SHA512:
6
- metadata.gz: 19e97e2679c79cc42f76d37fb64822a642197e8ff3a176d9b743fbf085879cfa3d15221f60ec4188b4e243a2f53310298abd43082ef8548aed6e753a47b85821
7
- data.tar.gz: eb854d49e2a8e6394cf03fbcee88ac45fcfd8145f2a350d86aa8507510a4c42b999c7abe2efb32c3c22ba3119a78c11c30a5cd7575c29b79e8dbf0474f6b8481
6
+ metadata.gz: 61c7e122234c614add381a063b9bfd6642b8e6b8fe0536e8ac3964aa60b7829893b0d797d32550a197f93e661eb51d9f508a31821906942652021753769a8595
7
+ data.tar.gz: c8ea678bee8c09ce83486826ed8a73edb4f5f3ed1fed33f958477e411df0c942369de5cd618492cc5a6397a1b1047823ca058bfa688e7fcba62250b3819304dc
data/README.md CHANGED
@@ -1,9 +1,15 @@
1
1
  # ActiveSorting
2
- [![Code Climate](https://codeclimate.com/github/owahab/active_sorting/badges/gpa.svg)](https://codeclimate.com/github/owahab/active_sorting)
2
+
3
+ ## Status
4
+
5
+ [![Gem Version](https://img.shields.io/gem/v/activesorting.svg)](http://rubygems.org/gems/activesorting)
3
6
  [![Build Status](https://travis-ci.org/owahab/active_sorting.svg?branch=master)](https://travis-ci.org/owahab/active_sorting)
4
7
  [![Coverage Status](https://coveralls.io/repos/github/owahab/active_sorting/badge.svg?branch=master)](https://coveralls.io/github/owahab/active_sorting?branch=master)
8
+ [![Code Climate](https://codeclimate.com/github/owahab/active_sorting/badges/gpa.svg)](https://codeclimate.com/github/owahab/active_sorting)
5
9
  [![Inline docs](http://inch-ci.org/github/owahab/active_sorting.svg?branch=master)](http://inch-ci.org/github/owahab/active_sorting)
6
10
  [![security](https://hakiri.io/github/owahab/active_sorting/master.svg)](https://hakiri.io/github/owahab/active_sorting/master)
11
+ [![Downloads](https://img.shields.io/gem/dtv/activesorting.svg)](http://rubygems.org/gems/activesorting)
12
+ [![GitHub issues](https://img.shields.io/github/issues/owahab/active_sorting.svg?maxAge=2592000)](https://github.com/owahab/active_sorting/issues)
7
13
 
8
14
  Allows sorting Rails models using a custom field.
9
15
 
@@ -61,3 +67,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
61
67
  Please see CONTRIBUTING.md for details.
62
68
 
63
69
  ## Credits
70
+
71
+ [![Eventtus](http://eventtus.com/css/images/logo.png)](http://eventtus.com)
72
+
73
+ Project is sponsored by [Eventtus](http://eventtus.com).
@@ -34,10 +34,10 @@ module ActiveSorting
34
34
  raise ArgumentError, "Sortable list should not be empty" unless new_list.count
35
35
  conditions = {}
36
36
  conditions[id_column] = new_list
37
- old_list = unscoped.where(conditions).pluck(:id)
37
+ old_list = unscoped.where(conditions).pluck(id_column)
38
38
  raise ArgumentError, "Sortable list should be persisted to database" unless old_list.count
39
39
  changes = active_sorting_changes_required(old_list, new_list)
40
- active_sorting_make_changes(new_list, changes)
40
+ active_sorting_make_changes(new_list, changes, id_column)
41
41
  end
42
42
 
43
43
  # Default sorting options
@@ -107,26 +107,26 @@ module ActiveSorting
107
107
  end
108
108
 
109
109
  # Commit changes to database
110
- def active_sorting_make_changes(new_list, changes)
110
+ def active_sorting_make_changes(new_list, changes, id_column)
111
111
  new_list.each_with_index do |id, index|
112
112
  next unless changes.include?(id)
113
113
  if index == new_list.count.pred
114
114
  # We're moving an item to last position,
115
115
  # increase the count of last item's position
116
116
  # by the step
117
- n1 = find(new_list[index.pred]).active_sorting_value
117
+ n1 = active_sorting_find_by(id_column, new_list[index.pred]).active_sorting_value
118
118
  n2 = n1 + active_sorting_step
119
119
  elsif index == 0
120
120
  # We're moving an item to first position
121
121
  # Calculate the gap between following 2 items
122
- n1 = find(new_list[index.next]).active_sorting_value
123
- n2 = find(new_list[index.next.next]).active_sorting_value
122
+ n1 = active_sorting_find_by(id_column, new_list[index.next]).active_sorting_value
123
+ n2 = active_sorting_find_by(id_column, new_list[index.next]).active_sorting_value
124
124
  else
125
125
  # We're moving a non-terminal item
126
- n1 = find(new_list[index.pred]).active_sorting_value
127
- n2 = find(new_list[index.next]).active_sorting_value
126
+ n1 = active_sorting_find_by(id_column, new_list[index.pred]).active_sorting_value
127
+ n2 = active_sorting_find_by(id_column, new_list[index.next]).active_sorting_value
128
128
  end
129
- find(id).active_sorting_center_item(n1, n2)
129
+ active_sorting_find_by(id_column, id).active_sorting_center_item(n1, n2)
130
130
  end
131
131
  end
132
132
 
@@ -145,6 +145,12 @@ module ActiveSorting
145
145
  def active_sorting_scope
146
146
  active_sorting_options[:scope]
147
147
  end
148
+
149
+ def active_sorting_find_by(id_column, value)
150
+ conditions = {}
151
+ conditions[id_column] = value
152
+ find_by(conditions)
153
+ end
148
154
  end
149
155
 
150
156
  def active_sorting_value
@@ -180,8 +186,8 @@ module ActiveSorting
180
186
  .unscoped
181
187
  .where(conditions)
182
188
  .maximum(self.class.active_sorting_field)
183
- # First value will always be 0
184
- return 0 if max.nil?
189
+ # First value will always equal step
190
+ return self.class.active_sorting_step if max.nil?
185
191
  # Increment by the step value configured
186
192
  max + self.class.active_sorting_step
187
193
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveSorting
2
- VERSION = '0.8.0'.freeze
2
+ VERSION = '0.8.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesorting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Omar Abdel-Wahab
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-14 00:00:00.000000000 Z
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -195,4 +195,3 @@ signing_key:
195
195
  specification_version: 4
196
196
  summary: Adds ability to sort models using a custom field
197
197
  test_files: []
198
- has_rdoc: