positioning 0.4.7 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0484d3a5e2c66cc3a052ddf7b84a77fb976062c1b8e4cff8aabf07606708917f'
4
- data.tar.gz: e0c48bf52a2c6172f9aeef7cd65a90d766d648249d2d4c64463cc34541e85e63
3
+ metadata.gz: 63a3ebb2bdc82dcc6ff4de2946abb06d95f671ef57397e04afe840505070c360
4
+ data.tar.gz: 54f4807848ed54c0c01028c97d4ca9e0eb15ed85ed55835b502f5f9da6ef5772
5
5
  SHA512:
6
- metadata.gz: 37200c924df3d9e7a7ce7ab32aba09f1e3dcda62e505eee98ad0b35239dbe26264b17402612d85ec97169ae4d678e484227812195b5d5db4aa3d96a647ff09bd
7
- data.tar.gz: 549596a8a3c6fe27297b746f92a1d2ed787c4d48405a12b9e0d10134ecd38aa8b326cc5d0f9065c1bc117cb416862322d1ce1fbabab700a3856a20943b1f9944
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
@@ -3,29 +3,32 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in positioning.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 13.0"
6
+ gem "rake", "~> 13.3"
7
7
 
8
- gem "minitest", "~> 5.0"
9
- gem "minitest-hooks", "~> 1.5.1"
10
- gem "mocha", "~> 2.1.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
16
+ gem "minitest-hooks", "~> 1.5.3"
17
+ gem "mocha", "~> 3.0.1"
11
18
 
12
- gem "standard", "~> 1.3"
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"]
20
28
  when "sqlite"
21
- if ENV["RAILS_VERSION"] &&
22
- Gem::Version.new(ENV["RAILS_VERSION"]) >= Gem::Version.new("7.2")
23
- gem "sqlite3", "~> 2.2.0"
24
- else
25
- gem "sqlite3", "~> 1.7.2"
26
- end
29
+ gem "sqlite3", "~> 2.9.0"
27
30
  when "postgresql"
28
- gem "pg", "~> 1.5.5"
31
+ gem "pg", "~> 1.6.3"
29
32
  else
30
- gem "mysql2", "~> 0.5.6"
33
+ gem "mysql2", "0.5.6"
31
34
  end
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Positioning
2
2
 
3
+ [![Ruby](https://github.com/brendon/positioning/actions/workflows/main.yml/badge.svg)](https://github.com/brendon/positioning/actions/workflows/main.yml)
4
+
3
5
  The aim of this gem is to allow you to easily position Active Record model instances within a scope of your choosing. In an ideal world this gem will give your model instances sequential integer positions beginning with `1`. Attempts are made to make all changes within a transaction so that position integers remain consistent. To this end, directly assigning a position is discouraged, instead you can move items by declaring an item's prior or subsequent item in the list and your item will be moved to be relative to that item.
4
6
 
5
7
  Positioning supports multiple lists per model with global, simple, and complex scopes.
@@ -87,7 +89,7 @@ belongs_to :list
87
89
  positioned on: :list
88
90
  ```
89
91
 
90
- 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:
91
93
 
92
94
  ```
93
95
  Item.heal_position_column! name: :desc
@@ -98,7 +100,9 @@ You may need to introduce your database constraints after healing your position
98
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.
99
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.
100
102
 
101
- 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:
102
106
 
103
107
  ```
104
108
  positioned on: :category, column: :category_position
@@ -274,7 +278,7 @@ If you must position child models separately, scope their position by the `type`
274
278
 
275
279
  ```rb
276
280
  positioned on: [:type]
277
- ```
281
+ ```
278
282
 
279
283
  ## Concurrency
280
284
 
@@ -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
@@ -211,7 +220,8 @@ module Positioning
211
220
  end
212
221
 
213
222
  def in_positioning_scope?
214
- @positioned.persisted? && positioning_scope.where(primary_key => [@positioned.id]).exists?
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?
@@ -222,7 +232,7 @@ module Positioning
222
232
 
223
233
  def destroyed_via_positioning_scope?
224
234
  @positioned.destroyed_by_association && scope_columns.any? do |scope_column|
225
- @positioned.destroyed_by_association.foreign_key == scope_column
235
+ Array(@positioned.destroyed_by_association.foreign_key).include?(scope_column)
226
236
  end
227
237
  end
228
238
  end
@@ -1,3 +1,3 @@
1
1
  module Positioning
2
- VERSION = "0.4.7"
2
+ VERSION = "0.4.9"
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
 
@@ -35,7 +35,7 @@ module Positioning
35
35
  reflection = reflections[scope_component]
36
36
 
37
37
  if reflection&.belongs_to?
38
- positioning_columns[column][:scope_columns] << reflection.foreign_key
38
+ positioning_columns[column][:scope_columns].concat Array(reflection.foreign_key)
39
39
  positioning_columns[column][:scope_columns] << reflection.foreign_type if reflection.polymorphic?
40
40
  positioning_columns[column][:scope_associations] << reflection.name
41
41
  else
@@ -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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: positioning
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brendon Muir
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  - !ruby/object:Gem::Version
78
78
  version: '0'
79
79
  requirements: []
80
- rubygems_version: 3.7.1
80
+ rubygems_version: 3.6.9
81
81
  specification_version: 4
82
82
  summary: Simple positioning for Active Record models.
83
83
  test_files: []