Sha256: 776b8535ace26b86bed9338884f4df118c01fa23ee90e8a627ed2848ec677ee7

Contents?: true

Size: 786 Bytes

Versions: 5

Compression:

Stored size: 786 Bytes

Contents

# frozen_string_literal: true

module TemporalTables
  # Uses the time from the "at" field stored in the record to filter queries
  # made to associations.
  module AssociationExtensions
    def target_scope
      # Kludge: Check +public_methods+ instead of using +responds_to?+ to
      # bypass +delegate_missing_to+ calls, as in +ActiveStorage::Attachment+.
      # Using responds_to? results in an infinite loop stack overflow.
      if @owner.public_methods.include?(:at_value)
        # If this is a history record but no at time was given,
        # assume the record's effective to date
        super.at(@owner.at_value || @owner.eff_to)
      else
        super
      end
    end
  end
end

ActiveRecord::Associations::Association.prepend TemporalTables::AssociationExtensions

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
temporal_tables-2.0.0 lib/temporal_tables/association_extensions.rb
temporal_tables-1.1.0 lib/temporal_tables/association_extensions.rb
temporal_tables-1.0.3 lib/temporal_tables/association_extensions.rb
temporal_tables-1.0.2 lib/temporal_tables/association_extensions.rb
temporal_tables-1.0.1 lib/temporal_tables/association_extensions.rb