From 211d88b71b3df2ae161b23579a79f8e937132388 Mon Sep 17 00:00:00 2001 From: Aleksey Magusev Date: Wed, 11 Jul 2012 01:38:21 +0400 Subject: Add join table migration generator For instance, running rails g migration CreateMediaJoinTable artists musics:uniq will create a migration with create_join_table :artists, :musics do |t| # t.index [:artist_id, :music_id] t.index [:music_id, :artist_id], unique: true end --- railties/lib/rails/generators/generated_attribute.rb | 3 ++- railties/test/generators/migration_generator_test.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'railties') diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb index d480fc12b5..35cd40d25e 100644 --- a/railties/lib/rails/generators/generated_attribute.rb +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -8,6 +8,7 @@ module Rails attr_accessor :name, :type attr_reader :attr_options + attr_writer :index_name class << self def parse(column_definition) @@ -94,7 +95,7 @@ module Rails end def index_name - if reference? + @index_name ||= if reference? polymorphic? ? %w(id type).map { |t| "#{name}_#{t}" } : "#{name}_id" else name diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 86e3793289..9f9cd9c9fc 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -167,6 +167,19 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end + def test_create_join_table_migration + migration = "add_media_join_table" + run_generator [migration, "artists", "musics:uniq"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |up| + assert_match(/create_join_table :artists, :musics/, up) + assert_match(/# t.index \[:artist_id, :music_id\]/, up) + assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, up) + end + end + end + def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove migration = "create_books" run_generator [migration, "title:string", "content:text"] -- cgit v1.2.3