Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…d::Model include instead of inherit from ActiveRecord::Base
  • Loading branch information...
commit 8ae55f02bed95fed372eba96a9f0bff10523b713 1 parent 6696f7e
Guillermo Iguaran authored
8 activerecord/lib/rails/generators/active_record/model/model_generator.rb
@@ -11,6 +11,8 @@ class ModelGenerator < Base
11 11 class_option :timestamps, :type => :boolean
12 12 class_option :parent, :type => :string, :desc => "The parent class for the generated model"
13 13 class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns"
  14 + class_option :with_include, :type => :boolean, :default => false,
  15 + :desc => "Include `ActiveRecord::Model` instead of inherit from `ActiveRecord::Base`"
14 16
15 17 def create_migration_file
16 18 return unless options[:migration] && options[:parent].nil?
@@ -18,7 +20,11 @@ def create_migration_file
18 20 end
19 21
20 22 def create_model_file
21   - template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
  23 + if options[:with_include] && options[:parent].nil?
  24 + template 'model_with_include.rb', File.join('app/models', class_path, "#{file_name}.rb")
  25 + else
  26 + template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
  27 + end
22 28 end
23 29
24 30 def create_module_file
9 activerecord/lib/rails/generators/active_record/model/templates/model_with_include.rb
... ... @@ -0,0 +1,9 @@
  1 +<% module_namespacing do -%>
  2 +class <%= class_name %>
  3 + include ActiveRecord::Model
  4 +
  5 +<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
  6 + belongs_to :<%= attribute.name %>
  7 +<% end -%>
  8 +end
  9 +<% end -%>
5 railties/test/generators/model_generator_test.rb
@@ -34,6 +34,11 @@ def test_model_with_parent_option
34 34 assert_no_migration "db/migrate/create_accounts.rb"
35 35 end
36 36
  37 + def test_model_with_with_include_option
  38 + run_generator ["account", "--with-include"]
  39 + assert_file "app/models/account.rb", /class Account\n include ActiveRecord::Model/
  40 + end
  41 +
37 42 def test_model_with_underscored_parent_option
38 43 run_generator ["account", "--parent", "admin/account"]
39 44 assert_file "app/models/account.rb", /class Account < Admin::Account/

0 comments on commit 8ae55f0

Please sign in to comment.
Something went wrong with that request. Please try again.