Skip to content

Commit

Permalink
Fix unscope and reselecting/reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
brendon committed Dec 3, 2024
1 parent 3a5ee0f commit b43d1cc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions lib/positioning/healer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/models/default_scope_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b43d1cc

Please sign in to comment.