class Fixturex::ModelFixtures

Fixtures for model class

Constants

Fixture

Public Class Methods

fixtures_paths(class_name) click to toggle source
# File lib/fixturex/tree_builder.rb, line 25
def self.fixtures_paths(class_name)
  fixtures_paths = []
  klass = class_name.constantize
  # TODO: is there a better way to find out fixtures root directory?
  fixtures_root = ActiveRecord::Tasks::DatabaseTasks.fixtures_path

  while klass < ActiveRecord::Base
    fixture_file = "#{klass.to_s.tableize}.yml"
    path = File.join(fixtures_root, *fixture_file.split('/'))

    fixtures_paths << path if File.exist?(path)

    klass = klass.superclass
  end
  fixtures_paths
end
load(class_name) click to toggle source
# File lib/fixturex/tree_builder.rb, line 14
def self.load(class_name)
  fixtures_paths(class_name).each_with_object([]) do |path, acc|
    fixtures = YAML.load_file(path)
    fixtures.select! do |_name, attributes|
      # if fixture has `type` - STI - then we only want type == class_name
      attributes['type'].nil? || attributes['type'] == class_name
    end
    acc.concat(fixtures.map { |name, attributes| Fixture.new(name, path, attributes) })
  end
end