Module: TemporalTables::TemporalClass

Defined in:
lib/temporal_tables/temporal_class.rb

Overview

This is mixed into all History classes.

Defined Under Namespace

Modules: ClassMethods, STIWithHistory

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/temporal_tables/temporal_class.rb', line 6

def self.included(base) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  base.class_eval do
    base.extend ClassMethods

    self.table_name += '_h'

    cattr_accessor :visited_associations
    @visited_associations = []

    # The at_value field stores the time from the query that yielded
    # this record.
    attr_accessor :at_value

    class << self
      prepend STIWithHistory

      delegate :at, to: :all
    end

    # Iterates all associations, makes sure their history classes are
    # created and initialized, and modifies the associations to point
    # to the target classes' history classes.
    def self.temporalize_associations! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
      reflect_on_all_associations.dup.each do |association|
        next if @visited_associations.include?(association.name) || association.options[:polymorphic]

        @visited_associations << association.name

        # Calling .history here will ensure that the history class
        # for this association is created and initialized
        clazz = association.klass.history

        # Recreate the association, updating it to point at the
        # history class.  The foreign key is explicitly set since it's
        # inferred from the class_name, but shouldn't be in this case.
        send(
          association.macro, association.name,
          **association.options.merge(
            class_name: clazz.name,
            foreign_key: association.foreign_key
          )
        )
      end
    end
  end
end

Instance Method Details

#nextObject



101
102
103
# File 'lib/temporal_tables/temporal_class.rb', line 101

def next
  @next ||= history.where(self.class.arel_table[:eff_from].gt(eff_from)).first
end

#orig_classObject



85
86
87
# File 'lib/temporal_tables/temporal_class.rb', line 85

def orig_class
  self.class.orig_class
end

#orig_idObject



89
90
91
# File 'lib/temporal_tables/temporal_class.rb', line 89

def orig_id
  attributes[orig_class.primary_key]
end

#orig_objObject



93
94
95
# File 'lib/temporal_tables/temporal_class.rb', line 93

def orig_obj
  @orig_obj ||= orig_class.find_by(orig_class.primary_key => orig_id)
end

#prevObject



97
98
99
# File 'lib/temporal_tables/temporal_class.rb', line 97

def prev
  @prev ||= history.where(self.class.arel_table[:eff_from].lt(eff_from)).last
end