Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/positioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def positioned(on: [], column: :position)
reflection = reflections[scope_component]

if reflection&.belongs_to?
positioning_columns[column][:scope_columns] << reflection.foreign_key
positioning_columns[column][:scope_columns].concat Array(reflection.foreign_key).map(&:to_s)
Comment thread
brendon marked this conversation as resolved.
Outdated
positioning_columns[column][:scope_columns] << reflection.foreign_type if reflection.polymorphic?
positioning_columns[column][:scope_associations] << reflection.name
else
Expand Down
8 changes: 6 additions & 2 deletions lib/positioning/mechanisms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,12 @@ def positioning_scope_changed?
end

def destroyed_via_positioning_scope?
@positioned.destroyed_by_association && scope_columns.any? do |scope_column|
@positioned.destroyed_by_association.foreign_key == scope_column
return false unless @positioned.destroyed_by_association
Comment thread
brendon marked this conversation as resolved.
Outdated

foreign_keys = Array(@positioned.destroyed_by_association.foreign_key).map(&:to_s)
Comment thread
brendon marked this conversation as resolved.
Outdated

scope_columns.any? do |scope_column|
foreign_keys.include?(scope_column.to_s)
Comment thread
brendon marked this conversation as resolved.
Outdated
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/test_positioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,32 @@ def test_destroyed_via_positioning_scope?
list.destroy
assert mechanisms.send(:destroyed_via_positioning_scope?)
end

def test_destroyed_via_positioning_scope_with_composite_foreign_key
list = List.create name: "List"
item = list.items.create name: "Item"

mechanisms = Positioning::Mechanisms.new(item, :position)

mock_association = mock
Comment thread
brendon marked this conversation as resolved.
Outdated
mock_association.stubs(:foreign_key).returns(["list_id", "other_key"])
item.stubs(:destroyed_by_association).returns(mock_association)

assert mechanisms.send(:destroyed_via_positioning_scope?)
end

def test_destroyed_via_positioning_scope_with_composite_foreign_key_no_match
list = List.create name: "List"
item = list.items.create name: "Item"

mechanisms = Positioning::Mechanisms.new(item, :position)

mock_association = mock
mock_association.stubs(:foreign_key).returns(["other_key", "another_key"])
item.stubs(:destroyed_by_association).returns(mock_association)

refute mechanisms.send(:destroyed_via_positioning_scope?)
end
end

class TestPositioningScopes < Minitest::Test
Expand Down
Loading