positioning 0.4.8 → 0.4.9
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/CHANGELOG.md +10 -0
- data/Gemfile +11 -3
- data/README.md +4 -2
- data/lib/positioning/healer.rb +25 -8
- data/lib/positioning/mechanisms.rb +15 -5
- data/lib/positioning/version.rb +1 -1
- data/lib/positioning.rb +3 -3
- metadata +3 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 63a3ebb2bdc82dcc6ff4de2946abb06d95f671ef57397e04afe840505070c360
|
|
4
|
+
data.tar.gz: 54f4807848ed54c0c01028c97d4ca9e0eb15ed85ed55835b502f5f9da6ef5772
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e74ecc5dde881ab23a5b9f70cf1c92c85b009ecd9e853ae879f1d2e831cdc52e402ff91168ecd5794f16329a099323de73c713d7afbd6e55b8c103c8a4e93887
|
|
7
|
+
data.tar.gz: 4eabb016e127fcf4a54d68e7191d5f813994ea5cc8df218736f2faf3019d4c8fa9250d7c027940b65495d4f48c97cc96808eae422e6079e90330aee812c3fdc1
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.4.9] - 2026-11-06
|
|
4
|
+
|
|
5
|
+
- Fix the healer not locking scopes without a `belongs_to` association (or with a nil one).
|
|
6
|
+
- Fix scope changes for records hidden by, or partially selected by, a default scope, and when changing part of a composite primary key.
|
|
7
|
+
- A default on the position column no longer places new records at the start of the list.
|
|
8
|
+
- Raise when `positioned` is called on an abstract class, as the error message already said.
|
|
9
|
+
- Fix `dup` not clearing positions, and `heal_*_column!` raising, on STI subclasses.
|
|
10
|
+
- Fix healing raising a unique index violation with a custom order, or with existing zero or negative positions. Tied positions are now healed in primary key order.
|
|
11
|
+
- Fix lists being corrupted when a record with a NULL position (or whose row is already gone) is updated or destroyed.
|
|
12
|
+
|
|
3
13
|
## [0.4.7] - 2025-08-09
|
|
4
14
|
|
|
5
15
|
- Fix `solidify_position` so that it properly raises an error for an invalid string position.
|
data/Gemfile
CHANGED
|
@@ -5,15 +5,23 @@ gemspec
|
|
|
5
5
|
|
|
6
6
|
gem "rake", "~> 13.3"
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
# Active Support 7.2.4+ requires minitest < 6. Without this, Bundler picks
|
|
9
|
+
# minitest 6 and an older Rails 7.2 patch release instead.
|
|
10
|
+
if ENV["RAILS_VERSION"] &&
|
|
11
|
+
Gem::Version.new(ENV["RAILS_VERSION"]) < Gem::Version.new("8.0")
|
|
12
|
+
gem "minitest", "~> 5.25"
|
|
13
|
+
else
|
|
14
|
+
gem "minitest", "~> 6.0"
|
|
15
|
+
end
|
|
9
16
|
gem "minitest-hooks", "~> 1.5.3"
|
|
10
17
|
gem "mocha", "~> 3.0.1"
|
|
11
18
|
|
|
12
19
|
gem "standard", "~> 1.52.0"
|
|
13
20
|
|
|
21
|
+
# RAILS_VERSION is a minor version (e.g. "7.2"); test against its latest patch release.
|
|
14
22
|
if ENV["RAILS_VERSION"]
|
|
15
|
-
gem "activerecord", ENV["RAILS_VERSION"]
|
|
16
|
-
gem "activesupport", ENV["RAILS_VERSION"]
|
|
23
|
+
gem "activerecord", "~> #{ENV["RAILS_VERSION"]}.0"
|
|
24
|
+
gem "activesupport", "~> #{ENV["RAILS_VERSION"]}.0"
|
|
17
25
|
end
|
|
18
26
|
|
|
19
27
|
case ENV["DB"]
|
data/README.md
CHANGED
|
@@ -89,7 +89,7 @@ belongs_to :list
|
|
|
89
89
|
positioned on: :list
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
-
you'll have a method called `heal_position_column!`. You can call this method and it will cycle through every existing scope combination in your database (every list with items in this case) and reset those items' position based on their current position order by default. You can pass in a custom order if you don't trust (or don't have) an existing order column. The custom order is passed through to the Active Record `reorder` method, so you can provide anything that that method accepts:
|
|
92
|
+
you'll have a method called `heal_position_column!`. **This method is named after the column used to store position values, so if you've overridden the column name the method name changes to match** (see [below](#heal-method-naming)). You can call this method and it will cycle through every existing scope combination in your database (every list with items in this case) and reset those items' position based on their current position order by default. You can pass in a custom order if you don't trust (or don't have) an existing order column. The custom order is passed through to the Active Record `reorder` method, so you can provide anything that that method accepts:
|
|
93
93
|
|
|
94
94
|
```
|
|
95
95
|
Item.heal_position_column! name: :desc
|
|
@@ -100,7 +100,9 @@ You may need to introduce your database constraints after healing your position
|
|
|
100
100
|
* We recommend a `null: false` constraint on the position column but if your existing column has `NULL` values, you'll need to fix those first. The heal method will heal `NULL` positions but depending on your database engine `NULL` positioned items might be placed at the start of the returned records or at the end (if positioning on the position column). Some databases allow this behaviour to be customised.
|
|
101
101
|
* We also recommend a unique index on the scope columns and the position column. If you have repeated position integers per scope you'll need to use the heal method to fix these first before applying the unique index in a separate migration step.
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
#### Heal Method Naming
|
|
104
|
+
|
|
105
|
+
The heal method name is named after the column used to store position values. By default this is `position`, so the method is `heal_position_column!`. If you override the column name then the method name changes to match. For example:
|
|
104
106
|
|
|
105
107
|
```
|
|
106
108
|
positioned on: :category, column: :category_position
|
data/lib/positioning/healer.rb
CHANGED
|
@@ -10,12 +10,15 @@ module Positioning
|
|
|
10
10
|
if scope_columns.present?
|
|
11
11
|
@model.unscope(:order).reselect(*scope_columns).distinct.each do |scope_record|
|
|
12
12
|
@model.transaction do
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
associated_records = scope_associations.filter_map do |scope_association|
|
|
14
|
+
scope_record.send(scope_association)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
if associated_records.any?
|
|
18
|
+
associated_records.each(&:lock!)
|
|
17
19
|
else
|
|
18
|
-
|
|
20
|
+
# Relation#lock! only flags the relation, it doesn't run a query
|
|
21
|
+
@model.where(scope_record.slice(*scope_columns)).lock.ids
|
|
19
22
|
end
|
|
20
23
|
|
|
21
24
|
sequence @model.where(scope_record.slice(*scope_columns))
|
|
@@ -23,7 +26,7 @@ module Positioning
|
|
|
23
26
|
end
|
|
24
27
|
else
|
|
25
28
|
@model.transaction do
|
|
26
|
-
@model.
|
|
29
|
+
@model.lock.ids
|
|
27
30
|
sequence @model
|
|
28
31
|
end
|
|
29
32
|
end
|
|
@@ -39,10 +42,24 @@ module Positioning
|
|
|
39
42
|
@model.positioning_columns[@column][:scope_associations]
|
|
40
43
|
end
|
|
41
44
|
|
|
45
|
+
def primary_key
|
|
46
|
+
@model.primary_key
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Records are first parked on distinct values below anything currently in use (and below 0),
|
|
50
|
+
# then flipped into 1..n in one statement, so a unique index on the position column can't be
|
|
51
|
+
# violated part way through.
|
|
42
52
|
def sequence(scope)
|
|
43
|
-
scope.
|
|
44
|
-
|
|
53
|
+
ids = scope.reorder(@order).order(*Array(primary_key)).pluck(*Array(primary_key))
|
|
54
|
+
return if ids.empty?
|
|
55
|
+
|
|
56
|
+
floor = [scope.minimum(@column) || 0, 0].min
|
|
57
|
+
|
|
58
|
+
ids.each.with_index(1) do |id, index|
|
|
59
|
+
@model.unscoped.where(primary_key => [id]).update_all @column => floor - index
|
|
45
60
|
end
|
|
61
|
+
|
|
62
|
+
scope.update_all @column => Arel::Nodes::Subtraction.new(Arel::Nodes.build_quoted(floor), @model.arel_table[@column])
|
|
46
63
|
end
|
|
47
64
|
end
|
|
48
65
|
end
|
|
@@ -16,6 +16,9 @@ module Positioning
|
|
|
16
16
|
def create_position
|
|
17
17
|
lock_positioning_scope!
|
|
18
18
|
|
|
19
|
+
# A column default isn't a requested position. Only an assigned value is.
|
|
20
|
+
clear_position unless position_changed?
|
|
21
|
+
|
|
19
22
|
solidify_position
|
|
20
23
|
|
|
21
24
|
expand(positioning_scope, position..)
|
|
@@ -31,7 +34,10 @@ module Positioning
|
|
|
31
34
|
solidify_position
|
|
32
35
|
move_out_of_the_way
|
|
33
36
|
|
|
34
|
-
|
|
37
|
+
# A record without a position doesn't occupy a slot, so there's no gap to close
|
|
38
|
+
if position_was.nil?
|
|
39
|
+
expand(positioning_scope, position..)
|
|
40
|
+
elsif positioning_scope_changed?
|
|
35
41
|
contract(positioning_scope_was, position_was..)
|
|
36
42
|
expand(positioning_scope, position..)
|
|
37
43
|
elsif position_was > position
|
|
@@ -46,7 +52,7 @@ module Positioning
|
|
|
46
52
|
lock_positioning_scope!
|
|
47
53
|
|
|
48
54
|
move_out_of_the_way
|
|
49
|
-
contract(positioning_scope, (position_was + 1)..)
|
|
55
|
+
contract(positioning_scope, (position_was + 1)..) unless position_was.nil?
|
|
50
56
|
end
|
|
51
57
|
end
|
|
52
58
|
|
|
@@ -76,8 +82,9 @@ module Positioning
|
|
|
76
82
|
end
|
|
77
83
|
end
|
|
78
84
|
|
|
85
|
+
# The record's row as it is in the database, regardless of default scopes or in-memory key changes
|
|
79
86
|
def record_scope
|
|
80
|
-
base_class.where primary_key => [@positioned.
|
|
87
|
+
base_class.unscoped.where primary_key => [@positioned.id_in_database]
|
|
81
88
|
end
|
|
82
89
|
|
|
83
90
|
def position
|
|
@@ -97,7 +104,9 @@ module Positioning
|
|
|
97
104
|
end
|
|
98
105
|
|
|
99
106
|
def position_was
|
|
100
|
-
@position_was
|
|
107
|
+
return @position_was if defined?(@position_was)
|
|
108
|
+
|
|
109
|
+
@position_was = record_scope.pick(@column)
|
|
101
110
|
end
|
|
102
111
|
|
|
103
112
|
def move_out_of_the_way
|
|
@@ -211,7 +220,8 @@ module Positioning
|
|
|
211
220
|
end
|
|
212
221
|
|
|
213
222
|
def in_positioning_scope?
|
|
214
|
-
@positioned.persisted? &&
|
|
223
|
+
@positioned.persisted? &&
|
|
224
|
+
positioning_scope.where(primary_key => [@positioned.id_in_database]).where.not(@column => nil).exists?
|
|
215
225
|
end
|
|
216
226
|
|
|
217
227
|
def positioning_scope_changed?
|
data/lib/positioning/version.rb
CHANGED
data/lib/positioning.rb
CHANGED
|
@@ -19,7 +19,7 @@ module Positioning
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def positioned(on: [], column: :position)
|
|
22
|
-
|
|
22
|
+
if abstract_class? || !base_class?
|
|
23
23
|
raise Error.new "can't be called on an abstract class or STI subclass."
|
|
24
24
|
end
|
|
25
25
|
|
|
@@ -56,7 +56,7 @@ module Positioning
|
|
|
56
56
|
before_destroy { Mechanisms.new(self, column).destroy_position }
|
|
57
57
|
|
|
58
58
|
define_singleton_method(:"heal_#{column}_column!") do |order = column|
|
|
59
|
-
Healer.new(
|
|
59
|
+
Healer.new(base_class, column, order).heal
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
end
|
|
@@ -65,7 +65,7 @@ module Positioning
|
|
|
65
65
|
def initialize_dup(other)
|
|
66
66
|
super
|
|
67
67
|
|
|
68
|
-
self.class.positioning_columns.keys.each do |positioning_column|
|
|
68
|
+
self.class.base_class.positioning_columns.keys.each do |positioning_column|
|
|
69
69
|
send :"#{positioning_column}=", nil
|
|
70
70
|
end
|
|
71
71
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: positioning
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brendon Muir
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activesupport
|
|
@@ -38,7 +37,6 @@ dependencies:
|
|
|
38
37
|
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
39
|
version: '6.1'
|
|
41
|
-
description:
|
|
42
40
|
email:
|
|
43
41
|
- brendon@spike.net.nz
|
|
44
42
|
executables: []
|
|
@@ -65,7 +63,6 @@ metadata:
|
|
|
65
63
|
source_code_uri: https://github.com/brendon/positioning
|
|
66
64
|
changelog_uri: https://github.com/brendon/positioning/blob/main/CHANGELOG.md
|
|
67
65
|
funding_uri: https://github.com/sponsors/brendon
|
|
68
|
-
post_install_message:
|
|
69
66
|
rdoc_options: []
|
|
70
67
|
require_paths:
|
|
71
68
|
- lib
|
|
@@ -80,8 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
80
77
|
- !ruby/object:Gem::Version
|
|
81
78
|
version: '0'
|
|
82
79
|
requirements: []
|
|
83
|
-
rubygems_version: 3.
|
|
84
|
-
signing_key:
|
|
80
|
+
rubygems_version: 3.6.9
|
|
85
81
|
specification_version: 4
|
|
86
82
|
summary: Simple positioning for Active Record models.
|
|
87
83
|
test_files: []
|