Sha256: 36bfb897eb95d21952e7f84c07827f92f83d22b946616e858be9a20c3bdf2608

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

module FixtureBackground
  module ActiveSupport
    module TestCase
      extend ::ActiveSupport::Concern
  
      included do
        class_inheritable_accessor :background_ivar_cache
        class_inheritable_accessor :active_record_fixture_cache_resetted

        set_callback(:setup, :before, :reset_active_record_fixture_cache, {:prepend => true})
        set_callback(:setup, :before, :setup_background_ivars)
      end

      module ClassMethods
        def parent_fixture_background
          nil
        end
        
        def fixture_background
          @fixture_background
        end
        
        def background(&blk)
          @fixture_background = FixtureBackground::Background.new(name, self, nil, blk) 
        end
      end
  
      module InstanceMethods
        def setup_background_ivars
          return unless File.exist?("#{fixture_path}/ivars.dump")

          fill_background_ivar_cache unless background_ivar_cache
          background_ivar_cache.each do |ivar, record|
            # deep clone the object
            instance_variable_set(ivar, Marshal.load(Marshal.dump(record)))
          end
        end
    
        def fill_background_ivar_cache
          bm = Benchmark.realtime do
            self.background_ivar_cache = YAML.load_file("#{fixture_path}/ivars.dump")
            background_ivar_cache.each do |ivar, (class_name, id)|
              record = class_name.constantize.find(id)
              self.background_ivar_cache[ivar] = record
            end
          end
        end
    
        def reset_active_record_fixture_cache
          return if active_record_fixture_cache_resetted
      
          Fixtures.class_variable_set(:@@all_cached_fixtures, {})
          self.active_record_fixture_cache_resetted = true
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fixture_background-0.9.0.2 lib/fixture_background/active_support/test_case.rb
fixture_background-0.9.0.1 lib/fixture_background/active_support/test_case.rb
fixture_background-0.9 lib/fixture_background/active_support/test_case.rb