Module: ActiveHistory::Adapter::ActiveRecord

Extended by:
ActiveSupport::Concern
Defined in:
lib/activehistory/adapters/active_record.rb

Instance Method Summary collapse

Instance Method Details

#activehistory_association_changed(relation_name, added: [], removed: [], timestamp: nil, type: :update) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/activehistory/adapters/active_record.rb', line 242

def activehistory_association_changed(relation_name, added: [], removed: [], timestamp: nil, type: :update)
  timestamp ||= activehistory_timestamp
  
  self.class.activehistory_association_changed(id, relation_name, {
    added: added,
    removed: removed,
    timestamp: timestamp,
    type: type
  })
end

#activehistory_association_udpated(reflection, id, added: [], removed: [], timestamp: nil, type: :update) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/activehistory/adapters/active_record.rb', line 253

def activehistory_association_udpated(reflection, id, added: [], removed: [], timestamp: nil, type: :update)
  return if !activehistory_tracking || (removed.empty? && added.empty?)
  klass = reflection.active_record
  inverse_klass = reflection.klass
  
  inverse_association = if activehistory_tracking.has_key?(:habtm_model)
    inverse_klass.reflect_on_association(activehistory_tracking.dig(:habtm_model, reflection.name.to_s.singularize.to_sym, :inverse_of))
  else
    reflection.inverse_of
  end
  
  if inverse_association.nil?
    puts "NO INVERSE for #{self.class}.#{reflection.name}!!!"
    return
  end
  
  action = activehistory_event.action_for(klass, id, {
    type: type,
    timestamp: timestamp
  })
  
  action.diff ||= {}
  if (reflection.collection? || activehistory_tracking[:habtm_model])
    diff_key = "#{reflection.name.to_s.singularize}_ids"
    action.diff[diff_key] ||= [[], []]
    action.diff[diff_key][0] |= removed
    action.diff[diff_key][1] |= added
  else
    diff_key = "#{reflection.name.to_s.singularize}_id"
    action.diff[diff_key] ||= [removed.first, added.first]
  end
  
  removed.each do |removed_id|
    action = activehistory_event.action_for(inverse_klass, removed_id, {
      type: type,
      timestamp: timestamp
    })
  
    action.diff ||= {}

    if inverse_association.collection? || activehistory_tracking[:habtm_model]
      diff_key = "#{inverse_association.name.to_s.singularize}_ids"
      action.diff[diff_key] ||= [[], []]
      action.diff[diff_key][0] |= [id]
    else
      diff_key = "#{inverse_association.name.to_s.singularize}_id"
      action.diff[diff_key] ||= [id, nil]
    end
  end
  
  added.each do |added_id|
    action = activehistory_event.action_for(inverse_klass, added_id, {
      type: type,
      timestamp: timestamp
    })
  
    action.diff ||= {}
    if inverse_association.collection? || activehistory_tracking[:habtm_model]
      diff_key = "#{inverse_association.name.to_s.singularize}_ids"
      action.diff[diff_key] ||= [[], []]
      action.diff[diff_key][1] |= [id]
    else
      diff_key = "#{inverse_association.name.to_s.singularize}_id"
      action.diff[diff_key] ||= [nil, id]
    end
  end
end

#activehistory_eventObject



151
152
153
# File 'lib/activehistory/adapters/active_record.rb', line 151

def activehistory_event
  ActiveHistory.current_event(timestamp: activehistory_timestamp)
end

#activehistory_timestampObject



97
98
99
# File 'lib/activehistory/adapters/active_record.rb', line 97

def activehistory_timestamp
  @activehistory_timestamp ||= Time.now.utc
end

#activehistory_track(type) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/activehistory/adapters/active_record.rb', line 155

