diff options
author | Michael Koziarski <michael@koziarski.com> | 2007-11-20 21:53:22 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2007-11-20 21:53:22 +0000 |
commit | c95f066bf5b5121ff86db090eff3436e63db9230 (patch) | |
tree | 582d35eeaff9f9a6d3bb3f8f3bc584defe40933d /activerecord/lib | |
parent | 0c12d6c6dc66cb7873969fc7f982ad9244a4ea00 (diff) | |
download | rails-c95f066bf5b5121ff86db090eff3436e63db9230.tar.gz rails-c95f066bf5b5121ff86db090eff3436e63db9230.tar.bz2 rails-c95f066bf5b5121ff86db090eff3436e63db9230.zip |
Add 'foxy' support for fixtures of polymorphic associations. Closes #10183 [jbarnette, David Lowenfels]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8170 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/fixtures.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 84e7c84d64..97fca6026b 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -301,6 +301,31 @@ end # a target *label* for the *association* (monkey: george) rather than # a target *id* for the *FK* (monkey_id: 1). # +# ==== Polymorphic belongs_to +# +# Supporting polymorphic relationships is a little bit more complicated, since +# ActiveRecord needs to know what type your association is pointing at. Something +# like this should look familiar: +# +# ### in fruit.rb +# +# belongs_to :eater, :polymorphic => true +# +# ### in fruits.yml +# +# apple: +# id: 1 +# name: apple +# eater_id: 1 +# eater_type: Monkey +# +# Can we do better? You bet! +# +# apple: +# eater: george (Monkey) +# +# Just provide the polymorphic target type and ActiveRecord will take care of the rest. +# # === has_and_belongs_to_many # # Time to give our monkey some fruit. @@ -556,6 +581,16 @@ class Fixtures < YAML::Omap case association.macro when :belongs_to if value = row.delete(association.name.to_s) + if association.options[:polymorphic] + if value.sub!(/\s*\(([^\)]*)\)\s*$/, "") + target_type = $1 + target_type_name = (association.options[:foreign_type] || "#{association.name}_type").to_s + + # support polymorphic belongs_to as "label (Type)" + row[target_type_name] = target_type + end + end + fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s row[fk_name] = Fixtures.identify(value) end |