class Fixturex::TreeBuilder

Public Instance Methods

build_dependency_tree(fixture_path, fixture_name) click to toggle source
# File lib/fixturex/tree_builder.rb, line 58
def build_dependency_tree(fixture_path, fixture_name)
  TreeEntry.new(
    FixtureLocation.new(fixture_path, fixture_name),
    nested_fixtures_locations(FixtureModel.new(fixture_path).model_class, fixture_name)
  )
end

Private Instance Methods

associations_for_nested_models(model_class) click to toggle source
# File lib/fixturex/tree_builder.rb, line 88
def associations_for_nested_models(model_class)
  model_class.reflect_on_all_associations(:has_many) +
    model_class.reflect_on_all_associations(:has_one)
end
belongs_to_attribute_for_association(association) click to toggle source
# File lib/fixturex/tree_builder.rb, line 93
def belongs_to_attribute_for_association(association)
  if association.type.present?
    association.type.sub('_type', '')
  else
    association.active_record.name.tableize.singularize
  end
end
fixture_already_collected(list, fixture) click to toggle source
# File lib/fixturex/tree_builder.rb, line 82
def fixture_already_collected(list, fixture)
  list.find do |tree_entry|
    tree_entry.value.path == fixture.path && tree_entry.value.name == fixture.name
  end
end
nested_fixtures_locations(parent_fixture_model_class, parent_fixture_name) click to toggle source
# File lib/fixturex/tree_builder.rb, line 67
def nested_fixtures_locations(parent_fixture_model_class, parent_fixture_name)
  associations_for_nested_models(parent_fixture_model_class).each_with_object([]) do |association, acc|
    belongs_to_attribute = belongs_to_attribute_for_association(association)
    model_fixtures = ModelFixtures.load(association.class_name)

    model_fixtures.each do |fixture|
      next if fixture.attributes.fetch(belongs_to_attribute, '').to_s.sub(/ .*/, '') != parent_fixture_name

      next if fixture_already_collected(acc, fixture)

      acc << build_dependency_tree(fixture.path, fixture.name)
    end
  end
end