Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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/mechanisms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def in_positioning_scope?
end

def positioning_scope_changed?
scope_columns.any? do |scope_column|
scope_columns.flatten.any? do |scope_column|
Comment thread
brendon marked this conversation as resolved.
Outdated
@positioned.attribute_changed?(scope_column)
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/models/composite_foreign_key_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CompositeForeignKeyItem < ActiveRecord::Base
belongs_to :list, class_name: "CompositePrimaryKeyItem", foreign_key: [:cpki_item_id, :cpki_account_id]

positioned on: :list
end
2 changes: 2 additions & 0 deletions test/models/composite_primary_key_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ class CompositePrimaryKeyItem < ActiveRecord::Base

belongs_to :list

has_many :composite_foreign_key_items, -> { order(:position) }, foreign_key: [:cpki_item_id, :cpki_account_id], dependent: :destroy

positioned on: :list
end
9 changes: 9 additions & 0 deletions test/support/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@

add_index :composite_primary_key_items, [:list_id, :position], unique: true

create_table :composite_foreign_key_items, force: true do |t|
t.string :name
t.integer :position, null: false
t.integer :cpki_item_id, null: false
t.integer :cpki_account_id, null: false
end

add_index :composite_foreign_key_items, [:cpki_item_id, :cpki_account_id, :position], unique: true, name: "index_cfki_on_scope_and_position"

create_table :categories, force: true do |t|
t.string :name
t.integer :position, null: false
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require_relative "models/new_item"
require_relative "models/default_scope_item"
require_relative "models/composite_primary_key_item"
require_relative "models/composite_foreign_key_item"
require_relative "models/product"
require_relative "models/category"
require_relative "models/categorised_item"
Expand Down
54 changes: 52 additions & 2 deletions test/test_positioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,19 @@ 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")
parent = CompositePrimaryKeyItem.create(item_id: 1, account_id: 1, list: list, name: "Parent")
child = parent.composite_foreign_key_items.create(name: "Child 1")
parent.composite_foreign_key_items.create(name: "Child 2")

mechanisms = Positioning::Mechanisms.new(child, :position)
refute mechanisms.send(:destroyed_via_positioning_scope?)
Comment thread
brendon marked this conversation as resolved.

parent.destroy
assert mechanisms.send(:destroyed_via_positioning_scope?)
end
end

class TestPositioningScopes < Minitest::Test
Expand Down Expand Up @@ -606,6 +619,13 @@ def test_that_position_columns_will_cope_with_polymorphic_belong_to
assert_equal({position: {scope_columns: ["includable_id", "includable_type"], scope_associations: [:includable]}}, Entity.positioning_columns)
end

def test_that_position_columns_will_cope_with_composite_foreign_key
assert_equal(
{position: {scope_columns: [["cpki_item_id", "cpki_account_id"]], scope_associations: [:list]}},
CompositeForeignKeyItem.positioning_columns
)
end

def test_that_position_columns_must_have_unique_keys
assert_raises(Positioning::Error) do
Item.send :positioned, on: :list
Expand Down Expand Up @@ -1135,8 +1155,6 @@ def test_destroying_multiple_items

class TestCompositePrimaryKeyPositioning < TestPositioning
Comment thread
brendon marked this conversation as resolved.
def configure
skip if ActiveRecord.version < Gem::Version.new("7.1.0")

@association = :composite_primary_key_items
@id = Enumerator.new do |yielder|
number = 1
Expand All @@ -1149,6 +1167,38 @@ def configure
end
end

class TestCompositeForeignKeyPositioning < TestPositioning
def configure
@association = :composite_foreign_key_items
@id = Enumerator.new do |yielder|
loop do
yielder.yield(nil)
end
end
end

def setup
configure

list = List.create name: "List"
@first_list = CompositePrimaryKeyItem.create(item_id: 1, account_id: 1, list: list, name: "First List")
@second_list = CompositePrimaryKeyItem.create(item_id: 2, account_id: 2, list: list, name: "Second List")
@first_item = @first_list.send(@association).create id: @id.next, name: "First Item"
@second_item = @first_list.send(@association).create id: @id.next, name: "Second Item"
@third_item = @first_list.send(@association).create id: @id.next, name: "Third Item"
@fourth_item = @second_list.send(@association).create id: @id.next, name: "Fourth Item"
@fifth_item = @second_list.send(@association).create id: @id.next, name: "Fifth Item"
@sixth_item = @second_list.send(@association).create id: @id.next, name: "Sixth Item"

@models = [
@first_list, @second_list, @first_item, @second_item,
@third_item, @fourth_item, @fifth_item, @sixth_item
]

reload_models
end
end

class TestNoScopePositioning < Minitest::Test
include Minitest::Hooks

Expand Down
Loading