Class: Test::Unit::TestCase
- Inherits:
-
Object
- Object
- Test::Unit::TestCase
- Defined in:
- lib/active_record/fixtures.rb
Overview
:nodoc:
Constant Summary collapse
- @@already_loaded_fixtures =
{}
Class Method Summary collapse
- .fixtures(*table_names) ⇒ Object
- .method_added(method) ⇒ Object
- .require_fixture_classes(table_names = nil) ⇒ Object
- .set_fixture_class(class_names = {}) ⇒ Object
- .setup_fixture_accessors(table_names = nil) ⇒ Object
- .uses_transaction(*methods) ⇒ Object
- .uses_transaction?(method) ⇒ Boolean
Instance Method Summary collapse
- #setup_with_fixtures ⇒ Object (also: #setup)
- #teardown_with_fixtures ⇒ Object (also: #teardown)
- #use_transactional_fixtures? ⇒ Boolean
Class Method Details
.fixtures(*table_names) ⇒ Object
451 452 453 454 455 456 |
# File 'lib/active_record/fixtures.rb', line 451 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
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/active_record/fixtures.rb', line 541 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
458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/active_record/fixtures.rb', line 458 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 file_name rescue LoadError # Let's hope the developer has included it himself end end end |
.set_fixture_class(class_names = {}) ⇒ Object
447 448 449 |
# File 'lib/active_record/fixtures.rb', line 447 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
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/active_record/fixtures.rb', line 470 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
486 487 488 489 |
# File 'lib/active_record/fixtures.rb', line 486 def self.uses_transaction(*methods) @uses_transaction ||= [] @uses_transaction.concat methods.map { |m| m.to_s } end |
.uses_transaction?(method) ⇒ Boolean
491 492 493 |
# File 'lib/active_record/fixtures.rb', line 491 def self.uses_transaction?(method) @uses_transaction && @uses_transaction.include?(method.to_s) end |
Instance Method Details
#setup_with_fixtures ⇒ Object Also known as: setup
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/active_record/fixtures.rb', line 500 def setup_with_fixtures 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.lock_mutex 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_fixtures ⇒ Object Also known as: teardown
530 531 532 533 534 535 536 537 |
# File 'lib/active_record/fixtures.rb', line 530 def teardown_with_fixtures # Rollback changes. if use_transactional_fixtures? ActiveRecord::Base.connection.rollback_db_transaction ActiveRecord::Base.unlock_mutex end ActiveRecord::Base.verify_active_connections! end |
#use_transactional_fixtures? ⇒ Boolean
495 496 497 498 |
# File 'lib/active_record/fixtures.rb', line 495 def use_transactional_fixtures? use_transactional_fixtures && !self.class.uses_transaction?(method_name) end |