def activehistory_track(type)
  return if !activehistory_tracking
  
  if type == :create || type == :update
    diff = self.saved_changes.select { |k,v| !activehistory_tracking[:exclude].include?(k.to_sym) }

    if type == :create
      self.class.columns.each do |column|
        if !diff[column.name] && !activehistory_tracking[:exclude].include?(column.name.to_sym) && column.default != self.attributes[column.name]
          diff[column.name] = [nil, self.attributes[column.name]]
        end
      end
    end
  elsif type == :destroy
    relations_ids = self.class.reflect_on_all_associations.map { |r| "#{r.name.to_s.singularize}_ids" }

    diff = self.attributes.select do |k|
      !activehistory_tracking[:exclude].include?(k.to_sym) 
    end.map do |k, i|
      if relations_ids.include?(k)
        [ k, [ i, [] ] ]
      else
        [ k, [ i, nil ] ]
      end
    end.to_h
  end

  if type == :update
    diff_without_timestamps = if self.class.record_timestamps
      diff.keys - (self.class.send(:timestamp_attributes_for_update_in_model) + self.class.send(:timestamp_attributes_for_create_in_model))
    else
      diff.keys
    end
    
    return if diff_without_timestamps.empty?
  end

  if activehistory_tracking[:habtm_model]
    if type == :create
      self.class.reflect_on_association(:left_side).klass.activehistory_association_changed(
        self.send(activehistory_tracking[:habtm_model][:left_side][:foreign_key]),
        activehistory_tracking[:habtm_model][:left_side][:inverse_of],
        added: [self.send((self.class.column_names - [activehistory_tracking[:habtm_model][:left_side][:foreign_key]]).first)],
        timestamp: activehistory_timestamp
      )
    end
  else
    activehistory_event.action_for(self.class, id, {
      type: type,
      diff: diff,
      timestamp: activehistory_timestamp
    })
    
    
    self.class.reflect_on_all_associations.each do |reflection|
      next if activehistory_tracking[:habtm_model]
      
      if reflection.macro == :has_and_belongs_to_many && type == :destroy
        activehistory_association_changed(reflection, removed: self.send("#{reflection.name.to_s.singularize}_ids"))
      elsif reflection.macro == :belongs_to && diff.has_key?(reflection.foreign_key)
        case type
        when :create
          old_id = nil
          new_id = diff[reflection.foreign_key][1]
        when :destroy
          old_id = diff[reflection.foreign_key][0]
          new_id = nil
        else
          old_id = diff[reflection.foreign_key][0]
          new_id = diff[reflection.foreign_key][1]
        end
        
        relation_id = self.id || diff.find { |k, v| k != foreign_key }[1][1]
        
        if reflection.polymorphic?
        else
          activehistory_association_changed(reflection, removed: [old_id]) if old_id
          activehistory_association_changed(reflection, added:   [new_id]) if new_id
        end
        
      end
    end
  end

  
end

#activehistory_trackingObject



145
146
147
148
149
# File 'lib/activehistory/adapters/active_record.rb', line 145

def activehistory_tracking
  if ActiveHistory.configured? && self.class.instance_variable_defined?(:@activehistory)
    self.class.instance_variable_get(:@activehistory)
  end
end

#with_transaction_returning_statusObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
# File 'lib/activehistory/adapters/active_record.rb', line 101

def with_transaction_returning_status
  @activehistory_timestamp = Time.now.utc
  if !Thread.current[:activehistory_save_lock]
    run_save = true
    Thread.current[:activehistory_save_lock] = true
    if !Thread.current[:activehistory_event]
      destroy_current_event = true
      Thread.current[:activehistory_event] = ActiveHistory::Event.new(timestamp: @activehistory_timestamp)
    end
  end

  status = nil
  self.class.transaction do
    add_to_transaction
    begin
      status = yield
    rescue ::ActiveRecord::Rollback
      clear_transaction_record_state
      status = nil
    end

    if status
      if run_save && ActiveHistory.configured? && !activehistory_event.actions.empty?
        activehistory_event&.save!
      end
    else
      raise ::ActiveRecord::Rollback
    end
  end
  status
ensure
  @activehistory_timestamp = nil
  if run_save
    Thread.current[:activehistory_save_lock] = false
  end
  if destroy_current_event
    Thread.current[:activehistory_event] = nil
  end

  if @transaction_state && @transaction_state.committed?
    clear_transaction_record_state
  end
end