diff --git a/railties/lib/generators/active_record/model/model_generator.rb b/railties/lib/generators/active_record/model/model_generator.rb index 45e773fde1..53592c222f 100644 --- a/railties/lib/generators/active_record/model/model_generator.rb +++ b/railties/lib/generators/active_record/model/model_generator.rb @@ -8,7 +8,7 @@ module ActiveRecord conditional_class_option :timestamps conditional_class_option :migration - class_option :parent, :type => :string, :default => "ActiveRecord::Base", + class_option :parent, :type => :string, :desc => "The parent class for the generated model" def create_model_file @@ -17,13 +17,19 @@ module ActiveRecord # TODO Add migration support def create_migration_file -# unless options[:skip_migration] + if options[:migration] && options[:parent].nil? # m.migration_template 'migration.rb', 'db/migrate', :assigns => { # :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}" # }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" -# end + end end + protected + + def parent_class_name + options[:parent] || "ActiveRecord::Base" + end + end end end diff --git a/railties/lib/generators/active_record/model/templates/model.rb b/railties/lib/generators/active_record/model/templates/model.rb index fb337d10e6..21ae29e9f2 100644 --- a/railties/lib/generators/active_record/model/templates/model.rb +++ b/railties/lib/generators/active_record/model/templates/model.rb @@ -1,4 +1,4 @@ -class <%= class_name %> < <%= options[:parent] %> +class <%= class_name %> < <%= parent_class_name.classify %> <% attributes.select {|attr| attr.reference? }.each do |attribute| -%> belongs_to :<%= attribute.name %> <% end -%> diff --git a/railties/lib/generators/rails/model/USAGE b/railties/lib/generators/rails/model/USAGE index 73c8b69153..b056d5df8b 100644 --- a/railties/lib/generators/rails/model/USAGE +++ b/railties/lib/generators/rails/model/USAGE @@ -12,6 +12,9 @@ Description: This generator invokes your configured ORM and test framework, which defaults to ActiveRecord and TestUnit. + Finally, if --parent option is given, it's used as superclass of the + created model. This allows you create Single Table Inheritance models. + Examples: `./script/generate model account` diff --git a/railties/lib/generators/rails/model_subclass/model_subclass_generator.rb b/railties/lib/generators/rails/model_subclass/model_subclass_generator.rb new file mode 100644 index 0000000000..4649709780 --- /dev/null +++ b/railties/lib/generators/rails/model_subclass/model_subclass_generator.rb @@ -0,0 +1,11 @@ +module Rails + module Generators + class ModelSubclassGenerator < Base + desc "model_subclass is deprecated. Invoke model with --parent option instead." + + def say_deprecation_warn + say self.class.desc + end + end + end +end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 55c08d0199..62ad1d9305 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -16,6 +16,11 @@ class ModelGeneratorTest < GeneratorsTestCase assert_file "app/models/account.rb", /class Account < Admin::Account/ end + def test_orm_with_underscored_parent_option + run_generator ["account", "--parent", "admin/account"] + assert_file "app/models/account.rb", /class Account < Admin::Account/ + end + def test_invokes_default_test_framework run_generator assert_file "test/unit/account_test.rb", /class AccountTest < ActiveSupport::TestCase/