提交 ca050167 编写于 作者: A Aaron Patterson

extract rows that should be inserted to a method

上级 2487aab9
......@@ -617,7 +617,9 @@ def tables
}.flatten.uniq
end
def insert_fixtures
# Return a hash of rows to be inserted. The key is the table, the value is
# a list of rows to insert to that table.
def rows
now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
now = now.to_s(:db)
......@@ -625,9 +627,9 @@ def insert_fixtures
fixtures.delete('DEFAULTS')
# track any join tables we need to insert later
habtm_fixtures = Hash.new { |h,k| h[k] = [] }
rows = Hash.new { |h,table| h[table] = [] }
rows = fixtures.map do |label, fixture|
rows[table_name] = fixtures.map do |label, fixture|
row = fixture.to_hash
if model_class && model_class < ActiveRecord::Base
......@@ -674,7 +676,7 @@ def insert_fixtures
if (targets = row.delete(association.name.to_s))
targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
table_name = association.options[:join_table]
habtm_fixtures[table_name].concat targets.map { |target|
rows[table_name].concat targets.map { |target|
{ association.foreign_key => row[primary_key_name],
association.association_foreign_key => Fixtures.identify(target) }
}
......@@ -685,15 +687,13 @@ def insert_fixtures
row
end
rows
end
rows.each do |row|
@connection.insert_fixture(row, table_name)
end
# insert any HABTM join tables we discovered
habtm_fixtures.each do |table, fixtures|
fixtures.each do |row|
@connection.insert_fixture(row, table)
def insert_fixtures
rows.each do |table_name, rows|
rows.each do |row|
@connection.insert_fixture(row, table_name)
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册