Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8219

Show
Ignore:
Timestamp:
11/26/07 22:46:11 (1 year ago)
Author:
bitsweat
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r8218 r8219  
    11*SVN* 
     2 
     3* Foxy fixtures: support single-table inheritance.  #10234 [tom] 
    24 
    35* Foxy fixtures: allow mixed usage to make migration easier and more attractive.  #10004 [lotswholetime] 
  • trunk/activerecord/lib/active_record/fixtures.rb

    r8218 r8219  
    582582        end 
    583583 
    584         model_class.reflect_on_all_associations.each do |association| 
     584        # If STI is used, find the correct subclass for association reflection 
     585        reflection_class = 
     586          if row.include?(inheritance_column_name) 
     587            row[inheritance_column_name].constantize rescue model_class 
     588          else 
     589            model_class 
     590          end 
     591 
     592        reflection_class.reflect_on_all_associations.each do |association| 
    585593          case association.macro 
    586594          when :belongs_to 
     
    649657        column_names.include?(name) 
    650658      end 
     659    end 
     660 
     661    def inheritance_column_name 
     662      @inheritance_column_name ||= model_class && model_class.inheritance_column 
    651663    end 
    652664 
  • trunk/activerecord/test/fixtures_test.rb

    r8218 r8219  
    563563    assert_equal(parrots(:louis), treasures(:ruby).looter) 
    564564  end 
    565    
     565 
    566566  def test_only_generates_a_pk_if_necessary 
    567567    m = Matey.find(:first) 
     
    569569    m.target = pirates(:redbeard) 
    570570  end 
     571 
     572  def test_supports_sti 
     573    assert_kind_of DeadParrot, parrots(:polly) 
     574    assert_equal pirates(:blackbeard), parrots(:polly).killer 
     575  end 
    571576end 
    572577 
  • trunk/activerecord/test/fixtures/db_definitions/schema.rb

    r8218 r8219  
    308308  create_table :parrots, :force => true do |t| 
    309309    t.column :name, :string 
     310    t.column :parrot_sti_class, :string 
     311    t.column :killer_id, :integer 
    310312    t.column :created_at, :datetime 
    311313    t.column :created_on, :datetime 
  • trunk/activerecord/test/fixtures/parrot.rb

    r8170 r8219  
    11class Parrot < ActiveRecord::Base 
     2  set_inheritance_column :parrot_sti_class 
    23  has_and_belongs_to_many :pirates 
    34  has_and_belongs_to_many :treasures 
    45  has_many :loots, :as => :looter 
    56end 
     7 
     8class LiveParrot < Parrot 
     9end 
     10 
     11class DeadParrot < Parrot 
     12  belongs_to :killer, :class_name => 'Pirate' 
     13end 
  • trunk/activerecord/test/fixtures/parrots.yml

    r8218 r8219  
    22  name: "Curious George" 
    33  treasures: diamond, sapphire 
     4  parrot_sti_class: LiveParrot 
    45 
    56louis: 
    67  name: "King Louis" 
    78  treasures: [diamond, sapphire] 
     9  parrot_sti_class: LiveParrot 
    810 
    911frederick: 
    1012  name: $LABEL 
     13  parrot_sti_class: LiveParrot 
    1114 
    1215polly: 
    1316  id: 4 
    1417  name: $LABEL 
     18  killer: blackbeard 
    1519  treasures: sapphire, ruby 
     20  parrot_sti_class: DeadParrot 
    1621 
    1722DEFAULTS: &DEFAULTS 
    1823  treasures: sapphire, ruby 
     24  parrot_sti_class: LiveParrot 
    1925 
    2026davey: