Permalink
Browse files

Fixed generating a namespaced model with table pluralization turned o…

…ff. Add tests for namespaced model generation.

[#767 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent a0caad5 commit 22b38c18c61a18babd671b973e804e054ba795d4 @hugopeixoto hugopeixoto committed with jeremy Aug 9, 2009
View
4 railties/lib/generators/active_record/model/model_generator.rb
@@ -13,7 +13,9 @@ class ModelGenerator < Base
def create_migration_file
if options[:migration] && options[:parent].nil?
- file_name = "create_#{file_path.gsub(/\//, '_').pluralize}"
+ klass_name = file_path.gsub(/\//, '_')
+ klass_name = klass_name.pluralize if ActiveRecord::Base.pluralize_table_names
+ file_name = "create_#{klass_name}"
migration_template "migration.rb", "db/migrate/#{file_name}.rb"
end
end
View
3 railties/lib/generators/named_base.rb
@@ -28,14 +28,15 @@ def assign_names!(given_name) #:nodoc:
else
singular_name
end
- @table_name.gsub! '/', '_'
if class_nesting.empty?
@class_name = class_name_without_nesting
else
@table_name = class_nesting.underscore << "_" << @table_name
@class_name = "#{class_nesting}::#{class_name_without_nesting}"
end
+
+ @table_name.gsub! '/', '_'
end
# Convert attributes hash into an array with GeneratedAttribute objects.
View
44 railties/test/generators/model_generator_test.rb
@@ -31,6 +31,50 @@ def test_migration
assert_migration "db/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration/
end
+ def test_migration_with_namespace
+ run_generator ["Gallery::Image"]
+ assert_migration "db/migrate/create_gallery_images", /class CreateGalleryImages < ActiveRecord::Migration/
+ assert_no_migration "db/migrate/create_images"
+ end
+
+ def test_migration_with_nested_namespace
+ run_generator ["Admin::Gallery::Image"]
+ assert_no_migration "db/migrate/create_images"
+ assert_no_migration "db/migrate/create_gallery_images"
+ assert_migration "db/migrate/create_admin_gallery_images", /class CreateAdminGalleryImages < ActiveRecord::Migration/
+ assert_migration "db/migrate/create_admin_gallery_images", /create_table :admin_gallery_images/
+ end
+
+ def test_migration_with_nested_namespace_without_pluralization
+ ActiveRecord::Base.pluralize_table_names = false
+ run_generator ["Admin::Gallery::Image"]
+ assert_no_migration "db/migrate/create_images"
+ assert_no_migration "db/migrate/create_gallery_images"
+ assert_no_migration "db/migrate/create_admin_gallery_images"
+ assert_migration "db/migrate/create_admin_gallery_image", /class CreateAdminGalleryImage < ActiveRecord::Migration/
+ assert_migration "db/migrate/create_admin_gallery_image", /create_table :admin_gallery_image/
+ ensure
+ ActiveRecord::Base.pluralize_table_names = true
+ end
+
+ def test_migration_with_namespaces_in_model_name_without_plurization
+ ActiveRecord::Base.pluralize_table_names = false
+ run_generator ["Gallery::Image"]
+ assert_migration "db/migrate/create_gallery_image", /class CreateGalleryImage < ActiveRecord::Migration/
+ assert_no_migration "db/migrate/create_gallery_images"
+ ensure
+ ActiveRecord::Base.pluralize_table_names = true
+ end
+
+ def test_migration_without_pluralization
+ ActiveRecord::Base.pluralize_table_names = false
+ run_generator
+ assert_migration "db/migrate/create_account", /class CreateAccount < ActiveRecord::Migration/
+ assert_no_migration "db/migrate/create_accounts"
+ ensure
+ ActiveRecord::Base.pluralize_table_names = true
+ end
+
def test_migration_is_skipped
run_generator ["account", "--no-migration"]
assert_no_migration "db/migrate/create_accounts.rb"

0 comments on commit 22b38c1

Please sign in to comment.