aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/fixtures.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-09-06 11:47:02 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-09-06 11:47:02 -0700
commita9da99ee5fd73b6d52c6fb443b2443cdfb262d5f (patch)
treeafcb194e8b52a16df8e9e5c7236dd4da19c27b99 /activerecord/lib/active_record/fixtures.rb
parentea55d86eb110842347351c2b15b4103ed62172fb (diff)
downloadrails-a9da99ee5fd73b6d52c6fb443b2443cdfb262d5f.tar.gz
rails-a9da99ee5fd73b6d52c6fb443b2443cdfb262d5f.tar.bz2
rails-a9da99ee5fd73b6d52c6fb443b2443cdfb262d5f.zip
use polymorphic proxies to remove duplicate code
Diffstat (limited to 'activerecord/lib/active_record/fixtures.rb')
-rw-r--r--activerecord/lib/active_record/fixtures.rb67
1 files changed, 43 insertions, 24 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index 0e96de518d..6780e309f2 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -625,10 +625,10 @@ module ActiveRecord
end
when :has_many
if association.options[:through]
- handle_hmt(rows, row, association)
+ add_join_records(rows, row, HasManyThroughProxy.new(association))
end
when :has_and_belongs_to_many
- handle_habtm(rows, row, association)
+ add_join_records(rows, row, HABTMProxy.new(association))
end
end
end
@@ -638,38 +638,57 @@ module ActiveRecord
rows
end
- private
- def primary_key_name
- @primary_key_name ||= model_class && model_class.primary_key
+ class ReflectionProxy # :nodoc:
+ def initialize(association)
+ @association = association
end
- def join_rows(targets, row, lhs_key, rhs_key)
- targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
- targets.map { |target|
- { lhs_key => row[primary_key_name],
- rhs_key => ActiveRecord::FixtureSet.identify(target) }
- }
+ def join_table
+ @association.join_table
end
- def handle_hmt(rows, row, association)
- # This is the case when the join table has no fixtures file
- if (targets = row.delete(association.name.to_s))
- table_name = association.join_table
- lhs_key = association.through_reflection.foreign_key
- rhs_key = association.foreign_key
+ def name
+ @association.name
+ end
+ end
- rows[table_name].concat join_rows(targets, row, lhs_key, rhs_key)
- end
+ class HasManyThroughProxy < ReflectionProxy # :nodoc:
+ def rhs_key
+ @association.foreign_key
+ end
+
+ def lhs_key
+ @association.through_reflection.foreign_key
end
+ end
+
+ class HABTMProxy < ReflectionProxy # :nodoc:
+ def rhs_key
+ @association.association_foreign_key
+ end
+
+ def lhs_key
+ @association.foreign_key
+ end
+ end
- def handle_habtm(rows, row, association)
+ private
+ def primary_key_name
+ @primary_key_name ||= model_class && model_class.primary_key
+ end
+
+ def add_join_records(rows, row, association)
# This is the case when the join table has no fixtures file
if (targets = row.delete(association.name.to_s))
table_name = association.join_table
- lhs_key = association.foreign_key
- rhs_key = association.association_foreign_key
-
- rows[table_name].concat join_rows(targets, row, lhs_key, rhs_key)
+ lhs_key = association.lhs_key
+ rhs_key = association.rhs_key
+
+ targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
+ rows[table_name].concat targets.map { |target|
+ { lhs_key => row[primary_key_name],
+ rhs_key => ActiveRecord::FixtureSet.identify(target) }
+ }
end
end