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

Generate module file for namespaced models [#4230 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Andrew White 2010-04-11 09:27:45 +01:00 committed by José Valim
parent b9b9f9ee15
commit 788d923893
4 changed files with 33 additions and 0 deletions

View file

@ -20,6 +20,11 @@ module ActiveRecord
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
end
def create_module_file
return if class_path.empty?
template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb")
end
hook_for :test_framework
protected

View file

@ -0,0 +1,5 @@
module <%= class_path.map(&:camelize).join('::') %>
def self.table_name_prefix
'<%= class_path.join('_') %>_'
end
end

View file

@ -15,6 +15,10 @@ Description:
Finally, if --parent option is given, it's used as superclass of the
created model. This allows you create Single Table Inheritance models.
If you pass a namespaced model name (e.g. admin/account or Admin::Account)
then the generator will create a module with a table_name_prefix method
to prefix the model's table name with the module name (e.g. admin_account)
Examples:
`rails generate model account`
@ -28,3 +32,14 @@ Examples:
`rails generate model post title:string body:text published:boolean`
Creates a Post model with a string title, text body, and published flag.
`rails generate model admin/account`
For ActiveRecord and TestUnit it creates:
Module: app/models/admin.rb
Model: app/models/admin/account.rb
Test: test/unit/admin/account_test.rb
Fixtures: test/fixtures/admin_accounts.yml
Migration: db/migrate/XXX_add_admin_accounts.rb

View file

@ -27,6 +27,14 @@ class ModelGeneratorTest < Rails::Generators::TestCase
assert_file "app/models/account.rb", /class Account < Admin::Account/
end
def test_model_with_namespace
run_generator ["admin/account"]
assert_file "app/models/admin.rb", /module Admin/
assert_file "app/models/admin.rb", /def self\.table_name_prefix/
assert_file "app/models/admin.rb", /'admin_'/
assert_file "app/models/admin/account.rb", /class Admin::Account < ActiveRecord::Base/
end
def test_migration
run_generator
assert_migration "db/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration/