Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/fixtures.rb

Overview

:nodoc:

Constant Summary collapse

@@already_loaded_fixtures =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fixtures(*table_names) ⇒ Object



474
475
476
477
478
479
# File 'lib/active_record/fixtures.rb', line 474

def self.fixtures(*table_names)
  table_names = table_names.flatten.map { |n| n.to_s }
  self.fixture_table_names |= table_names
  require_fixture_classes(table_names)
  setup_fixture_accessors(table_names)
end

.method_added(method) ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/active_record/fixtures.rb', line 569

def self.method_added(method)
  case method.to_s
  when 'setup'
    unless method_defined?(:setup_without_fixtures)
      alias_method :setup_without_fixtures, :setup
      define_method(:setup) do
        setup_with_fixtures
        setup_without_fixtures
      end
    end
  when 'teardown'
    unless method_defined?(:teardown_without_fixtures)
      alias_method :teardown_without_fixtures, :teardown
      define_method(:teardown) do
        teardown_without_fixtures
        teardown_with_fixtures
      end
    end
  end
end

.require_fixture_classes(table_names = nil) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
# File 'lib/active_record/fixtures.rb', line 481

def self.require_fixture_classes(table_names=nil)
  (table_names || fixture_table_names).each do |table_name| 
    file_name = table_name.to_s
    file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names
    begin
      require_dependency file_name
    rescue LoadError
      # Let's hope the developer has included it himself
    end
  end
end

.set_fixture_class(class_names = {}) ⇒ Object



470
471
472
# File 'lib/active_record/fixtures.rb', line 470

def self.set_fixture_class(class_names = {})
  self.fixture_class_names = self.fixture_class_names.merge(class_names)
end

.setup_fixture_accessors(table_names = nil) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/active_record/fixtures.rb', line 493

def self.setup_fixture_accessors(table_names=nil)
  (table_names || fixture_table_names).each do |table_name|
    table_name = table_name.to_s.tr('.','_')
    define_method(table_name) do |fixture, *optionals|
      force_reload = optionals.shift
      @fixture_cache[table_name] ||= Hash.new
      @fixture_cache[table_name][fixture] = nil if force_reload
      if @loaded_fixtures[table_name][fixture.to_s]
        @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find
      else
        raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
      end
    end
  end
end

.uses_transaction(*methods) ⇒ Object



509
510
511
512
# File 'lib/active_record/fixtures.rb', line 509

def self.uses_transaction(*methods)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.concat methods.map(&:to_s)
end

.uses_transaction?(method) ⇒ Boolean

Returns:

  • (Boolean)


514
515
516
517
# File 'lib/active_record/fixtures.rb', line 514

def self.uses_transaction?(method)
  @uses_transaction = [] unless defined?(@uses_transaction)
  @uses_transaction.include?(method.to_s)
end

Instance Method Details

#setup_with_fixturesObject Also known as: setup



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/active_record/fixtures.rb', line 524

def setup_with_fixtures
  return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?

  if pre_loaded_fixtures && !use_transactional_fixtures
    raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures' 
  end

  @fixture_cache = Hash.new

  # Load fixtures once and begin transaction.
  if use_transactional_fixtures?
    if @@already_loaded_fixtures[self.class]
      @loaded_fixtures = @@already_loaded_fixtures[self.class]
    else
      load_fixtures
      @@already_loaded_fixtures[self.class] = @loaded_fixtures
    end
    ActiveRecord::Base.send :increment_open_transactions
    ActiveRecord::Base.connection.begin_db_transaction

  # Load fixtures for every test.
  else
    @@already_loaded_fixtures[self.class] = nil
    load_fixtures
  end

  # Instantiate fixtures for every test if requested.
  instantiate_fixtures if use_instantiated_fixtures
end

#teardown_with_fixturesObject Also known as: teardown



556
557
558
559
560
561
562
563
564
565
# File 'lib/active_record/fixtures.rb', line 556

def teardown_with_fixtures
  return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?

  # Rollback changes if a transaction is active.
  if use_transactional_fixtures? && Thread.current['open_transactions'] != 0
    ActiveRecord::Base.connection.rollback_db_transaction
    Thread.current['open_transactions'] = 0
  end
  ActiveRecord::Base.verify_active_connections!
end

#use_transactional_fixtures?Boolean

Returns:

  • (Boolean)


519
520
521
522
# File 'lib/active_record/fixtures.rb', line 519

def use_transactional_fixtures?
  use_transactional_fixtures &&
    !self.class.uses_transaction?(method_name)
end