Skip to content

Commit

Permalink
Use active support on load hooks
Browse files Browse the repository at this point in the history
Close #231
  • Loading branch information
tagliala committed Sep 24, 2023
1 parent 9c15b68 commit 3898582
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
50 changes: 15 additions & 35 deletions lib/chrono_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'active_record'

require 'chrono_model/chrono'
require 'chrono_model/conversions'
require 'chrono_model/patches'
require 'chrono_model/adapter'
Expand Down Expand Up @@ -37,47 +38,26 @@ def self.history_models
require 'chrono_model/railtie'
end

ActiveRecord::Base.instance_eval do
# Checks whether this Active Recoed model is backed by a temporal table
#
def chrono?
return false unless connection.respond_to? :is_chrono?
ActiveSupport.on_load :active_record do
extend ChronoModel::Chrono

connection.is_chrono?(table_name)
end
end
# Hooks into Association#scope to pass the As-Of time automatically
# to methods that load associated ChronoModel records.
ActiveRecord::Associations::Association.prepend ChronoModel::Patches::Association

# Hooks into Association#scope to pass the As-Of time automatically
# to methods that load associated ChronoModel records.
#
ActiveRecord::Associations::Association.instance_eval do
prepend ChronoModel::Patches::Association
end
# Hooks into Relation#build_arel to use `:joins` on your ChronoModels
# and join data from associated records As-Of time.
ActiveRecord::Relation.prepend ChronoModel::Patches::Relation

# Hooks into Relation#build_arel to use :joins on your ChronoModels
# and join data from associated records As-Of time.
#
ActiveRecord::Relation.instance_eval do
prepend ChronoModel::Patches::Relation
end
# Hooks in two points of the AR Preloader to preload As-Of time records of
# associated ChronoModels. is used by `.includes`, `.preload`, and `.eager_load`.
ActiveRecord::Associations::Preloader.prepend ChronoModel::Patches::Preloader

# Hooks in two points of the AR Preloader to preload As-Of time records of
# associated ChronoModels. is used by .includes, .preload and .eager_load.
#
ActiveRecord::Associations::Preloader.instance_eval do
prepend ChronoModel::Patches::Preloader
end

ActiveRecord::Associations::Preloader::Association.instance_eval do
prepend ChronoModel::Patches::Preloader::Association
end
ActiveRecord::Associations::Preloader::Association.prepend ChronoModel::Patches::Preloader::Association

ActiveRecord::Associations::Preloader::ThroughAssociation.instance_eval do
prepend ChronoModel::Patches::Preloader::ThroughAssociation
end
ActiveRecord::Associations::Preloader::ThroughAssociation.prepend ChronoModel::Patches::Preloader::ThroughAssociation

ActiveRecord::Batches::BatchEnumerator.instance_eval do
prepend ChronoModel::Patches::Batches::BatchEnumerator
ActiveRecord::Batches::BatchEnumerator.prepend ChronoModel::Patches::Batches::BatchEnumerator
end

if defined?(Rails::DBConsole) && Rails.version < '7.1'
Expand Down
17 changes: 17 additions & 0 deletions lib/chrono_model/chrono.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module ChronoModel
# A module to add to ActiveRecord::Base to check if they are backed by
# temporal tables.
module Chrono
# Checks whether this Active Record model is backed by a temporal table
#
# @return [Boolean] false if the connection does not respond to is_chrono?
# the result of connection.is_chrono?(table_name) otherwise
def chrono?
return false unless connection.respond_to? :is_chrono?

connection.is_chrono?(table_name)
end
end
end

0 comments on commit 3898582

Please sign in to comment.