Sha256: 693eaa4fbb77b7a2cbae900b7989132c31d45f0e369516b37e7025cdf7272b77

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module TemporalTables
  # This hooks in a "history" method to ActiveRecord::Base which will
  # return the class's History class.  The history class extends the original
  # class, but runs against the history table to provide temporal results.
  #
  #  class Person < ActiveRecord::Base
  #    attr_accessible :name
  #  end
  #
  #  Person         #=> Person(id: integer, name: string)
  #  Person.history #=> PersonHistory(history_id: integer, id: integer, name: string, eff_from: datetime, eff_to: datetime)
  module HistoryHook
    def self.included(base)
      base.class_eval do
        # Return this class's history class.
        # If it doesn't exist yet, create and initialize it, as well
        # as all dependent classes (through associations).
        def self.history
          raise "Can't view history of history" if name =~ /History$/

          history_class = "#{name}History"
          history_class.constantize
        rescue NameError
          # If the history class doesn't exist yet, create it
          new_class = Class.new(self) do
            include TemporalTables::TemporalClass
          end
          segments = history_class.split("::")
          object_class = segments[0...-1].inject(Object) { |o, s| o.const_get(s) }
          object_class.const_set segments.last, new_class

          # Traverse associations and make sure they have
          # history classes too.
          history_class.constantize.temporalize_associations!
          history_class.constantize
        end
      end
    end

    # Returns a scope for the list of all history records for this
    # particular object.
    def history
      clazz = is_a?(TemporalTables::TemporalClass) ? self.class : self.class.history
      oid = is_a?(TemporalTables::TemporalClass) ? orig_class.primary_key : self.class.primary_key
      clazz.unscoped.where(id: attributes[oid]).order(:eff_from)
    end
  end
end

ActiveRecord::Base.send :include, TemporalTables::HistoryHook

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
temporal_tables-0.8.1 lib/temporal_tables/history_hook.rb
temporal_tables-0.8.0 lib/temporal_tables/history_hook.rb
temporal_tables-0.7.1 lib/temporal_tables/history_hook.rb
temporal_tables-0.7.0 lib/temporal_tables/history_hook.rb