Class: ActiveRecord::Materialized::WriteChange

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/activerecord/materialized/write_change.rb

Overview

A committed write to a dependency table, captured as full before/after attribute snapshots (string-keyed). Snapshots are complete — not just the changed columns — so maintenance can compute deltas even when the GROUP BY key or a summed column did not change.

Constant Summary collapse

Operation =
T.type_alias { T.any(Symbol, String) }
Attributes =
T.type_alias { T::Hash[String, T.untyped] }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, operation:, before:, after:) ⇒ WriteChange

Returns a new instance of WriteChange.



29
30
31
32
33
34
# File 'lib/activerecord/materialized/write_change.rb', line 29

def initialize(table_name:, operation:, before:, after:)
  @table_name = table_name
  @operation = operation
  @before = before
  @after = after
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



26
27
28
# File 'lib/activerecord/materialized/write_change.rb', line 26

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



23
24
25
# File 'lib/activerecord/materialized/write_change.rb', line 23

def before
  @before
end

#operationObject (readonly)

Returns the value of attribute operation.



20
21
22
# File 'lib/activerecord/materialized/write_change.rb', line 20

def operation
  @operation
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



17
18
19
# File 'lib/activerecord/materialized/write_change.rb', line 17

def table_name
  @table_name
end

Class Method Details

.from_record(record, operation) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/activerecord/materialized/write_change.rb', line 37

def self.from_record(record, operation)
  table_name = record.class.table_name
  case operation.to_sym
  when :create
    new(table_name: table_name, operation: :create, before: {}, after: stringify_keys(record.attributes))
  when :update
    new(table_name: table_name, operation: :update, before: old_attributes(record),
        after: stringify_keys(record.attributes))
  when :destroy
    # attributes_in_database is emptied once the row is gone; use in-memory attributes.
    new(table_name: table_name, operation: :destroy, before: stringify_keys(record.attributes), after: {})
  else
    raise ArgumentError, "unsupported write operation: #{operation}"
  end
end