method

create_fixtures

Importance_0
create_fixtures(fixtures_directory, fixture_set_names, class_names = {}, config = ActiveRecord::Base) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/fixtures.rb, line 531
    def self.create_fixtures(fixtures_directory, fixture_set_names, class_names = {}, config = ActiveRecord::Base)
      fixture_set_names = Array(fixture_set_names).map(&:to_s)
      class_names = ClassCache.new class_names, config

      # FIXME: Apparently JK uses this.
      connection = block_given? ? yield : ActiveRecord::Base.connection

      files_to_read = fixture_set_names.reject { |fs_name|
        fixture_is_cached?(connection, fs_name)
      }

      unless files_to_read.empty?
        fixtures_map = {}

        fixture_sets = files_to_read.map do |fs_name|
          klass = class_names[fs_name]
          conn = klass ? klass.connection : connection
          fixtures_map[fs_name] = new( # ActiveRecord::FixtureSet.new
            conn,
            fs_name,
            klass,
            ::File.join(fixtures_directory, fs_name))
        end

        update_all_loaded_fixtures fixtures_map
        fixture_sets_by_connection = fixture_sets.group_by { |fs| fs.model_class ? fs.model_class.connection : connection }

        fixture_sets_by_connection.each do |conn, set|
          table_rows_for_connection = Hash.new { |h, k| h[k] = [] }

          set.each do |fs|
            fs.table_rows.each do |table, rows|
              table_rows_for_connection[table].unshift(*rows)
            end
          end
          conn.insert_fixtures_set(table_rows_for_connection, table_rows_for_connection.keys)

          # Cap primary key sequences to max(pk).
          if conn.respond_to?(:reset_pk_sequence!)
            set.each { |fs| conn.reset_pk_sequence!(fs.table_name) }
          end
        end

        cache_fixtures(connection, fixtures_map)
      end
      cached_fixtures(connection, fixture_set_names)
    end
Register or log in to add new notes.