Permalink
Browse files

Add an option to model generator to generate classes with ActiveRecor…

…d::Model include instead of inherit from ActiveRecord::Base
  • Loading branch information...
1 parent 6696f7e commit 8ae55f02bed95fed372eba96a9f0bff10523b713 @guilleiguaran committed Dec 24, 2011
View
8 activerecord/lib/rails/generators/active_record/model/model_generator.rb
@@ -11,14 +11,20 @@ class ModelGenerator < Base
class_option :timestamps, :type => :boolean
class_option :parent, :type => :string, :desc => "The parent class for the generated model"
class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns"
+ class_option :with_include, :type => :boolean, :default => false,
+ :desc => "Include `ActiveRecord::Model` instead of inherit from `ActiveRecord::Base`"
def create_migration_file
return unless options[:migration] && options[:parent].nil?
migration_template "migration.rb", "db/migrate/create_#{table_name}.rb"
end
def create_model_file
- template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
+ if options[:with_include] && options[:parent].nil?
+ template 'model_with_include.rb', File.join('app/models', class_path, "#{file_name}.rb")
+ else
+ template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
+ end
end
def create_module_file
View
9 activerecord/lib/rails/generators/active_record/model/templates/model_with_include.rb
@@ -0,0 +1,9 @@
+<% module_namespacing do -%>
+class <%= class_name %>
+ include ActiveRecord::Model
+
+<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
+ belongs_to :<%= attribute.name %>
+<% end -%>
+end
+<% end -%>
View
5 railties/test/generators/model_generator_test.rb
@@ -34,6 +34,11 @@ def test_model_with_parent_option
assert_no_migration "db/migrate/create_accounts.rb"
end
+ def test_model_with_with_include_option
+ run_generator ["account", "--with-include"]
+ assert_file "app/models/account.rb", /class Account\n include ActiveRecord::Model/
+ end
+
def test_model_with_underscored_parent_option
run_generator ["account", "--parent", "admin/account"]
assert_file "app/models/account.rb", /class Account < Admin::Account/

0 comments on commit 8ae55f0

Please sign in to comment.