positioning 0.4.8 → 0.4.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b3882c55b3e657ad75fb0cabf586ae890506ba89acc34a198d32c31c2aa8521
4
- data.tar.gz: e8a3ffa61736d782745416743cf926daf2170be8beee94189adf9bf962515fd5
3
+ metadata.gz: ecb51b68f80bde257ed294934fd122a1fabff3acb3438767253afb614664d318
4
+ data.tar.gz: c688039c633220e0afb1b640375fed1dffaf312cd5e418eaa62fb4c5b60b0779
5
5
  SHA512:
6
- metadata.gz: 77acd60a82aef76786848043aeebe7ed58e1f6725592ef21ff0265eed16d1fdc4fd52024988b4e86e7537c990e6f6f1be8c6bb7392250a30dee8901ead8b3179
7
- data.tar.gz: ff80e90b8ed3d4d96f24f26c879a0fb4d570d92b22c4159ecc79683e9f56b39df46c0cb524a7a478781905a3e46bdc44012f6cca7d959f9b0d226f69de67d33d
6
+ metadata.gz: c588024c1fb254446fd782d03c04c57363bf49cb85911ecee07d525797b31a40b7f0b7389f71a372dcd23e084f234dfdb4d613242e5f88dc3ba382abf882f028
7
+ data.tar.gz: 640dd4e8e75db84a9053e54f57066ea4ab5f80216b84e5754b953851056f9409d0dc979f3d6a7038245e05b35b0f1e390153e5567aab6fe9f4a10fbde964e475
data/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.10] - 2026-11-09
4
+
5
+ - Fix MySQL deadlocks when adding a record to the end of a list while another transaction holds the list's first row and then waits on the scope lock, such as a `touch: true` chain from a child. When nothing needs to move, we no longer run the query that locked that row.
6
+
7
+ ## [0.4.9] - 2026-11-06
8
+
9
+ - Fix the healer not locking scopes without a `belongs_to` association (or with a nil one).
10
+ - Fix scope changes for records hidden by, or partially selected by, a default scope, and when changing part of a composite primary key.
11
+ - A default on the position column no longer places new records at the start of the list.
12
+ - Raise when `positioned` is called on an abstract class, as the error message already said.
13
+ - Fix `dup` not clearing positions, and `heal_*_column!` raising, on STI subclasses.
14
+ - 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.
15
+ - Fix lists being corrupted when a record with a NULL position (or whose row is already gone) is updated or destroyed.
16
+
17
+ ## [0.4.8] - 2026-03-28
18
+
19
+ - Fix `belongs_to` scopes with a composite foreign key, both when positioning records and when they're destroyed through their scope's parent.
20
+
3
21
  ## [0.4.7] - 2025-08-09
4
22
 
5
23
  - 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
- gem "minitest", "~> 6.0"
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
- The heal method name is named after the column used to store position values. By default this is `position` but if you override it then the method name will change:
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
@@ -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
- if scope_associations.present?
14
- scope_associations.each do |scope_association|
15
- scope_record.send(scope_association)&.lock!
16
- end
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
- @model.where(scope_record.slice(*scope_columns)).lock!
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.all.lock!
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.unscope(:select).reorder(@order).each.with_index(1) do |record, index|
44
- record.update_columns @column => index
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
- if positioning_scope_changed?
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.id]
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 ||= record_scope.pick(@column)
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
@@ -105,14 +114,17 @@ module Positioning
105
114
  record_scope.update_all @column => 0
106
115
  end
107
116
 
117
+ # Skip the second query if nothing moved, as MySQL would still lock the scope's first row to find no matches
108
118
  def expand(scope, range)
109
- scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1"
110
- scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 + 1"
119
+ if scope.where(@column => range).update_all("#{quoted_column} = #{quoted_column} * -1").positive?
120
+ scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 + 1"
121
+ end
111
122
  end
112
123
 
113
124
  def contract(scope, range)
114
- scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1"
115
- scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 - 1"
125
+ if scope.where(@column => range).update_all("#{quoted_column} = #{quoted_column} * -1").positive?
126
+ scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 - 1"
127
+ end
116
128
  end
117
129
 
118
130
  def solidify_position
@@ -211,7 +223,8 @@ module Positioning
211
223
  end
212
224
 
213
225
  def in_positioning_scope?
214
- @positioned.persisted? && positioning_scope.where(primary_key => [@positioned.id]).exists?
226
+ @positioned.persisted? &&
227
+ positioning_scope.where(primary_key => [@positioned.id_in_database]).where.not(@column => nil).exists?
215
228
  end
216
229
 
217
230
  def positioning_scope_changed?
@@ -1,3 +1,3 @@
1
1
  module Positioning
2
- VERSION = "0.4.8"
2
+ VERSION = "0.4.10"
3
3
  end
data/lib/positioning.rb CHANGED
@@ -19,7 +19,7 @@ module Positioning
19
19
  end
20
20
 
21
21
  def positioned(on: [], column: :position)
22
- unless base_class?
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(self, column, order).heal
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.8
4
+ version: 0.4.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brendon Muir
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2026-03-28 00:00:00.000000000 Z
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.5.22
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: []