<commit_before>module Daylight::AssociationPersistance

  def self.prepended(base)
    base.before_save :include_child_updates
  end

  def load(attributes, remove_root = false, persisted = false)
    super(attributes, remove_root, persisted)

    @attribute_hash_on_load = self.attributes.hash

    self
  end

  # has our attributes changed since we were loaded?
  def changed?
    attributes.hash != @attribute_hash_on_load
  end

  protected

    # update the attributes for assocations if they have changed
    def include_child_updates
      self.class.reflection_names.each do |reflection_name|
        association = instance_variable_get("@#{reflection_name}")
        next unless association

        # recurse into the child(ren)
        attributes["#{reflection_name}_attributes"] =
          if Enumerable === association
            # currently we need to send ALL the children if any of them
            # have changed
            association.each {|child| child.include_child_updates }
            association.map(&:serializable_hash) if association.any?(&:changed?)
          else
            association.include_child_updates
            association.serializable_hash if association.changed?
          end
      end
    end

end<commit_msg>make sure we don't overwrite wanted changes in associations<commit_after>module Daylight::AssociationPersistance

  def self.prepended(base)
    base.before_save :include_child_updates
  end

  def load(attributes, remove_root = false, persisted = false)
    super(attributes, remove_root, persisted)

    @attribute_hash_on_load = self.attributes.hash

    self
  end

  # has our attributes changed since we were loaded?
  def changed?
    attributes.hash != @attribute_hash_on_load
  end

  protected

    # update the attributes for assocations if they have changed
    def include_child_updates
      self.class.reflection_names.each do |reflection_name|
        association = instance_variable_get("@#{reflection_name}")
        attribute_name = "#{reflection_name}_attributes"
        # ignore associations that have not been set
        next unless association
        # don't overwrite associations that already exist in the attributes hash
        next unless attributes[attribute_name].nil?

        # recurse into the child(ren)
        attributes[attribute_name] =
          if Enumerable === association
            # currently we need to send ALL the children if any of them
            # have changed
            association.each {|child| child.include_child_updates }
            association.map(&:serializable_hash) if association.any?(&:changed?)
          else
            association.include_child_updates
            association.serializable_hash if association.changed?
          end
      end
    end

end
{'fraction_non_alphanumeric': 0.06353194544149318, 'fraction_numerical': 0.0, 'mean_word_length': 2.942008486562942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2635397', 'n_tokens_mistral': 731, 'n_tokens_neox': 713, 'n_words': 209}