diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-07-18 00:16:39 -0700 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2012-07-18 00:16:39 -0700 |
commit | 58ccc9f6c5d261f7c1305d822b16447ac3d286b3 (patch) | |
tree | f47e5097d83b04464904fa56d0f65bc91b521fc8 /activerecord/lib/rails | |
parent | 17b6fa877752c217a9171b7f420bc86d55fddcbc (diff) | |
parent | 211d88b71b3df2ae161b23579a79f8e937132388 (diff) | |
download | rails-58ccc9f6c5d261f7c1305d822b16447ac3d286b3.tar.gz rails-58ccc9f6c5d261f7c1305d822b16447ac3d286b3.tar.bz2 rails-58ccc9f6c5d261f7c1305d822b16447ac3d286b3.zip |
Merge pull request #7028 from lexmag/join_table_indexes
Add indexes to create_join_table method
Diffstat (limited to 'activerecord/lib/rails')
-rw-r--r-- | activerecord/lib/rails/generators/active_record/migration/migration_generator.rb | 23 | ||||
-rw-r--r-- | activerecord/lib/rails/generators/active_record/migration/templates/migration.rb | 10 |
2 files changed, 27 insertions, 6 deletions
diff --git a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb index 1509e34473..c1f9d8962b 100644 --- a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb +++ b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb @@ -11,15 +11,28 @@ module ActiveRecord end protected - attr_reader :migration_action + attr_reader :migration_action, :join_tables - def set_local_assigns! - if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/ - @migration_action = $1 - @table_name = $2.pluralize + def set_local_assigns! + case file_name + when /^(add|remove)_.*_(?:to|from)_(.*)/ + @migration_action = $1 + @table_name = $2.pluralize + when /join_table/ + if attributes.length == 2 + @migration_action = 'join' + @join_tables = attributes.map(&:name) + + set_index_names end end + end + def set_index_names + attributes.each_with_index do |attr, i| + attr.index_name = [attr, attributes[i - 1]].map{ |a| :"#{a.name.singularize}_id"} + end + end end end end diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb index 34eaffcb0f..d5c07aecd3 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -12,6 +12,14 @@ class <%= migration_class_name %> < ActiveRecord::Migration <%- end -%> <%- end -%> end +<%- elsif migration_action == 'join' -%> + def change + create_join_table :<%= join_tables.first %>, :<%= join_tables.second %> do |t| + <%- attributes.each do |attribute| -%> + <%= '# ' unless attribute.has_index? -%>t.index <%= attribute.index_name %><%= attribute.inject_index_options %> + <%- end -%> + end + end <%- else -%> def up <% attributes.each do |attribute| -%> @@ -40,4 +48,4 @@ class <%= migration_class_name %> < ActiveRecord::Migration <%- end -%> end <%- end -%> -end
\ No newline at end of file +end |