提交 c35fb52c 编写于 作者: Y Yves Senn

Merge pull request #13440 from kuldeepaggarwal/pluralize_table_name_issue

Generating proper migration when ActiveRecord::Base.pluralize_table_names = false
* Fix Generation of proper migration when
ActiveRecord::Base.pluralize_table_names = false.
Previously, generation a migration like this:
rails g migration add_column_name_to_user name
would not generating the correct table name.
Fixes #13426.
*Kuldeep Aggarwal*
* `touch` accepts many attributes to be touched at once.
Example:
......
......@@ -23,16 +23,16 @@ def set_local_assigns!
case file_name
when /^(add|remove)_.*_(?:to|from)_(.*)/
@migration_action = $1
@table_name = $2.pluralize
@table_name = normalize_table_name($2)
when /join_table/
if attributes.length == 2
@migration_action = 'join'
@join_tables = attributes.map(&:plural_name)
@join_tables = pluralize_table_names? ? attributes.map(&:plural_name) : attributes.map(&:singular_name)
set_index_names
end
when /^create_(.+)/
@table_name = $1.pluralize
@table_name = normalize_table_name($1)
@migration_template = "create_table_migration.rb"
end
end
......@@ -61,6 +61,10 @@ def validate_file_name!
raise IllegalMigrationNameError.new(file_name)
end
end
def normalize_table_name(_table_name)
pluralize_table_names? ? _table_name.pluralize : _table_name.singularize
end
end
end
end
......@@ -94,6 +94,10 @@ def plural_name
name.sub(/_id$/, '').pluralize
end
def singular_name
name.sub(/_id$/, '').singularize
end
def human_name
name.humanize
end
......
......@@ -197,4 +197,54 @@ def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_
def test_properly_identifies_usage_file
assert generator_class.send(:usage_path)
end
def test_migration_with_singular_table_name
with_singular_table_name do
migration = "add_title_body_to_post"
run_generator [migration, 'title:string']
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :post, :title, :string/, change)
end
end
end
end
def test_create_join_table_migration_with_singular_table_name
with_singular_table_name do
migration = "add_media_join_table"
run_generator [migration, "artist_id", "music:uniq"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_join_table :artist, :music/, change)
assert_match(/# t.index \[:artist_id, :music_id\]/, change)
assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, change)
end
end
end
end
def test_create_table_migration_with_singular_table_name
with_singular_table_name do
run_generator ["create_book", "title:string", "content:text"]
assert_migration "db/migrate/create_book.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :book/, change)
assert_match(/ t\.string :title/, change)
assert_match(/ t\.text :content/, change)
end
end
end
end
private
def with_singular_table_name
old_state = ActiveRecord::Base.pluralize_table_names
ActiveRecord::Base.pluralize_table_names = false
yield
ensure
ActiveRecord::Base.pluralize_table_names = old_state
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册