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



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

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

  # Make subclasses/ancestors discoverable
  Rails.application.eager_load!

  superclasses = model_class.ancestors.select do |ancestor|
    ancestor < ActiveRecord::Base
  end

  (
    superclasses + [model_class] + model_class.subclasses
  ).filter_map do |klass|
    fixture_file = "#{klass.to_s.tableize}.yml"
    path = File.join(fixtures_root, *fixture_file.split('/'))

    path if File.exist?(path)
  end
end

.load(model_class) ⇒ Object



14
15
16
17
18
19
20
21
22
# 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|
      attributes['type'].nil? || attributes['type'].constantize <= model_class
    end
    acc.concat(fixtures.map { |name, attributes| Fixture.new(name, path, attributes) })
  end
end