Class: Fixturex::ModelFixtures

Inherits:
Object
  • Object
show all
Defined in:
lib/fixturex/tree_builder.rb

Overview

Fixtures for model class

Defined Under Namespace

Classes: Fixture

Class Method Summary collapse

Class Method Details

.fixtures_paths(model_class) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fixturex/tree_builder.rb', line 25

def self.fixtures_paths(model_class)
  fixtures_paths = []
  # TODO: is there a better way to find out fixtures root directory?
  fixtures_root = ActiveRecord::Tasks::DatabaseTasks.fixtures_path

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

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

    model_class = model_class.superclass
  end
  fixtures_paths
end

.load(model_class) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/fixturex/tree_builder.rb', line 14

def self.load(model_class)
  fixtures_paths(model_class).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'] == model_class.name
    end
    acc.concat(fixtures.map { |name, attributes| Fixture.new(name, path, attributes) })
  end
end