Browse files

Foxy fixtures: support single-table inheritance. Closes #10234.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8219 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 8b3f831 commit c7e39691899b080a451a4c94a3632cada5ab0750 @jeremy jeremy committed Nov 26, 2007
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Foxy fixtures: support single-table inheritance. #10234 [tom]
+
* Foxy fixtures: allow mixed usage to make migration easier and more attractive. #10004 [lotswholetime]
* Make the record_timestamps class-inheritable so it can be set per model. #10004 [tmacedo]
View
14 activerecord/lib/active_record/fixtures.rb
@@ -581,7 +581,15 @@ def insert_fixtures
row[primary_key_name] = Fixtures.identify(label)
end
- model_class.reflect_on_all_associations.each do |association|
+ # If STI is used, find the correct subclass for association reflection
+ reflection_class =
+ if row.include?(inheritance_column_name)
+ row[inheritance_column_name].constantize rescue model_class
+ else
+ model_class
+ end
+
+ reflection_class.reflect_on_all_associations.each do |association|
case association.macro
when :belongs_to
# Do not replace association name with association foreign key if they are named the same
@@ -650,6 +658,10 @@ def timestamp_column_names
end
end
+ def inheritance_column_name
+ @inheritance_column_name ||= model_class && model_class.inheritance_column
+ end
+
def column_names
@column_names ||= @connection.columns(@table_name).collect(&:name)
end
View
2 activerecord/test/fixtures/db_definitions/schema.rb
@@ -307,6 +307,8 @@ def create_table(*args, &block)
create_table :parrots, :force => true do |t|
t.column :name, :string
+ t.column :parrot_sti_class, :string
+ t.column :killer_id, :integer
t.column :created_at, :datetime
t.column :created_on, :datetime
t.column :updated_at, :datetime
View
8 activerecord/test/fixtures/parrot.rb
@@ -1,5 +1,13 @@
class Parrot < ActiveRecord::Base
+ set_inheritance_column :parrot_sti_class
has_and_belongs_to_many :pirates
has_and_belongs_to_many :treasures
has_many :loots, :as => :looter
end
+
+class LiveParrot < Parrot
+end
+
+class DeadParrot < Parrot
+ belongs_to :killer, :class_name => 'Pirate'
+end
View
6 activerecord/test/fixtures/parrots.yml
@@ -1,21 +1,27 @@
george:
name: "Curious George"
treasures: diamond, sapphire
+ parrot_sti_class: LiveParrot
louis:
name: "King Louis"
treasures: [diamond, sapphire]
+ parrot_sti_class: LiveParrot
frederick:
name: $LABEL
+ parrot_sti_class: LiveParrot
polly:
id: 4
name: $LABEL
+ killer: blackbeard
treasures: sapphire, ruby
+ parrot_sti_class: DeadParrot
DEFAULTS: &DEFAULTS
treasures: sapphire, ruby
+ parrot_sti_class: LiveParrot
davey:
<<: *DEFAULTS
View
7 activerecord/test/fixtures_test.rb
@@ -562,12 +562,17 @@ def test_supports_polymorphic_belongs_to
assert_equal(pirates(:redbeard), treasures(:sapphire).looter)
assert_equal(parrots(:louis), treasures(:ruby).looter)
end
-
+
def test_only_generates_a_pk_if_necessary
m = Matey.find(:first)
m.pirate = pirates(:blackbeard)
m.target = pirates(:redbeard)
end
+
+ def test_supports_sti
+ assert_kind_of DeadParrot, parrots(:polly)
+ assert_equal pirates(:blackbeard), parrots(:polly).killer
+ end
end
class ActiveSupportSubclassWithFixturesTest < ActiveSupport::TestCase

0 comments on commit c7e3969

Please sign in to comment.