From 5753a8cb2a63284aeb30d5b46e28301478cb779e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 1 Aug 2013 17:46:44 -0700 Subject: [PATCH] extract habtm handling to a method --- activerecord/lib/active_record/fixtures.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 47d4f3637f..20ca4e3c91 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -592,14 +592,7 @@ module ActiveRecord row[fk_name] = ActiveRecord::FixtureSet.identify(value) end when :has_and_belongs_to_many - if (targets = row.delete(association.name.to_s)) - targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/) - table_name = association.join_table - rows[table_name].concat targets.map { |target| - { association.foreign_key => row[primary_key_name], - association.association_foreign_key => ActiveRecord::FixtureSet.identify(target) } - } - end + handle_habtm(rows, row, association) end end end @@ -614,6 +607,17 @@ module ActiveRecord @primary_key_name ||= model_class && model_class.primary_key end + def handle_habtm(rows, row, association) + if (targets = row.delete(association.name.to_s)) + targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/) + table_name = association.join_table + rows[table_name].concat targets.map { |target| + { association.foreign_key => row[primary_key_name], + association.association_foreign_key => ActiveRecord::FixtureSet.identify(target) } + } + end + end + def has_primary_key_column? @has_primary_key_column ||= primary_key_name && model_class.columns.any? { |c| c.name == primary_key_name }