Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add an option to model generator to generate classes with ActiveRecord::Model #4174

Closed
wants to merge 1 commit into from

7 participants

@guilleiguaran

This add an option to model generator to generate classes that use ActiveRecord::Model. Ex:

rails g model Post --with-include

will generate:

class Post
  include ActiveRecord::Model
end

Please don't merge this inmediatly, I think we can choose a better option name than --with-include for this

@guilleiguaran guilleiguaran Add an option to model generator to generate classes with ActiveRecor…
…d::Model include instead of inherit from ActiveRecord::Base
8ae55f0
@jonleighton
Collaborator

Thanks for the patch.

However, I'm not sure about this:

  • Feels premature, I'd rather wait and see what the take-up on AR::Model is before adding extra 'stuff' for it
  • Not a huge gain - is not much to type out manually
  • Alternatively, we might consider adding an app-wide option in the future instead
  • Alternatively, we might just switch to generating with AR::Model by default in the future, when it is more proven/stable

But, I am biased because I never use generators (except for migrations).

cc @josevalim @tenderlove

Merry christmas! ho ho ho etc :)

@tenderlove
Owner

TBH, I'd like to see this bake in master for a while before we start adding generators. Right now, I think this is an "advanced users" feature, and I don't think advanced users need / user generators. But that's my 2cents.

@guilleiguaran

Agree, we can leave this open and revisit it on the next months

@isaacsanders

I :+1: this. Any thoughts on this @tenderlove / @wycats?

@josevalim
Owner

I still think the points given by @jonleighton and @tenderlove still apply. We will only be able to decide this after 4.0 is out.

@carlosantoniodasilva

Hey guys, do we keep this around opened for more time, or should we consider it closed?

@rafaelfranca

Lets just close it.

Closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 24, 2011
  1. @guilleiguaran

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

    guilleiguaran authored
    …d::Model include instead of inherit from ActiveRecord::Base
This page is out of date. Refresh to see the latest.
View
8 activerecord/lib/rails/generators/active_record/model/model_generator.rb
@@ -11,6 +11,8 @@ 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?
@@ -18,7 +20,11 @@ def create_migration_file
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/
Something went wrong with that request. Please try again.