diff --git a/CHANGELOG.md b/CHANGELOG.md index 4173b71..59218d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## [Unreleased] +- Fix healing a list with a default scope `:order` and/or `:select`. Thanks @LukasSkywalker! + ## [0.4.4] - 2024-11-20 - Add `funding_uri` to gemspec. diff --git a/lib/positioning/healer.rb b/lib/positioning/healer.rb index 5548fd9..d1b058e 100644 --- a/lib/positioning/healer.rb +++ b/lib/positioning/healer.rb @@ -8,7 +8,7 @@ def initialize(model, column, order) def heal if scope_columns.present? - @model.unscope(:select, :order).select(*scope_columns).distinct.each do |scope_record| + @model.unscope(:order).reselect(*scope_columns).distinct.each do |scope_record| @model.transaction do if scope_associations.present? scope_associations.each do |scope_association| @@ -18,7 +18,7 @@ def heal @model.where(scope_record.slice(*scope_columns)).lock! end - sequence @model.unscope(:select, :order).where(scope_record.slice(*scope_columns)) + sequence @model.where(scope_record.slice(*scope_columns)) end end else @@ -40,7 +40,7 @@ def scope_associations end def sequence(scope) - scope.reorder(@order).each.with_index(1) do |record, index| + scope.unscope(:select).reorder(@order).each.with_index(1) do |record, index| record.update_columns @column => index end end diff --git a/test/models/default_scope_item.rb b/test/models/default_scope_item.rb index af36d25..3853af9 100644 --- a/test/models/default_scope_item.rb +++ b/test/models/default_scope_item.rb @@ -3,5 +3,5 @@ class DefaultScopeItem < ActiveRecord::Base positioned on: :list - default_scope -> { select(:name).order(:position) } + default_scope -> { select(:name, :position).order(:position) } end