提交 0cae7c60 编写于 作者: A Aleksey Magusev

Add references statements to migration generator

AddXXXToYYY/RemoveXXXFromYYY migrations are produced with references
statements, for instance

    rails g migration AddReferencesToProducts user:references
supplier:references{polymorphic}

will generate the migration with:

    add_reference :products, :user, index: true
    add_reference :products, :supplier, polymorphic: true, index: true
上级 7404cda9
...@@ -2,30 +2,42 @@ class <%= migration_class_name %> < ActiveRecord::Migration ...@@ -2,30 +2,42 @@ class <%= migration_class_name %> < ActiveRecord::Migration
<%- if migration_action == 'add' -%> <%- if migration_action == 'add' -%>
def change def change
<% attributes.each do |attribute| -%> <% attributes.each do |attribute| -%>
<%- if attribute.reference? -%>
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
<%- else -%>
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
<%- if attribute.has_index? -%> <%- if attribute.has_index? -%>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
<%- end -%> <%- end -%>
<%- end -%>
<%- end -%> <%- end -%>
end end
<%- else -%> <%- else -%>
def up def up
<% attributes.each do |attribute| -%> <% attributes.each do |attribute| -%>
<%- if migration_action -%> <%- if migration_action -%>
<%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %> <%- if attribute.reference? -%>
remove_reference :<%= table_name %>, :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
<%- else -%>
remove_column :<%= table_name %>, :<%= attribute.name %>
<%- end -%> <%- end -%>
<%- end -%>
<%- end -%> <%- end -%>
end end
def down def down
<% attributes.reverse.each do |attribute| -%> <% attributes.reverse.each do |attribute| -%>
<%- if migration_action -%> <%- if migration_action -%>
<%- if attribute.reference? -%>
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
<%- else -%>
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
<%- if attribute.has_index? -%> <%- if attribute.has_index? -%>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
<%- end -%> <%- end -%>
<%- end -%> <%- end -%>
<%- end -%>
<%- end -%> <%- end -%>
end end
<%- end -%> <%- end -%>
end end
\ No newline at end of file
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##
* The migration generator will now produce AddXXXToYYY/RemoveXXXFromYYY migrations with references statements, for instance
rails g migration AddReferencesToProducts user:references supplier:references{polymorphic}
will generate the migration with:
add_reference :products, :user, index: true
add_reference :products, :supplier, polymorphic: true, index: true
*Aleksey Magusev*
* Allow scaffold/model/migration generators to accept a `polymorphic` modifier * Allow scaffold/model/migration generators to accept a `polymorphic` modifier
for `references`/`belongs_to`, for instance for `references`/`belongs_to`, for instance
......
...@@ -76,6 +76,23 @@ def test_remove_migration_with_attributes ...@@ -76,6 +76,23 @@ def test_remove_migration_with_attributes
end end
end end
def test_remove_migration_with_references_options
migration = "remove_references_from_books"
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :up, content do |up|
assert_match(/remove_reference :books, :author/, up)
assert_match(/remove_reference :books, :distributor, polymorphic: true/, up)
end
assert_method :down, content do |down|
assert_match(/add_reference :books, :author, index: true/, down)
assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, down)
end
end
end
def test_add_migration_with_attributes_and_indices def test_add_migration_with_attributes_and_indices
migration = "add_title_with_index_and_body_to_posts" migration = "add_title_with_index_and_body_to_posts"
run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"] run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"]
...@@ -138,6 +155,18 @@ def test_add_migration_with_attributes_index_declaration_and_attribute_options ...@@ -138,6 +155,18 @@ def test_add_migration_with_attributes_index_declaration_and_attribute_options
end end
end end
def test_add_migration_with_references_options
migration = "add_references_to_books"
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |up|
assert_match(/add_reference :books, :author, index: true/, up)
assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, up)
end
end
end
def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove
migration = "create_books" migration = "create_books"
run_generator [migration, "title:string", "content:text"] run_generator [migration, "title:string", "content:text"]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册