1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #13440 from kuldeepaggarwal/pluralize_table_name_issue

Generating proper migration when ActiveRecord::Base.pluralize_table_names = false
This commit is contained in:
Yves Senn 2014-03-25 16:03:51 +01:00
commit c35fb52c0c
4 changed files with 74 additions and 3 deletions

View file

@ -1,3 +1,16 @@
* 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. * `touch` accepts many attributes to be touched at once.
Example: Example:

View file

@ -23,16 +23,16 @@ module ActiveRecord
case file_name case file_name
when /^(add|remove)_.*_(?:to|from)_(.*)/ when /^(add|remove)_.*_(?:to|from)_(.*)/
@migration_action = $1 @migration_action = $1
@table_name = $2.pluralize @table_name = normalize_table_name($2)
when /join_table/ when /join_table/
if attributes.length == 2 if attributes.length == 2
@migration_action = 'join' @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 set_index_names
end end
when /^create_(.+)/ when /^create_(.+)/
@table_name = $1.pluralize @table_name = normalize_table_name($1)
@migration_template = "create_table_migration.rb" @migration_template = "create_table_migration.rb"
end end
end end
@ -61,6 +61,10 @@ module ActiveRecord
raise IllegalMigrationNameError.new(file_name) raise IllegalMigrationNameError.new(file_name)
end end
end end
def normalize_table_name(_table_name)
pluralize_table_names? ? _table_name.pluralize : _table_name.singularize
end
end end
end end
end end

View file

@ -94,6 +94,10 @@ module Rails
name.sub(/_id$/, '').pluralize name.sub(/_id$/, '').pluralize
end end
def singular_name
name.sub(/_id$/, '').singularize
end
def human_name def human_name
name.humanize name.humanize
end end

View file

@ -197,4 +197,54 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
def test_properly_identifies_usage_file def test_properly_identifies_usage_file
assert generator_class.send(:usage_path) assert generator_class.send(:usage_path)
end 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 end