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



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

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



74
75
76
77
78
# File 'lib/chrono_model/patches/relation.rb', line 74

def build_preloader
  ActiveRecord::Associations::Preloader.new(
    model: 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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chrono_model/patches/relation.rb', line 51

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 =
    if join.left.respond_to?(:table_name)
      ChronoModel.history_models[join.left.table_name]
    else
      ChronoModel.history_models[join.left]
    end

  return unless model

  join.left = ChronoModel::Patches::JoinNode.new(
    join.left, model.history, @_as_of_time
  )
end

#empty_scope?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/chrono_model/patches/relation.rb', line 20

def empty_scope?
  return super unless @_as_of_time

  @values == klass.unscoped.as_of(as_of_time).values
end

#find_nthObject



80
81
82
83
84
# File 'lib/chrono_model/patches/relation.rb', line 80

def find_nth(*)
  return super unless try(:history?)

  with_hid_pkey { super }
end

#lastObject



86
87
88
89
90
# File 'lib/chrono_model/patches/relation.rb', line 86

def last(*)
  return super unless try(:history?)

  with_hid_pkey { super }
end

#loadObject



26
27
28
29
30
# File 'lib/chrono_model/patches/relation.rb', line 26

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

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

#mergeObject



32
33
34
35
36
# File 'lib/chrono_model/patches/relation.rb', line 32

def merge(*)
  return super unless @_as_of_time

  super.as_of_time!(@_as_of_time)
end

#preload_associations(records) ⇒ Object

:nodoc:



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/chrono_model/patches/relation.rb', line 8

def preload_associations(records) # :nodoc:
  preload = preload_values
  preload += includes_values unless eager_loading?
  scope = StrictLoadingScope if strict_loading_value

  preload.each do |associations|
    ActiveRecord::Associations::Preloader.new(
      records: records, associations: associations, scope: scope, model: model, as_of_time: as_of_time
    ).call
  end
end