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



4
5
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
# File 'lib/temporal_tables/temporal_class.rb', line 4

def self.included(base)
  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!
      reflect_on_all_associations.dup.each do |association|
        unless @@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.class_name.constantize.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,
              primary_key: clazz.orig_class.primary_key
            )
          )
        end
      end
    end
  end
end

Instance Method Details

#nextObject



99
100
101
# File 'lib/temporal_tables/temporal_class.rb', line 99

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

#orig_classObject



83
84
85
# File 'lib/temporal_tables/temporal_class.rb', line 83

def orig_class
  self.class.orig_class
end

#orig_idObject



87
88
89
# File 'lib/temporal_tables/temporal_class.rb', line 87

def orig_id
  attributes[orig_class.primary_key]
end

#orig_objObject



91
92
93
# File 'lib/temporal_tables/temporal_class.rb', line 91

def orig_obj
  @orig_obj ||= orig_class.find_by_id orig_id
end

#prevObject



95
96
97
# File 'lib/temporal_tables/temporal_class.rb', line 95

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