Methods
#
C
E
F
I
N
R
S
T
Constants
MAX_ID = 2 ** 30 - 1
Attributes
[R] table_name
[R] name
[R] fixtures
[R] model_class
Class Public methods
cache_fixtures(connection, fixtures_map)
# File activerecord/lib/active_record/fixtures.rb, line 431
def self.cache_fixtures(connection, fixtures_map)
  cache_for_connection(connection).update(fixtures_map)
end
cache_for_connection(connection)
# File activerecord/lib/active_record/fixtures.rb, line 415
def self.cache_for_connection(connection)
  @@all_cached_fixtures[connection]
end
cached_fixtures(connection, keys_to_fetch = nil)
# File activerecord/lib/active_record/fixtures.rb, line 423
def self.cached_fixtures(connection, keys_to_fetch = nil)
  if keys_to_fetch
    cache_for_connection(connection).values_at(*keys_to_fetch)
  else
    cache_for_connection(connection).values
  end
end
create_fixtures(fixtures_directory, table_names, class_names = {})
# File activerecord/lib/active_record/fixtures.rb, line 456
def self.create_fixtures(fixtures_directory, table_names, class_names = {})
  table_names = [table_names].flatten.map { |n| n.to_s }
  table_names.each { |n|
    class_names[n.tr('/', '_').to_sym] = n.classify if n.include?('/')
  }

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

  files_to_read = table_names.reject { |table_name|
    fixture_is_cached?(connection, table_name)
  }

  unless files_to_read.empty?
    connection.disable_referential_integrity do
      fixtures_map = {}

      fixture_files = files_to_read.map do |path|
        table_name = path.tr '/', '_'

        fixtures_map[path] = ActiveRecord::Fixtures.new(
          connection,
          table_name,
          class_names[table_name.to_sym] || table_name.classify,
          File.join(fixtures_directory, path))
      end

      all_loaded_fixtures.update(fixtures_map)

      connection.transaction(:requires_new => true) do
        fixture_files.each do |ff|
          conn = ff.model_class.respond_to?(:connection) ? ff.model_class.connection : connection
          table_rows = ff.table_rows

          table_rows.keys.each do |table|
            conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete'
          end

          table_rows.each do |table_name,rows|
            rows.each do |row|
              conn.insert_fixture(row, table_name)
            end
          end
        end

        # 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.tr('/', '_'))
          end
        end
      end

      cache_fixtures(connection, fixtures_map)
    end
  end
  cached_fixtures(connection, table_names)
end
fixture_is_cached?(connection, table_name)
# File activerecord/lib/active_record/fixtures.rb, line 419
def self.fixture_is_cached?(connection, table_name)
  cache_for_connection(connection)[table_name]
end
identify(label)

Returns a consistent, platform-independent identifier for label. Identifiers are positive integers less than 2^32.

# File activerecord/lib/active_record/fixtures.rb, line 517
def self.identify(label)
  Zlib.crc32(label.to_s) % MAX_ID
end
instantiate_all_loaded_fixtures(object, load_instances = true)
# File activerecord/lib/active_record/fixtures.rb, line 447
def self.instantiate_all_loaded_fixtures(object, load_instances = true)
  all_loaded_fixtures.each do |table_name, fixtures|
    ActiveRecord::Fixtures.instantiate_fixtures(object, table_name, fixtures, load_instances)
  end
end
instantiate_fixtures(object, fixture_name, fixtures, load_instances = true)
# File activerecord/lib/active_record/fixtures.rb, line 435
def self.instantiate_fixtures(object, fixture_name, fixtures, load_instances = true)
  if load_instances
    fixtures.each do |name, fixture|
      begin
        object.instance_variable_set "@#{name}", fixture.find
      rescue FixtureClassNotFound
        nil
      end
    end
  end
end
new(connection, table_name, class_name, fixture_path)
# File activerecord/lib/active_record/fixtures.rb, line 523
def initialize(connection, table_name, class_name, fixture_path)
  @connection   = connection
  @table_name   = table_name
  @fixture_path = fixture_path
  @name         = table_name # preserve fixture base name
  @class_name   = class_name

  @fixtures     = ActiveSupport::OrderedHash.new
  @table_name   = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}"

  # Should be an AR::Base type class
  if class_name.is_a?(Class)
    @table_name   = class_name.table_name
    @connection   = class_name.connection
    @model_class  = class_name
  else
    @model_class  = class_name.constantize rescue nil
  end

  read_fixture_files
end
reset_cache()
# File activerecord/lib/active_record/fixtures.rb, line 411
def self.reset_cache
  @@all_cached_fixtures.clear
end
Instance Public methods
[](x)
# File activerecord/lib/active_record/fixtures.rb, line 545
def [](x)
  fixtures[x]
end
[]=(k,v)
# File activerecord/lib/active_record/fixtures.rb, line 549
def []=(k,v)
  fixtures[k] = v
end
each(&block)
# File activerecord/lib/active_record/fixtures.rb, line 553
def each(&block)
  fixtures.each(&block)
end
size()
# File activerecord/lib/active_record/fixtures.rb, line 557
def size
  fixtures.size
end
table_rows()

Return a hash of rows to be inserted. The key is the table, the value is a list of rows to insert to that table.

# File activerecord/lib/active_record/fixtures.rb, line 563
def table_rows
  now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
  now = now.to_s(:db)

  # allow a standard key to be used for doing defaults in YAML
  fixtures.delete('DEFAULTS')

  # track any join tables we need to insert later
  rows = Hash.new { |h,table| h[table] = [] }

  rows[table_name] = fixtures.map do |label, fixture|
    row = fixture.to_hash

    if model_class && model_class < ActiveRecord::Base
      # fill in timestamp columns if they aren't specified and the model is set to record_timestamps
      if model_class.record_timestamps
        timestamp_column_names.each do |name|
          row[name] = now unless row.key?(name)
        end
      end

      # interpolate the fixture label
      row.each do |key, value|
        row[key] = label if value == "$LABEL"
      end

      # generate a primary key if necessary
      if has_primary_key_column? && !row.include?(primary_key_name)
        row[primary_key_name] = ActiveRecord::Fixtures.identify(label)
      end

      # If STI is used, find the correct subclass for association reflection
      reflection_class =
        if row.include?(inheritance_column_name)
          row[inheritance_column_name].constantize rescue model_class
        else
          model_class
        end

      reflection_class.reflect_on_all_associations.each do |association|
        case association.macro
        when :belongs_to
          # Do not replace association name with association foreign key if they are named the same
          fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s

          if association.name.to_s != fk_name && value = row.delete(association.name.to_s)
            if association.options[:polymorphic] && value.sub!(/\s*\(([^\)]*)\)\s*$/, "")
              # support polymorphic belongs_to as "label (Type)"
              row[association.foreign_type] = $1
            end

            row[fk_name] = ActiveRecord::Fixtures.identify(value)
          end
        when :has_and_belongs_to_many
          if (targets = row.delete(association.name.to_s))
            targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
            table_name = association.options[:join_table]
            rows[table_name].concat targets.map { |target|
              { association.foreign_key             => row[primary_key_name],
                association.association_foreign_key => ActiveRecord::Fixtures.identify(target) }
            }
          end
        end
      end
    end

    row
  end
  rows
end