From 8c5622f0d4d3b5c6bce52b0eb22756a83060b1b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 27 Jun 2009 13:03:07 +0200 Subject: Initial scratch of model generators. --- .../active_record/model/model_generator.rb | 33 ++++++++++++++++++++++ .../active_record/model/templates/migration.rb | 16 +++++++++++ .../active_record/model/templates/model.rb | 5 ++++ 3 files changed, 54 insertions(+) create mode 100644 railties/lib/generators/active_record/model/model_generator.rb create mode 100644 railties/lib/generators/active_record/model/templates/migration.rb create mode 100644 railties/lib/generators/active_record/model/templates/model.rb (limited to 'railties/lib/generators/active_record/model') diff --git a/railties/lib/generators/active_record/model/model_generator.rb b/railties/lib/generators/active_record/model/model_generator.rb new file mode 100644 index 0000000000..448bf5c37f --- /dev/null +++ b/railties/lib/generators/active_record/model/model_generator.rb @@ -0,0 +1,33 @@ +module ActiveRecord + module Generators + class ModelGenerator < Base + argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + + check_class_collision + + # TODO Add parent support + + # TODO Add DEFAULTS support + class_option :skip_timestamps, :type => :boolean, :default => false, + :desc => "Don't add timestamps to the migration file" + + # TODO Make this a invoke_if + # TODO Add DEFAULTS support + class_option :skip_migration, :type => :boolean, :default => false, + :desc => "Don't generate a migration file" + + def create_model_file + template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb") + end + + # TODO Add migration support + def create_migration_file +# unless options[:skip_migration] +# 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 + end +end diff --git a/railties/lib/generators/active_record/model/templates/migration.rb b/railties/lib/generators/active_record/model/templates/migration.rb new file mode 100644 index 0000000000..382fd1156e --- /dev/null +++ b/railties/lib/generators/active_record/model/templates/migration.rb @@ -0,0 +1,16 @@ +class <%= migration_name %> < ActiveRecord::Migration + def self.up + create_table :<%= table_name %> do |t| +<% for attribute in attributes -%> + t.<%= attribute.type %> :<%= attribute.name %> +<% end -%> +<% unless options[:skip_timestamps] %> + t.timestamps +<% end -%> + end + end + + def self.down + drop_table :<%= table_name %> + end +end diff --git a/railties/lib/generators/active_record/model/templates/model.rb b/railties/lib/generators/active_record/model/templates/model.rb new file mode 100644 index 0000000000..0656b06dfe --- /dev/null +++ b/railties/lib/generators/active_record/model/templates/model.rb @@ -0,0 +1,5 @@ +class <%= class_name %> < ActiveRecord::Base +<% attributes.select {|attr| attr.reference? }.each do |attribute| -%> + belongs_to :<%= attribute.name %> +<% end -%> +end -- cgit v1.2.3