Module: ChronoModel::Patches::Association

Defined in:
lib/chrono_model/patches.rb

Overview

Patches ActiveRecord::Associations::Association to add support for temporal associations.

Each record fetched from the as_of scope on the owner class will have an additional “as_of_time” field yielding the UTC time of the request, then the as_of scope is called on either this association's class or on the join model's (:through association) one.

Instance Method Summary collapse

Instance Method Details

#scopeObject

If the association class or the through association are ChronoModels, then fetches the records from a virtual table using a subquery scope to a specific timestamp.


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chrono_model/patches.rb', line 185

def scope
  scope = super
  return scope unless _chrono_record?

  if _chrono_target?
    # For standard associations, replace the table name with the virtual
    # as-of table name at the owner's as-of-time
    #
    scope = scope.from(klass.history.virtual_table_at(owner.as_of_time))
  elsif respond_to?(:through_reflection) && through_reflection.klass.chrono?

    # For through associations, replace the joined table name instead
    # with a virtual table that selects records from the history at
    # the given +as_of_time+.
    #
    scope.join_sources.each do |join|
      if join.left.name == through_reflection.klass.table_name
        history_model = through_reflection.klass.history

        join.left = JoinNode.new(join.left, history_model, owner.as_of_time)
      end
    end
  end

  scope.as_of_time!(owner.as_of_time)

  return scope
end

#skip_statement_cache?Boolean

Returns:

  • (Boolean)

178
179
180
# File 'lib/chrono_model/patches.rb', line 178

def skip_statement_cache?
  super || _chrono_target?
end