-
Notifications
You must be signed in to change notification settings - Fork 248
feat: Add dependent: :adopt option for has_closure_tree
#471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,270 @@ | ||||
| # frozen_string_literal: true | ||||
|
|
||||
| require 'test_helper' | ||||
|
|
||||
| def run_adopt_tests_for(model_class) | ||||
| describe "#{model_class} with dependent: :adopt" do | ||||
| before do | ||||
| model_class.delete_all | ||||
| model_class.hierarchy_class.delete_all | ||||
| end | ||||
|
|
||||
| it 'moves children to grandparent when parent is destroyed and updates hierarchy table' do | ||||
| p1 = model_class.create!(name: 'p1') | ||||
| p2 = model_class.create!(name: 'p2', parent: p1) | ||||
| p3 = model_class.create!(name: 'p3', parent: p2) | ||||
| p4 = model_class.create!(name: 'p4', parent: p3) | ||||
|
|
||||
| # Verify initial structure: p1 -> p2 -> p3 -> p4 | ||||
| assert_equal p2, p3.parent | ||||
| assert_equal p3, p4.parent | ||||
| assert_equal p1, p2.parent | ||||
|
|
||||
| # Verify initial hierarchy table entries | ||||
| hierarchy = model_class.hierarchy_class | ||||
| assert hierarchy.where(ancestor_id: p1.id, descendant_id: p4.id, generations: 3).exists? | ||||
| assert hierarchy.where(ancestor_id: p2.id, descendant_id: p4.id, generations: 2).exists? | ||||
| assert hierarchy.where(ancestor_id: p3.id, descendant_id: p4.id, generations: 1).exists? | ||||
| assert hierarchy.where(ancestor_id: p3.id, descendant_id: p3.id, generations: 0).exists? | ||||
|
|
||||
| # Destroy p3 | ||||
| p3.destroy | ||||
|
|
||||
| # After destroying p3, p4 should be adopted by p2 (p3's parent) | ||||
| p4.reload | ||||
| p2.reload | ||||
| assert_equal p2, p4.parent, 'p4 should be moved to p2 (grandparent)' | ||||
| assert_equal p1, p2.parent, 'p2 should still have p1 as parent' | ||||
| assert_equal [p4], p2.children.to_a, 'p2 should have p4 as child' | ||||
|
|
||||
| # Verify hierarchy table was updated correctly | ||||
| # p3 should be removed from hierarchy | ||||
| assert_empty hierarchy.where(ancestor_id: p3.id) | ||||
| assert_empty hierarchy.where(descendant_id: p3.id) | ||||
|
|
||||
| # p4 should now have p2 as direct parent (generations: 1) | ||||
| assert hierarchy.where(ancestor_id: p2.id, descendant_id: p4.id, generations: 1).exists? | ||||
| # p4 should have p1 as ancestor (generations: 2) | ||||
| assert hierarchy.where(ancestor_id: p1.id, descendant_id: p4.id, generations: 2).exists? | ||||
| # p4 should have itself (generations: 0) | ||||
| assert hierarchy.where(ancestor_id: p4.id, descendant_id: p4.id, generations: 0).exists? | ||||
| end | ||||
|
|
||||
| it 'moves children to root when parent without grandparent is destroyed and updates hierarchy table' do | ||||
| p1 = model_class.create!(name: 'p1') | ||||
| p2 = model_class.create!(name: 'p2', parent: p1) | ||||
| p3 = model_class.create!(name: 'p3', parent: p2) | ||||
|
|
||||
| # Verify initial structure: p1 -> p2 -> p3 | ||||
| assert_equal p1, p2.parent | ||||
| assert_equal p2, p3.parent | ||||
|
|
||||
| hierarchy = model_class.hierarchy_class | ||||
| initial_p2_hierarchies = hierarchy.where(ancestor_id: p2.id).count | ||||
| initial_p3_hierarchies = hierarchy.where(descendant_id: p3.id).count | ||||
|
||||
| initial_p3_hierarchies = hierarchy.where(descendant_id: p3.id).count |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assignment to initial_count is useless, since its value is never read.
| initial_count = hierarchy.count |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,7 @@ | ||||
| # frozen_string_literal: true | ||||
|
|
||||
| class AdoptableTag < ApplicationRecord | ||||
| has_closure_tree dependent: :adopt, name_column: 'name' | ||||
| end | ||||
|
|
||||
|
|
||||
|
Comment on lines
+6
to
+7
|
||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,6 @@ | ||||
| # frozen_string_literal: true | ||||
|
|
||||
| class MemoryAdoptableTag < SqliteRecord | ||||
| has_closure_tree dependent: :adopt, name_column: 'name' | ||||
| end | ||||
|
|
||||
|
||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,6 @@ | ||||
| # frozen_string_literal: true | ||||
|
|
||||
| class MysqlAdoptableTag < MysqlRecord | ||||
| has_closure_tree dependent: :adopt, name_column: 'name' | ||||
| end | ||||
|
|
||||
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assignment to initial_p2_hierarchies is useless, since its value is never read.