Skip to content
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

Rails 7.1 edge support #180

Merged
merged 3 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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