Module: Spree::FixtureLoadingExtension::ClassMethods

Defined in:
lib/plugins/extension_patches/lib/fixture_loading_extension.rb

Instance Method Summary collapse

Instance Method Details

#create_fixtures_with_multiple_paths(fixtures_directory, table_names, class_names = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/plugins/extension_patches/lib/fixture_loading_extension.rb', line 15

def create_fixtures_with_multiple_paths(fixtures_directory, table_names, class_names = {})
  table_names = [table_names].flatten.map { |n| n.to_s }
  connection = block_given? ? yield : ActiveRecord::Base.connection
  ActiveRecord::Base.silence do
    fixtures_map = {}
    fixtures = table_names.map do |table_name|
      paths = Array(fixtures_directory.dup)
      begin
        directory = paths.pop
        fixtures_map[table_name] = Fixtures.new(connection, File.split(table_name.to_s).last, class_names[table_name.to_sym], File.join(directory, table_name))
      rescue SystemCallError => e
        retry unless paths.empty?
        raise e
      end
    end
    all_loaded_fixtures.merge! fixtures_map

    # was connection.transaction(Thread.current['open_transactions'] == 0) do 
    # active_record (2.3.2) now looks at this quantity directly
    connection.transaction do
      fixtures.reverse.each { |fixture| fixture.delete_existing_fixtures }
      fixtures.each { |fixture| fixture.insert_fixtures }

      # Cap primary key sequences to max(pk).
      if connection.respond_to?(:reset_pk_sequence!)
        table_names.each do |table_name|
          connection.reset_pk_sequence!(table_name)
        end
      end
    end

    return fixtures.size > 1 ? fixtures : fixtures.first
  end
end