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

Changeset 7973

Show
Ignore:
Timestamp:
10/19/07 02:09:06 (7 months ago)
Author:
nzkoz
Message:

Add t.belongs_to and t.references to sexy migrations [arthurgeek]
Test harness for Sexy Migrations. [Koz]
Closes #9775

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    r7798 r7973  
    394394      # There's a short-hand method for each of the type values declared at the top. And then there's  
    395395      # TableDefinition#timestamps that'll add created_at and updated_at as datetimes. 
     396      # 
     397      # TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type 
     398      # column if the :polymorphic option is supplied. If :polymorphic is a hash of options, these will be 
     399      # used when creating the _type column. So what can be written like this: 
     400      # 
     401      #   create_table :taggings do |t| 
     402      #     t.integer :tag_id, :tagger_id, :taggable_id 
     403      #     t.string  :tagger_type 
     404      #     t.string  :taggable_type, :default => 'Photo' 
     405      #   end 
     406      # 
     407      # Can also be written as follows using references: 
     408      # 
     409      #   create_table :taggings do |t| 
     410      #     t.references :tag 
     411      #     t.references :tagger, :polymorphic => true 
     412      #     t.references :taggable, :polymorphic => { :default => 'Photo' } 
     413      #   end 
    396414      def column(name, type, options = {}) 
    397415        column = self[name] || ColumnDefinition.new(@base, name, type) 
     
    421439      end 
    422440 
     441      def references(*args) 
     442        options = args.extract_options! 
     443        polymorphic = options.delete(:polymorphic) 
     444        args.each do |col| 
     445          column("#{col}_id", :integer, options) 
     446          unless polymorphic.nil? 
     447            column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : {}) 
     448          end 
     449        end 
     450      end 
     451      alias :belongs_to :references 
     452 
    423453      # Returns a String whose contents are the column definitions 
    424454      # concatenated together.  This string can then be pre and appended to 
  • trunk/activerecord/test/migration_test.rb

    r7909 r7973  
    869869      end 
    870870    end 
    871  
    872871  end 
     872 
     873  uses_mocha 'Sexy migration tests' do 
     874    class SexyMigrationsTest < Test::Unit::TestCase 
     875      def test_references_column_type_adds_id 
     876        with_new_table do |t| 
     877          t.expects(:column).with('customer_id', :integer, {}) 
     878          t.references :customer 
     879        end 
     880      end 
     881  
     882      def test_references_column_type_with_polymarphic_adds_type 
     883        with_new_table do |t| 
     884          t.expects(:column).with('taggable_type', :string, {}) 
     885          t.expects(:column).with('taggable_id', :integer, {}) 
     886          t.references :taggable, :polymorphic => true 
     887        end 
     888      end 
     889       
     890      def test_belongs_to_works_like_references 
     891        with_new_table do |t| 
     892          t.expects(:column).with('customer_id', :integer, {}) 
     893          t.belongs_to :customer 
     894        end 
     895      end 
     896       
     897      protected 
     898      def with_new_table 
     899        Person.connection.create_table :delete_me do |t| 
     900          yield t 
     901        end 
     902      ensure 
     903        Person.connection.drop_table :delete_me rescue nil 
     904      end 
     905       
     906    end # SexyMigrationsTest 
     907  end # uses_mocha 
    873908end 
    874909