lib/chrono_model/patches/relation.rb in chrono_model-1.2.2 vs lib/chrono_model/patches/relation.rb in chrono_model-2.0.0

- old
+ new

@@ -1,15 +1,36 @@ +# frozen_string_literal: true + module ChronoModel module Patches - module Relation include ChronoModel::Patches::AsOfTimeHolder + if ActiveRecord::Associations::Preloader.instance_methods.include?(:call) + def preload_associations(records) # :nodoc: + preload = preload_values + preload += includes_values unless eager_loading? + scope = StrictLoadingScope if strict_loading_value + + preload.each do |associations| + ActiveRecord::Associations::Preloader.new( + records: records, associations: associations, scope: scope, model: model, as_of_time: as_of_time + ).call + end + end + end + + def empty_scope? + return super unless @_as_of_time + + @values == klass.unscoped.as_of(as_of_time).values + end + def load return super unless @_as_of_time && !loaded? - super.each {|record| record.as_of_time!(@_as_of_time) } + super.each { |record| record.as_of_time!(@_as_of_time) } end def merge(*) return super unless @_as_of_time @@ -18,15 +39,13 @@ def build_arel(*) return super unless @_as_of_time super.tap do |arel| - arel.join_sources.each do |join| chrono_join_history(join) end - end end # Replaces a join with the current data with another that # loads records As-Of time against the history data. @@ -35,24 +54,50 @@ # This case happens with nested includes, where the below # code has already replaced the join.left with a JoinNode. # return if join.left.respond_to?(:as_of_time) - model = ChronoModel.history_models[join.left.table_name] + model = + if join.left.respond_to?(:table_name) + ChronoModel.history_models[join.left.table_name] + else + ChronoModel.history_models[join.left] + end + return unless model join.left = ChronoModel::Patches::JoinNode.new( - join.left, model.history, @_as_of_time) + join.left, model.history, @_as_of_time + ) end # Build a preloader at the +as_of_time+ of this relation. # Pass the current model to define Relation # def build_preloader ActiveRecord::Associations::Preloader.new( - model: self.model, as_of_time: as_of_time + model: model, as_of_time: as_of_time ) end - end + def find_nth(*) + return super unless try(:history?) + + with_hid_pkey { super } + end + + def last(*) + return super unless try(:history?) + + with_hid_pkey { super } + end + + private + + def ordered_relation + return super unless try(:history?) + + with_hid_pkey { super } + end + end end end