Class: TransientRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/transient_record.rb

Overview

Transient Record helps define transient tables and Active Record models.

It’s essential to understand Transient Record Contexts in order to use the library effectively. Let’s start the discussion with how Active Record handles connections.

Active Record organizes connection pools around classes. Connecting to multiple databases requires defining multiple abstract classes. For example:

class ApplicationRecord < ActiveRecord::Base
end

class AnalyticsRecord < ApplicationRecord
  self.abstract_class = true

  connects_to database: { writing: :analytics }
end

In this case, ApplicationRecord.connection returns a connection to the primary database and AnalyticsRecord.connection returns a connection to the other database.

A context is related to an Active Record base class that’s used to access the database directly and to define transient models. After defining an Active Record base class, a context can be created by calling TransientRecord.context_for and must be assigned to a constant.

After the context is created, you can create tables and models by calling Context#create_table and Context#define_model. When you’re done, call TransientRecord.cleanup to drop all transient tables and models

Examples:

Creating a table and a model

# Define the context for classes using ActiveRecord::Base to connect. It's
# a constant defined outside of the test suite.
Primary = TransientRecord.context_for ActiveRecord::Base

# #create_table is a wrapper around #create_table in Active Record, and
# works almost exactly like the that method.
Primary.create_table :users do |t|
  t.string :email, null: false
end.define_model do
  validates :email, presence: true
end

# Instantiate the model
user = Primary::User.new email: nil

# Clean up when done.
TransientRecord.cleanup

Defining a model for a pre-existing table

Primary = TransientRecord.context_for ActiveRecord::Base

Primary.define_model :User do
  validates :email, presence: true
end

user = Primary::User.new email: nil

Creating a table and a model in another database

#
Analytics = TransientRecord.context_for AnalyticsRecord

Analytics.create_table :events do |t|
  # ...
end.define_model do
  # ...
end

event = Analytics::Event.new

Defined Under Namespace

Modules: Context Classes: Error

Constant Summary collapse

VERSION =

Transient Record version number.

"2.0.0"

Class Method Summary collapse

Class Method Details

.cleanupObject



97
98
99
100
# File 'lib/transient_record.rb', line 97

def cleanup
  @contexts.each_value(&:cleanup)
  nil
end

.context_for(base_class) ⇒ Module

Creates a namespace for tables and models corresponding to the given base class.

Active Record sets up connection pools for abstract Active Record model classes.

Parameters:

  • base_class (Class)

    class inheriting from ActiveRecord::Base

Returns:

  • (Module)

    module where transient models will be defined; the module extends Context, so it’s instance methods can be called on the module.



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

def context_for base_class
  @contexts[base_class] ||= Context.create base_class
end