Sha256: 4279f4f6d7d9bd9ed52507ba8bbaf3aad98dfab9ab8b0fae2e01969cbf1091e1

Contents?: true

Size: 1.73 KB

Versions: 10

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module PaperTrailAssociationTracking
  module Reifiers
    # Reify a single HABTM association of `model`.
    # @api private
    module HasAndBelongsToMany
      class << self
        # @api private
        def reify(pt_enabled, assoc, model, options, transaction_id)
          version_ids = ::PaperTrail::VersionAssociation.
            where("foreign_key_name = ?", assoc.name).
            where("version_id = ?", transaction_id).
            pluck(:foreign_key_id)

          model.send(assoc.name).proxy_association.target =
            version_ids.map do |id|
              if pt_enabled
                version = load_version(assoc, id, transaction_id, options[:version_at])
                if version
                  next version.reify(
                    options.merge(
                      has_many: false,
                      has_one: false,
                      belongs_to: false,
                      has_and_belongs_to_many: false
                    )
                  )
                end
              end
              assoc.klass.where(assoc.klass.primary_key => id).first
            end
        end

        private

        # Given a HABTM association `assoc` 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

10 entries across 10 versions & 1 rubygems

Version Path
paper_trail-association_tracking-2.2.1 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-2.2.0 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-2.1.3 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-2.1.1 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-2.1.0 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-2.0.0 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-1.1.1 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-1.1.0 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-1.0.0 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb
paper_trail-association_tracking-0.0.1 lib/paper_trail_association_tracking/reifiers/has_and_belongs_to_many.rb