提交 7162ea2a 编写于 作者: A Alexey Muranov 提交者: Alexey Muranov

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.
上级 d3e6e7e4
......@@ -394,6 +394,11 @@ def self.default_fixture_model_name(fixture_name) # :nodoc:
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 @@ def self.create_fixtures(fixtures_directory, table_names, class_names = {})
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 @@ def self.identify(label)
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册