Module: ChronoModel::Patches::Relation

Includes:
AsOfTimeHolder
Defined in:
lib/chrono_model/patches/relation.rb

Instance Method Summary collapse

Methods included from AsOfTimeHolder

#as_of_time, #as_of_time!

Instance Method Details

#build_arelObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chrono_model/patches/relation.rb', line 19

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

#build_preloaderObject

Build a preloader at the as_of_time of this relation. Pass the current model to define Relation



50
51
52
53
54
# File 'lib/chrono_model/patches/relation.rb', line 50

def build_preloader
  ActiveRecord::Associations::Preloader.new(
    model: self.model, as_of_time: as_of_time
  )
end

#chrono_join_history(join) ⇒ Object

Replaces a join with the current data with another that loads records As-Of time against the history data.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chrono_model/patches/relation.rb', line 34

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

#loadObject



7
8
9
10
11
# File 'lib/chrono_model/patches/relation.rb', line 7

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

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

#mergeObject



13
14
15
16
17
# File 'lib/chrono_model/patches/relation.rb', line 13

def merge(*)
  return super unless @_as_of_time

  super.as_of_time!(@_as_of_time)
end