Ravings on CS, OSs, PLs, SF, and other things geeky...

Figuring out how Ruby on Rails loads up its fixtures was a royal pain in the ass. Instead of having a nice method like.

load_fixtures(path)

there’s only this in the ActiveRecord.TestFixtures module:

def fixtures(*table_names)
  if table_names.first == :all
    table_names = Dir["#{fixture_path}/*.yml"] + Dir["#{fixture_path}/*.csv"]
    table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') }
  else
    table_names = table_names.flatten.map { |n| n.to_s }
  end

  self.fixture_table_names |= table_names
  require_fixture_classes(table_names)
  setup_fixture_accessors(table_names)
end

So after digging around to see what did what I came up with a function to do the trick:

def self.load_fixture_from_path(local_fixture_path)
  table_names = Dir["#{local_fixture_path}/*.yml"] + Dir["#{local_fixture_path}/*.csv"]
  table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') }

  ActiveSupport::TestCase.fixture_table_names |= table_names
  ActiveSupport::TestCase.require_fixture_classes(table_names)
  ActiveSupport::TestCase.setup_fixture_accessors(table_names)
  Fixtures.create_fixtures(local_fixture_path, table_names, {})
end