module ActiveRecord::TrackDirtyAssociations

Public Class Methods

included(base) click to toggle source
# File lib/activerecord/track_dirty_associations.rb, line 3
def self.included(base)
  base.extend ClassMethods
  attr_accessor :dirty_assocs
end

Public Instance Methods

association_changes(association_type) click to toggle source
# File lib/activerecord/track_dirty_associations.rb, line 101
def association_changes association_type
  association_changes = dirty_assocs&.dig(association_type) || {}
  changes = []

  assocs = send association_type
  assocs = [assocs] unless assocs.is_a? Enumerable
  assocs.each do |assoc|
    assoc_changes = {}
    assoc_changes.merge!(assoc.changes) if assoc.respond_to?(:changes)
    assoc_changes.merge!(assoc.dirty_associations) if assoc.respond_to?(:dirty_associations)
    changes << assoc_changes if assoc_changes.any?
  end

  association_changes[:changes] = changes if changes.any?
  association_changes
end