Module: ActiveRecord::TrackDirtyAssociations

Included in:
Base
Defined in:
lib/activerecord/track_dirty_associations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
# File 'lib/activerecord/track_dirty_associations.rb', line 3

def self.included(base)
  base.extend ClassMethods
  attr_accessor :dirty_assocs
end

Instance Method Details

#association_changes(association_type) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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