aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/rails/generators/active_record/model
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/rails/generators/active_record/model')
-rw-r--r--activerecord/lib/rails/generators/active_record/model/model_generator.rb33
-rw-r--r--activerecord/lib/rails/generators/active_record/model/templates/migration.rb16
-rw-r--r--activerecord/lib/rails/generators/active_record/model/templates/model.rb5
3 files changed, 54 insertions, 0 deletions
diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb
new file mode 100644
index 0000000000..3e72fbeca8
--- /dev/null
+++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb
@@ -0,0 +1,33 @@
+require 'rails/generators/active_record'
+
+module ActiveRecord
+ module Generators
+ class ModelGenerator < Base
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
+ check_class_collision
+
+ class_option :migration, :type => :boolean
+ class_option :timestamps, :type => :boolean
+ class_option :parent, :type => :string, :desc => "The parent class for the generated model"
+
+ 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")
+ end
+
+ hook_for :test_framework
+
+ protected
+
+ def parent_class_name
+ options[:parent] || "ActiveRecord::Base"
+ end
+
+ end
+ end
+end
diff --git a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb
new file mode 100644
index 0000000000..1f68487304
--- /dev/null
+++ b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb
@@ -0,0 +1,16 @@
+class <%= migration_class_name %> < ActiveRecord::Migration
+ def self.up
+ create_table :<%= table_name %> do |t|
+<% for attribute in attributes -%>
+ t.<%= attribute.type %> :<%= attribute.name %>
+<% end -%>
+<% if options[:timestamps] %>
+ t.timestamps
+<% end -%>
+ end
+ end
+
+ def self.down
+ drop_table :<%= table_name %>
+ end
+end
diff --git a/activerecord/lib/rails/generators/active_record/model/templates/model.rb b/activerecord/lib/rails/generators/active_record/model/templates/model.rb
new file mode 100644
index 0000000000..21ae29e9f2
--- /dev/null
+++ b/activerecord/lib/rails/generators/active_record/model/templates/model.rb
@@ -0,0 +1,5 @@
+class <%= class_name %> < <%= parent_class_name.classify %>
+<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
+ belongs_to :<%= attribute.name %>
+<% end -%>
+end