From 21828b76b0e17617adeb73929b6153daf4056ca5 Mon Sep 17 00:00:00 2001 From: Jacko Zuidema Date: Wed, 1 Oct 2025 11:39:02 +0200 Subject: [PATCH 1/3] Allow touching of records on update positioning --- lib/positioning.rb | 12 ++++++------ lib/positioning/mechanisms.rb | 11 ++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/positioning.rb b/lib/positioning.rb index 638de7d..fa9d5dc 100644 --- a/lib/positioning.rb +++ b/lib/positioning.rb @@ -18,7 +18,7 @@ def positioning_columns @positioning_columns ||= {} end - def positioned(on: [], column: :position) + def positioned(on: [], column: :position, touch: false) unless base_class? raise Error.new "can't be called on an abstract class or STI subclass." end @@ -43,17 +43,17 @@ def positioned(on: [], column: :position) end end - define_method(:"prior_#{column}") { Mechanisms.new(self, column).prior } - define_method(:"subsequent_#{column}") { Mechanisms.new(self, column).subsequent } + define_method(:"prior_#{column}") { Mechanisms.new(self, column, touch).prior } + define_method(:"subsequent_#{column}") { Mechanisms.new(self, column, touch).subsequent } redefine_method(:"#{column}=") do |position| send :"#{column}_will_change!" super(position) end - before_create { Mechanisms.new(self, column).create_position } - before_update { Mechanisms.new(self, column).update_position } - before_destroy { Mechanisms.new(self, column).destroy_position } + before_create { Mechanisms.new(self, column, touch).create_position } + before_update { Mechanisms.new(self, column, touch).update_position } + before_destroy { Mechanisms.new(self, column, touch).destroy_position } define_singleton_method(:"heal_#{column}_column!") do |order = column| Healer.new(self, column, order).heal diff --git a/lib/positioning/mechanisms.rb b/lib/positioning/mechanisms.rb index d6bfa69..7985843 100644 --- a/lib/positioning/mechanisms.rb +++ b/lib/positioning/mechanisms.rb @@ -1,8 +1,9 @@ module Positioning class Mechanisms - def initialize(positioned, column) + def initialize(positioned, column, touch) @positioned = positioned @column = column.to_sym + @touch = touch end def prior @@ -108,11 +109,19 @@ def move_out_of_the_way def expand(scope, range) scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1" scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 + 1" + if @touch + scope.where(@column => range).touch_all + scope.where(@column => ..-1).touch_all + end end def contract(scope, range) scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1" scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 - 1" + if @touch + scope.where(@column => range).touch_all + scope.where(@column => ..-1).touch_all + end end def solidify_position From 53bf098f66ec1435adf8a50e3a63016393c26b54 Mon Sep 17 00:00:00 2001 From: Jacko Zuidema Date: Wed, 1 Oct 2025 11:50:34 +0200 Subject: [PATCH 2/3] Initialize each record to trigger callbacks --- lib/positioning/mechanisms.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/positioning/mechanisms.rb b/lib/positioning/mechanisms.rb index 7985843..d606ddc 100644 --- a/lib/positioning/mechanisms.rb +++ b/lib/positioning/mechanisms.rb @@ -110,8 +110,8 @@ def expand(scope, range) scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1" scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 + 1" if @touch - scope.where(@column => range).touch_all - scope.where(@column => ..-1).touch_all + scope.where(@column => range).find_each { it.touch } + scope.where(@column => ..-1).find_each { it.touch } end end @@ -119,8 +119,8 @@ def contract(scope, range) scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1" scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 - 1" if @touch - scope.where(@column => range).touch_all - scope.where(@column => ..-1).touch_all + scope.where(@column => range).find_each { it.touch } + scope.where(@column => ..-1).find_each { it.touch } end end From 2659bd238521a49d44c6db6a67a889fb1cc8faae Mon Sep 17 00:00:00 2001 From: Jacko Zuidema Date: Wed, 1 Oct 2025 13:10:19 +0200 Subject: [PATCH 3/3] memoize updated records --- lib/positioning/mechanisms.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/positioning/mechanisms.rb b/lib/positioning/mechanisms.rb index d606ddc..85fd907 100644 --- a/lib/positioning/mechanisms.rb +++ b/lib/positioning/mechanisms.rb @@ -107,20 +107,20 @@ def move_out_of_the_way end def expand(scope, range) - scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1" + records = scope.where(@column => range) + records.update_all "#{quoted_column} = #{quoted_column} * -1" scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 + 1" if @touch - scope.where(@column => range).find_each { it.touch } - scope.where(@column => ..-1).find_each { it.touch } + records.find_each { it.touch } end end def contract(scope, range) - scope.where(@column => range).update_all "#{quoted_column} = #{quoted_column} * -1" + records = scope.where(@column => range) + records.update_all "#{quoted_column} = #{quoted_column} * -1" scope.where(@column => ..-1).update_all "#{quoted_column} = #{quoted_column} * -1 - 1" if @touch - scope.where(@column => range).find_each { it.touch } - scope.where(@column => ..-1).find_each { it.touch } + records.find_each { it.touch } end end