aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/fixtures.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-02-14 12:01:22 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-02-14 12:01:22 -0800
commitca0501676500471981806993e7ccd7536f7943f0 (patch)
tree156003cde8ab2012f8646e663f68f17a0df52674 /activerecord/lib/active_record/fixtures.rb
parent2487aab99a0c1767502c9f635102607fb4ded05d (diff)
downloadrails-ca0501676500471981806993e7ccd7536f7943f0.tar.gz
rails-ca0501676500471981806993e7ccd7536f7943f0.tar.bz2
rails-ca0501676500471981806993e7ccd7536f7943f0.zip
extract rows that should be inserted to a method
Diffstat (limited to 'activerecord/lib/active_record/fixtures.rb')
-rw-r--r--activerecord/lib/active_record/fixtures.rb24
1 files changed, 12 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index 154c2d022c..75c5daaaae 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -617,7 +617,9 @@ class Fixtures
}.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 @@ class 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 @@ class 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 @@ class 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