aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-10-19 02:09:06 +0000
committerMichael Koziarski <michael@koziarski.com>2007-10-19 02:09:06 +0000
commit6ddde027c4e51262e58f67438672d66a2b278f43 (patch)
tree8f118a3be42d0513ea3e8a456f213d109913c3ed /activerecord
parent210f7e29e2557f9519ecad801c564db7fc0ec0a5 (diff)
downloadrails-6ddde027c4e51262e58f67438672d66a2b278f43.tar.gz
rails-6ddde027c4e51262e58f67438672d66a2b278f43.tar.bz2
rails-6ddde027c4e51262e58f67438672d66a2b278f43.zip
Add t.belongs_to and t.references to sexy migrations [arthurgeek]
Test harness for Sexy Migrations. [Koz] Closes #9775 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7973 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb30
-rw-r--r--activerecord/test/migration_test.rb37
2 files changed, 66 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index efc7cec51f..f5a6880f56 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -393,6 +393,24 @@ module ActiveRecord
#
# There's a short-hand method for each of the type values declared at the top. And then there's
# TableDefinition#timestamps that'll add created_at and updated_at as datetimes.
+ #
+ # TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
+ # column if the :polymorphic option is supplied. If :polymorphic is a hash of options, these will be
+ # used when creating the _type column. So what can be written like this:
+ #
+ # create_table :taggings do |t|
+ # t.integer :tag_id, :tagger_id, :taggable_id
+ # t.string :tagger_type
+ # t.string :taggable_type, :default => 'Photo'
+ # end
+ #
+ # Can also be written as follows using references:
+ #
+ # create_table :taggings do |t|
+ # t.references :tag
+ # t.references :tagger, :polymorphic => true
+ # t.references :taggable, :polymorphic => { :default => 'Photo' }
+ # end
def column(name, type, options = {})
column = self[name] || ColumnDefinition.new(@base, name, type)
column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym]
@@ -420,6 +438,18 @@ module ActiveRecord
column(:updated_at, :datetime)
end
+ def references(*args)
+ options = args.extract_options!
+ polymorphic = options.delete(:polymorphic)
+ args.each do |col|
+ column("#{col}_id", :integer, options)
+ unless polymorphic.nil?
+ column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : {})
+ end
+ end
+ end
+ alias :belongs_to :references
+
# Returns a String whose contents are the column definitions
# concatenated together. This string can then be pre and appended to
# to generate the final SQL to create the table.
diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb
index 4e778af336..5898a290f5 100644
--- a/activerecord/test/migration_test.rb
+++ b/activerecord/test/migration_test.rb
@@ -868,7 +868,42 @@ if ActiveRecord::Base.connection.supports_migrations?
Person.connection.execute("select suitably_short_seq.nextval from dual")
end
end
-
end
+
+ uses_mocha 'Sexy migration tests' do
+ class SexyMigrationsTest < Test::Unit::TestCase
+ def test_references_column_type_adds_id
+ with_new_table do |t|
+ t.expects(:column).with('customer_id', :integer, {})
+ t.references :customer
+ end
+ end
+
+ def test_references_column_type_with_polymarphic_adds_type
+ with_new_table do |t|
+ t.expects(:column).with('taggable_type', :string, {})
+ t.expects(:column).with('taggable_id', :integer, {})
+ t.references :taggable, :polymorphic => true
+ end
+ end
+
+ def test_belongs_to_works_like_references
+ with_new_table do |t|
+ t.expects(:column).with('customer_id', :integer, {})
+ t.belongs_to :customer
+ end
+ end
+
+ protected
+ def with_new_table
+ Person.connection.create_table :delete_me do |t|
+ yield t
+ end
+ ensure
+ Person.connection.drop_table :delete_me rescue nil
+ end
+
+ end # SexyMigrationsTest
+ end # uses_mocha
end