Sha256: be1d10027691f64955f81570e72d7257bd33e5e546502c43312b0d55ce70fc48

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module FrozenRecord
  module TestHelper
    NoFixturesLoaded = Class.new(StandardError)

    class << self
      def load_fixture(model_class, alternate_base_path)
        @cache ||= {}

        ensure_model_class_is_frozenrecord(model_class)

        return if @cache.key?(model_class)

        @cache[model_class] = {
          old_base_path: model_class.base_path,
          old_auto_reloading: model_class.auto_reloading,
        }

        model_class.auto_reloading = true
        model_class.base_path = alternate_base_path
      end

      def unload_fixtures
        @cache.each do |model_class, cached_values|
          model_class.base_path = cached_values[:old_base_path]
          model_class.load_records
          model_class.auto_reloading = cached_values[:old_auto_reloading]
        end

        @cache = nil
      end

      private

      def ensure_model_class_is_frozenrecord(model_class)
        return if model_class < FrozenRecord::Base
        raise ArgumentError, "Model class (#{model_class}) does not inherit from #{FrozenRecord::Base}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
frozen_record-0.12.0 lib/frozen_record/test_helper.rb