Skip to content

Commit

Permalink
Conditionally overload direct_descendants
Browse files Browse the repository at this point in the history
Rails 7.1 removed the deprecated method `direct_descendants` in favor
of `subclasses`

Subclasses implementation should be added in a different PR because it is not
trivial to implement

Ref #179

Ref:
- rails/rails@8f8aa85 rails/rails#39505
- rails/rails@1aa2463 rails/rails#46144
  • Loading branch information
tagliala committed Oct 3, 2022
1 parent 83493f6 commit 0c15d84
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/chrono_model/time_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ module TimeMachine
ChronoModel.history_models[table_name] = history

class << self
alias_method :direct_descendants_with_history, :direct_descendants
def direct_descendants
direct_descendants_with_history.reject(&:history?)
if method_defined?(:direct_descendants)
alias_method :direct_descendants_with_history, :direct_descendants
def direct_descendants
direct_descendants_with_history.reject(&:history?)
end
end

alias_method :descendants_with_history, :descendants
Expand Down
16 changes: 16 additions & 0 deletions spec/chrono_model/time_machine/sti_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ class ::Publication < Element
it { is_expected.to include(Publication) }
end

if ActiveRecord::Base.respond_to?(:direct_descendants)
describe '.direct_descendants' do
subject { Element.direct_descendants }

it { is_expected.to_not include(Element::History) }
it { is_expected.to include(Publication) }
end

describe '.direct_descendants_with_history' do
subject { Element.direct_descendants_with_history }

it { is_expected.to include(Element::History) }
it { is_expected.to include(Publication) }
end
end

describe 'timeline' do
let(:publication) do
pub = ts_eval { Publication.create! title: 'wrong title' }
Expand Down

0 comments on commit 0c15d84

Please sign in to comment.