module DirtyNestedAttributes::ChangesMarker

Marks association record changes with ActiveRecord::Dirty behavior

Public Instance Methods

assign_nested_attributes_for_collection_association(*args) click to toggle source

catch assignment for collection association and mark changes

Calls superclass method
# File lib/dirty_nested_attributes/changes_marker.rb, line 7
def assign_nested_attributes_for_collection_association(*args)
  super
  mark_associations_changes
end
assign_nested_attributes_for_one_to_one_association(*args) click to toggle source

catch assignment for one to one association and mark changes

Calls superclass method
# File lib/dirty_nested_attributes/changes_marker.rb, line 13
def assign_nested_attributes_for_one_to_one_association(*args)
  super
  mark_associations_changes
end

Private Instance Methods

mark_associations_changes() click to toggle source
# File lib/dirty_nested_attributes/changes_marker.rb, line 20
def mark_associations_changes
  self.class.reflect_on_all_autosave_associations.each do |reflection|
    association = association(reflection.name)
    if reflection.collection?
      mark_associations_changes_for_collection_association(association)
    else
      mark_associations_changes_for_single_association(association)
    end
  end
end
mark_associations_changes_for_collection_association(association) click to toggle source
# File lib/dirty_nested_attributes/changes_marker.rb, line 31
def mark_associations_changes_for_collection_association(association)
  records_that_will_change = association.target.select { |r| record_new_or_changed_or_marked_for_destruction?(r) }
  return if records_that_will_change.size == 0
  @changed_attributes[association.reflection.name] = records_that_will_change
end
mark_associations_changes_for_single_association(association) click to toggle source
# File lib/dirty_nested_attributes/changes_marker.rb, line 37
def mark_associations_changes_for_single_association(association)
  return if association.target.blank? || !record_new_or_changed_or_marked_for_destruction?(association.target)
  @changed_attributes[association.reflection.name] = association.target
end
record_new_or_changed_or_marked_for_destruction?(record) click to toggle source
# File lib/dirty_nested_attributes/changes_marker.rb, line 42
def record_new_or_changed_or_marked_for_destruction?(record)
  record.new_record? || record.changed? || record.marked_for_destruction?
end