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

Fixture's table name be defined in the model

Made the fixture's table name be taken from its model class whenever this class responds to table_name, instead of inferring it sometimes from the fixture's pass.

The previous behavior seemed buggy because it depended on whether the model class was passed as a constant or as a name string.

Improved Fixtures#initialize.
This commit is contained in:
Alexey Muranov 2011-12-20 21:58:26 +01:00 committed by Alexey Muranov
parent d3e6e7e484
commit 7162ea2a0c

View file

@ -394,6 +394,11 @@ module ActiveRecord
fixture_name.camelize
end
def self.default_fixture_table_name(fixture_name) # :nodoc:
"#{ActiveRecord::Base.table_name_prefix}"\
"#{fixture_name.tr('/', '_')}#{ActiveRecord::Base.table_name_suffix}".to_sym
end
def self.reset_cache
@@all_cached_fixtures.clear
end
@ -454,12 +459,11 @@ module ActiveRecord
fixtures_map = {}
fixture_files = files_to_read.map do |path|
table_name = path.tr '/', '_'
fixture_name = path
fixtures_map[fixture_name] = new( # ActiveRecord::Fixtures.new
connection,
table_name,
fixture_name,
class_names[fixture_name] || default_fixture_model_name(fixture_name),
::File.join(fixtures_directory, path))
end
@ -504,25 +508,28 @@ module ActiveRecord
attr_reader :table_name, :name, :fixtures, :model_class
def initialize(connection, table_name, class_name, fixture_path)
def initialize(connection, fixture_name, class_name, fixture_path)
@connection = connection
@table_name = table_name
@fixture_path = fixture_path
@name = table_name # preserve fixture base name
@name = fixture_name.tr('/', '_') # preserve fixture base name
# TODO: see how it is used and if the substitution can be avoided
@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
@model_class = class_name
else
@model_class = class_name.constantize rescue nil
@model_class = class_name.constantize rescue nil
end
@connection = model_class.connection if model_class && model_class.respond_to?(:connection)
@table_name = ( model_class.respond_to?(:table_name) ?
model_class.table_name :
self.class.default_fixture_table_name(fixture_name) )
read_fixture_files
end