Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
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 :composite_primary_key_item, foreign_key: [:cpki_item_id, :cpki_account_id]

positioned on: :composite_primary_key_item
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, 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
89 changes: 87 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: [:composite_primary_key_item]}},
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,73 @@ 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 Parent")
@second_list = CompositePrimaryKeyItem.create(item_id: 2, account_id: 2, list: list, name: "Second Parent")
@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

# Scope change tests are not applicable because CompositeForeignKeyItem's
Comment thread
brendon marked this conversation as resolved.
Outdated
# scope is :composite_primary_key_item, not :list
def test_that_an_item_is_added_to_the_end_of_a_new_scope_by_default
end

def test_that_an_item_is_added_to_position_of_a_new_scope_when_explicitly_set
end

def test_that_position_is_assignable_on_update_in_new_scope
end

def test_that_item_position_is_clamped_up_to_1_on_update_scope
end

def test_that_item_position_is_clamped_down_to_max_plus_1_on_update_scope
end

def test_that_items_are_moved_out_of_the_way_on_update_scope_with_before
end

def test_that_items_are_moved_out_of_the_way_on_update_scope_with_before_id
end

def test_that_items_are_moved_out_of_the_way_on_update_scope_with_before_nil
end

def test_that_items_are_moved_out_of_the_way_on_update_scope_with_after
end

def test_that_items_are_moved_out_of_the_way_on_update_scope_with_after_id
end

def test_that_items_are_moved_out_of_the_way_on_update_scope_with_after_nil
end
end

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

Expand Down
Loading