Sha256: b73168a1940beaabb83c7642e35799328e3526897572f609542ef9f90887908f

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module PaperTrailAssociationTracking
  module Reifiers
    # Reify a single `belongs_to` association of `model`.
    # @api private
    module BelongsTo
      class << self
        # @api private
        def reify(assoc, model, options, transaction_id)
          id = model.send(assoc.foreign_key)
          version = load_version(assoc, id, transaction_id, options[:version_at])
          record = load_record(assoc, id, options, version)
          model.send("#{assoc.name}=".to_sym, record)
        end

        private

        # Given a `belongs_to` association and a `version`, return a record that
        # can be assigned in order to reify that association.
        # @api private
        def load_record(assoc, id, options, version)
          if version.nil?
            assoc.klass.where(assoc.klass.primary_key => id).first
          else
            version.reify(
              options.merge(
                has_many: false,
                has_one: false,
                belongs_to: false,
                has_and_belongs_to_many: false
              )
            )
          end
        end

        # Given a `belongs_to` association and an `id`, return a version record
        # from the point in time identified by `transaction_id` or `version_at`.
        # @api private
        def load_version(assoc, id, transaction_id, version_at)
          assoc.klass.paper_trail.version_class.
            where("item_type = ?", assoc.klass.base_class.name).
            where("item_id = ?", id).
            where("created_at >= ? OR transaction_id = ?", version_at, transaction_id).
            order("id").limit(1).first
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paper_trail-association_tracking-1.0.0 lib/paper_trail_association_tracking/reifiers/belongs_to.rb
paper_trail-association_tracking-0.0.1 lib/paper_trail_association_tracking/reifiers/belongs_to.rb