Module: Changebase::Inline::ActiveRecord

Extended by:
ActiveSupport::Concern
Defined in:
lib/changebase/inline/active_record.rb

Defined Under Namespace

Modules: PostgreSQLAdapter

Instance Method Summary collapse

Instance Method Details

#changebase_track(type) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/changebase/inline/active_record.rb', line 115

def changebase_track(type)
  return if !changebase_tracking
  return if type == :update && self.previous_changes.empty?

  # Go through each of the Model#attributes and grab the type from the
  # Model#type_for_attribute(attr) to do the serialization, grab the
  # column definition using Model#column_for_attribute(attr) to write the
  # type, and use Model.columns.index(col) to grab the index of the column
  # in the database.
  columns = self.class.columns.each_with_index.reduce([]) do |acc, (column, index)|
    identity = self.class.primary_key ? self.class.primary_key == column.name : true

    attr_type = self.type_for_attribute(column.name)
    value = self.attributes[column.name]
    previous_value = self.previous_changes[column.name].try(:[], 0)

    case type
    when :update
      previous_value ||= value
    when :delete
      previous_value ||= value
      value = nil
    end

    acc << {
      index: index,
      identity: identity,
      name: column.name,
      type: column.sql_type,
      value: attr_type.serialize(value),
      previous_value: attr_type.serialize(previous_value)
    }
    acc
  end

  # Emit the event
  changebase_transaction.event!({
    schema: columns[0].try(:[], :schema) || self.class.connection.current_schema,
    table: self.class.table_name,
    type: type,
    columns: columns,
    timestamp: Time.current
  })
end

#changebase_trackingObject



104
105
106
107
108
109
# File 'lib/changebase/inline/active_record.rb', line 104

def changebase_tracking
  if Changebase.configured?# && self.class.instance_variable_defined?(:@changebase)
    # self.class.instance_variable_get(:@changebase)
    {exclude: []}
  end
end

#changebase_transactionObject



111
112
113
# File 'lib/changebase/inline/active_record.rb', line 111

def changebase_transaction
  self.class.connection.changebase_transaction
end