module TestProf::LetItBe::Freezer

Public Class Methods

deep_freeze(record) click to toggle source

Rerucsively freezes the object to detect modifications

# File lib/test_prof/recipes/rspec/let_it_be.rb, line 157
def deep_freeze(record)
  return if record.frozen?
  return if Stoplist.stop?(record)

  record.freeze

  # Support `let_it_be` with `create_list`
  return record.each { |rec| deep_freeze(rec) } if record.respond_to?(:each)

  # Freeze associations as well.
  return unless defined?(::ActiveRecord::Base)
  return unless record.is_a?(::ActiveRecord::Base)

  record.class.reflections.keys.each do |reflection|
    # But only if they are already loaded. If not yet loaded, they weren't
    # created by factories, and it's ok to mutate them.

    next unless record.association(reflection.to_sym).loaded?

    target = record.association(reflection.to_sym).target
    deep_freeze(target) if target.is_a?(::ActiveRecord::Base) || target.respond_to?(:each)
  end
end