Skip to content

Commit

Permalink
Merge pull request #180 from ifad/feature/rails-71-support
Browse files Browse the repository at this point in the history
Rails 7.1 edge support
  • Loading branch information
tagliala authored Oct 8, 2022
2 parents 83493f6 + d1e8729 commit 76a3540
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/active_record/connection_adapters/chronomodel_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

module ActiveRecord
module ConnectionHandling
def chronomodel_adapter_class
ConnectionAdapters::PostgreSQLAdapter
end

# Install the new adapter in ActiveRecord. This approach is required because
# the PG adapter defines +add_column+ itself, thus making impossible to use
Expand Down
2 changes: 1 addition & 1 deletion lib/chrono_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def chrono?
prepend ChronoModel::Patches::Batches::BatchEnumerator
end

if defined?(Rails::DBConsole)
if defined?(Rails::DBConsole) && Rails.version < '7.1'
Rails::DBConsole.instance_eval do
if Rails.version < '6.1'
prepend ChronoModel::Patches::DBConsole::Config
Expand Down
31 changes: 27 additions & 4 deletions lib/chrono_model/time_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,35 @@ 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 Rails.version >= '7.0'
alias_method :subclasses_with_history, :subclasses
def subclasses
subclasses_with_history.reject(&:history?)
end

# `direct_descendants` is deprecated method in 7.0 and has been
# removed in 7.1
if method_defined?(:direct_descendants)
alias_method :direct_descendants_with_history, :subclasses_with_history
alias_method :direct_descendants, :subclasses
end

# Ruby 3.1 has a native subclasses method and descendants is
# implemented with recursion of subclasses
if Class.method_defined?(:subclasses)
def descendants_with_history
subclasses_with_history.concat(subclasses.flat_map(&:descendants_with_history))
end
end
else
alias_method :descendants_with_history, :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
def descendants
descendants_with_history.reject(&:history?)
end
Expand Down
43 changes: 36 additions & 7 deletions spec/chrono_model/time_machine/sti_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,50 @@ class ::Element < ActiveRecord::Base
include ChronoModel::TimeMachine
end

class ::Publication < Element
class ::Publication < ::Element
end

class ::Magazine < ::Publication
end

describe '.descendants' do
subject { Element.descendants }
subject { Element.descendants.map(&:name) }

it { is_expected.to_not include(Element::History) }
it { is_expected.to include(Publication) }
it { is_expected.to match_array %w[Publication Magazine] }
end

describe '.descendants_with_history' do
subject { Element.descendants_with_history }
subject { Element.descendants_with_history.map(&:name) }

it { is_expected.to match_array %w[Element::History Publication Publication::History Magazine Magazine::History] }
end

if Element.respond_to?(:direct_descendants)
describe '.direct_descendants' do
subject { Element.direct_descendants.map(&:name) }

it { is_expected.to match_array %w[Publication] }
end

describe '.direct_descendants_with_history' do
subject { Element.direct_descendants_with_history.map(&:name) }

it { is_expected.to include(Element::History) }
it { is_expected.to include(Publication) }
it { is_expected.to match_array %w[Element::History Publication] }
end
end

if Rails.version >= '7.0'
describe '.subclasses' do
subject { Element.subclasses.map(&:name) }

it { is_expected.to match_array %w[Publication] }
end

describe '.subclasses_with_history' do
subject { Element.subclasses_with_history.map(&:name) }

it { is_expected.to match_array %w[Element::History Publication] }
end
end

describe 'timeline' do
Expand Down
1 change: 1 addition & 0 deletions spec/support/time_machine/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def adapter

def with_revert
adapter.transaction do
adapter.materialize_transactions if adapter.respond_to?(:materialize_transactions)
adapter.create_savepoint 'revert'

yield
Expand Down

0 comments on commit 76a3540

Please sign in to comment.