Skip to content

Commit

Permalink
Deprecate adding ancestry to the middle of an STI tree
Browse files Browse the repository at this point in the history
Provide deprecation warning for has_ancestry in the middle of a tree

Still haven't thought up with a good use case for this.
  • Loading branch information
kbrock committed Mar 10, 2023
1 parent 3879d3d commit bf947eb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 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
6 changes: 4 additions & 2 deletions test/concerns/sti_support_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ def test_sti_support_with_from_subclass
subclass1 = Object.const_set 'SubclassWithAncestry', Class.new(model)
subclass2 = Object.const_set 'SubclassOfSubclassWithAncestry', Class.new(subclass1)

# we are defining it one level below the parent ("model" class)
subclass1.has_ancestry :ancestry_column => :t1, :counter_cache => true
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

Expand Down

0 comments on commit bf947eb

Please sign in to comment.