From fd6272ddcde0afcdb881b4e0f8497defcbab0c3e Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sun, 24 Sep 2023 13:54:49 +0200 Subject: [PATCH] Use active support on load hooks Close #231 --- lib/chrono_model.rb | 56 ++++++++++++-------------------------- lib/chrono_model/chrono.rb | 17 ++++++++++++ 2 files changed, 35 insertions(+), 38 deletions(-) create mode 100644 lib/chrono_model/chrono.rb diff --git a/lib/chrono_model.rb b/lib/chrono_model.rb index f90c232..44e9a2c 100644 --- a/lib/chrono_model.rb +++ b/lib/chrono_model.rb @@ -2,6 +2,7 @@ require 'active_record' +require 'chrono_model/chrono' require 'chrono_model/conversions' require 'chrono_model/patches' require 'chrono_model/adapter' @@ -33,51 +34,30 @@ def self.history_models end end -if defined?(Rails::Railtie) - require 'chrono_model/railtie' -end +ActiveSupport.on_load :active_record do + extend ChronoModel::Chrono -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? + # 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 - connection.is_chrono?(table_name) - end -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 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 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 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 + ActiveRecord::Associations::Preloader::Association.prepend ChronoModel::Patches::Preloader::Association -# 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::ThroughAssociation.prepend ChronoModel::Patches::Preloader::ThroughAssociation -ActiveRecord::Associations::Preloader::Association.instance_eval do - prepend ChronoModel::Patches::Preloader::Association -end + ActiveRecord::Batches::BatchEnumerator.prepend ChronoModel::Patches::Batches::BatchEnumerator -ActiveRecord::Associations::Preloader::ThroughAssociation.instance_eval do - prepend ChronoModel::Patches::Preloader::ThroughAssociation -end - -ActiveRecord::Batches::BatchEnumerator.instance_eval do - prepend ChronoModel::Patches::Batches::BatchEnumerator + if defined?(Rails::Railtie) + require 'chrono_model/railtie' + end end if defined?(Rails::DBConsole) && Rails.version < '7.1' diff --git a/lib/chrono_model/chrono.rb b/lib/chrono_model/chrono.rb new file mode 100644 index 0000000..8385303 --- /dev/null +++ b/lib/chrono_model/chrono.rb @@ -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