acts_as_positioned 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/acts_as_positioned.rb +14 -10
- data/test/test.sqlite3 +0 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 819a27e3d8d9285287dbe10dfb2185d3e0a1efee
|
4
|
+
data.tar.gz: 5516a75100c59844cf19a52ac92b3164a029d981
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1e97ab6a6a4983ca04fb6c8afe53f3411f3a270c5d299fc0bb13ed5a6be3e8d0118dcbfa0d8cacf871cef6451c556f3038dc73b7823f3162cc943741b2e3331
|
7
|
+
data.tar.gz: 0247be198970593479b415940b1c3f1af3beb05b6699805b5437d5540f8ad3f6cf6609b3276cc5318cd4a16dd0d13dd1a8e16b65e90917ef4f5cf7cfcbec8f63
|
data/lib/acts_as_positioned.rb
CHANGED
@@ -30,17 +30,22 @@ module ActsAsPositioned
|
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
+
def aap_execute_query(column, scope, downwards)
|
34
|
+
quoted_column = scope.connection.quote_column_name(column)
|
35
|
+
scope.update_all("#{quoted_column} = #{quoted_column} #{downwards ? '+' : '-'} 1")
|
36
|
+
end
|
37
|
+
|
33
38
|
def aap_insert_position(column, scope_columns)
|
34
|
-
scope = aap_scope(column, scope_columns)
|
35
|
-
scope.where(scope.arel_table[column].gteq(send(column)))
|
39
|
+
scope = aap_scope(column, scope_columns, false)
|
40
|
+
aap_execute_query(column, scope.where(scope.arel_table[column].gteq(send(column))), true)
|
36
41
|
end
|
37
42
|
|
38
43
|
def aap_remove_position(column, scope_columns)
|
39
44
|
scope = aap_scope(column, scope_columns, true)
|
40
|
-
scope.where(scope.arel_table[column].gt(send("#{column}_was")))
|
45
|
+
aap_execute_query(column, scope.where(scope.arel_table[column].gt(send("#{column}_was"))), false)
|
41
46
|
end
|
42
47
|
|
43
|
-
def aap_scope(column, scope_columns, use_old_values
|
48
|
+
def aap_scope(column, scope_columns, use_old_values)
|
44
49
|
# When using the old values, make sure to overwrite the attribute values with the old values.
|
45
50
|
attrs = use_old_values ? attributes.merge(changed_attributes) : attributes
|
46
51
|
self.class.base_class.where(attrs.slice(*scope_columns)).where.not(column => nil)
|
@@ -48,12 +53,11 @@ module ActsAsPositioned
|
|
48
53
|
|
49
54
|
def aap_switch_positions(column, scope_columns)
|
50
55
|
old_value, new_value = changes[column]
|
51
|
-
from
|
56
|
+
from = [old_value + 1, new_value].min
|
57
|
+
to = [old_value - 1, new_value].max
|
52
58
|
|
53
|
-
|
54
|
-
|
55
|
-
statement = ", #{locking_column} = #{locking_column} + 1" if scope.locking_enabled?
|
56
|
-
scope.where(column => from.eql?(to) ? from : from..to).update_all(statement)
|
59
|
+
aap_execute_query(column, aap_scope(column, scope_columns, false).where(column => from.eql?(to) ? from : from..to),
|
60
|
+
old_value > new_value)
|
57
61
|
end
|
58
62
|
|
59
63
|
def aap_update_position(column, scope_columns)
|
@@ -79,7 +83,7 @@ module ActsAsPositioned
|
|
79
83
|
def aap_validate_position(column, scope_columns)
|
80
84
|
return if errors[column].present? || (changes.keys & ([column] + scope_columns)).empty?
|
81
85
|
|
82
|
-
scope = aap_scope(column, scope_columns)
|
86
|
+
scope = aap_scope(column, scope_columns, false)
|
83
87
|
options = { attributes: column, allow_nil: true, greater_than_or_equal_to: 0, only_integer: true,
|
84
88
|
less_than_or_equal_to: scope.where.not(scope.primary_key => id).count }
|
85
89
|
ActiveModel::Validations::NumericalityValidator.new(options).validate(self)
|
data/test/test.sqlite3
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_positioned
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Walter Horstman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|