1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

refactor fixtures to do less work in the constructor

This commit is contained in:
Aaron Patterson 2011-02-11 14:33:03 -08:00
parent 83dd6e1e62
commit fb09d02542
2 changed files with 24 additions and 11 deletions

View file

@ -450,6 +450,12 @@ class Fixtures
@@all_cached_fixtures = {} @@all_cached_fixtures = {}
def self.find_table_name(table_name) # :nodoc:
ActiveRecord::Base.pluralize_table_names ?
table_name.to_s.singularize.camelize :
table_name.to_s.camelize
end
def self.reset_cache(connection = nil) def self.reset_cache(connection = nil)
connection ||= ActiveRecord::Base.connection connection ||= ActiveRecord::Base.connection
@@all_cached_fixtures[connection.object_id] = {} @@all_cached_fixtures[connection.object_id] = {}
@ -547,14 +553,16 @@ class Fixtures
attr_reader :table_name, :name, :fixtures attr_reader :table_name, :name, :fixtures
def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE) def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
@fixtures = ActiveSupport::OrderedHash.new @fixtures = ActiveSupport::OrderedHash.new
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter @connection = connection
@name = table_name # preserve fixture base name @table_name = table_name
@class_name = class_name || @fixture_path = fixture_path
(ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize) @file_filter = file_filter
@table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}" @name = table_name # preserve fixture base name
@table_name = class_name.table_name if class_name.respond_to?(:table_name) @class_name = class_name
@connection = class_name.connection if class_name.respond_to?(:connection) @table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}"
@table_name = class_name.table_name if class_name.respond_to?(:table_name)
@connection = class_name.connection if class_name.respond_to?(:connection)
read_fixture_files read_fixture_files
end end
@ -587,7 +595,10 @@ class Fixtures
# track any join tables we need to insert later # track any join tables we need to insert later
habtm_fixtures = Hash.new do |h, habtm| habtm_fixtures = Hash.new do |h, habtm|
h[habtm] = HabtmFixtures.new(@connection, habtm.options[:join_table], nil, nil) h[habtm] = HabtmFixtures.new(
@connection,
habtm.options[:join_table],
Fixtures.find_table_name(habtm.options[:join_table]), nil)
end end
fixtures.each do |label, fixture| fixtures.each do |label, fixture|
@ -843,7 +854,9 @@ module ActiveRecord
self.use_instantiated_fixtures = false self.use_instantiated_fixtures = false
self.pre_loaded_fixtures = false self.pre_loaded_fixtures = false
self.fixture_class_names = {} self.fixture_class_names = Hash.new do |h, table_name|
h[table_name] = Fixtures.find_table_name(table_name)
end
end end
module ClassMethods module ClassMethods

View file

@ -94,7 +94,7 @@ class ActiveSupport::TestCase
self.use_transactional_fixtures = true self.use_transactional_fixtures = true
def create_fixtures(*table_names, &block) def create_fixtures(*table_names, &block)
Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block) Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, fixture_class_names, &block)
end end
end end