Skip to content

Commit

Permalink
Merge pull request #626 from kbrock/ancestry_sti_test
Browse files Browse the repository at this point in the history
Deprecate adding ancestry to the middle of an STI tree
  • Loading branch information
kbrock authored Mar 11, 2023
2 parents cfa09bd + bf947eb commit 0ba9506
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run_test_suite.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: run-test-suite
on:
push:
branches: [ master ]
branches: [ master, 4-3-stable ]
pull_request:
branches: [ master ]
branches: [ master, 4-3-stable ]

jobs:
test:
Expand Down
5 changes: 5 additions & 0 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module Ancestry
module HasAncestry
def has_ancestry options = {}

if base_class != self
ActiveSupport::Deprecation.warn("Please move has_ancestry to the root of the STI inheritance tree.")
end

# Check options
raise Ancestry::AncestryException.new(I18n.t("ancestry.option_must_be_hash")) unless options.is_a? Hash
options.each do |key, value|
Expand Down
32 changes: 29 additions & 3 deletions test/concerns/sti_support_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,39 @@ def test_sti_support
end
end

# not sure that we need to support this case
# where we are creating a tree in the middle of a model
def test_sti_support_with_from_subclass
AncestryTestDatabase.with_model :extra_columns => {:type => :string} do |model|
AncestryTestDatabase.with_model :ancestry_column => :t1,
:skip_ancestry => true,
:counter_cache => true,
:extra_columns => {:type => :string} do |model|
subclass1 = Object.const_set 'SubclassWithAncestry', Class.new(model)
subclass1.has_ancestry
subclass1.create!
subclass2 = Object.const_set 'SubclassOfSubclassWithAncestry', Class.new(subclass1)

ActiveSupport::Deprecation.silence do
# we are defining it one level below the parent ("model" class)
subclass1.has_ancestry :ancestry_column => :t1, :counter_cache => true
end

# if ancestry is not propogated, then create will fail

root = subclass1.create!
# this was the line that was blowing up for this orginal feature
child = subclass1.create!(:parent => root)
child2 = subclass2.create!(:parent => root)

# counter caches across class lines (going up to parent)

assert_equal 2, root.reload.children_count

# children

assert_equal [child, child2], root.children.order(:id)
assert_equal root, child.parent

Object.send :remove_const, 'SubclassWithAncestry'
Object.send :remove_const, 'SubclassOfSubclassWithAncestry'
end
end

Expand Down

0 comments on commit 0ba9506

Please sign in to comment.