Sha256: e2102a1410bc41427c1af72df9938c0b0adc69a3c5b76756eee716aba0b9ab35

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

module ChronoModel
  module Patches

    module Relation
      include ChronoModel::Patches::AsOfTimeHolder

      def load
        return super unless @_as_of_time && !loaded?

        super.each {|record| record.as_of_time!(@_as_of_time) }
      end

      def merge(*)
        return super unless @_as_of_time

        super.as_of_time!(@_as_of_time)
      end

      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.
      #
      def chrono_join_history(join)
        # 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]
        return unless model

        join.left = ChronoModel::Patches::JoinNode.new(
          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
        )
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chrono_model-1.2.2 lib/chrono_model/patches/relation.rb
chrono_model-1.2.1 lib/chrono_model/patches/relation.rb
chrono_model-1.2.0 lib/chrono_model/patches/relation.rb
chrono_model-1.1.0 lib/chrono_model/patches/relation.rb