From c16a4379caaa9e086aeeb58a9194e511c40921d9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 9 Oct 2006 00:08:13 +0000 Subject: Added script/generate resource which works just like scaffold_resource, but creates empty placeholders instead of predefined [DHH] Added generated attribute options to script/generate model, like the one found in scaffold_resource and resource [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5236 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../generators/components/model/USAGE | 31 +++++++++++++--------- .../components/model/templates/fixtures.yml | 10 +++++-- .../components/model/templates/migration.rb | 4 ++- 3 files changed, 30 insertions(+), 15 deletions(-) (limited to 'railties/lib/rails_generator/generators/components/model') diff --git a/railties/lib/rails_generator/generators/components/model/USAGE b/railties/lib/rails_generator/generators/components/model/USAGE index 57156ea535..d9efa7a000 100644 --- a/railties/lib/rails_generator/generators/components/model/USAGE +++ b/railties/lib/rails_generator/generators/components/model/USAGE @@ -1,19 +1,26 @@ Description: The model generator creates stubs for a new model. - The generator takes a model name as its argument. The model name may be - given in CamelCase or under_score and should not be suffixed with 'Model'. + The generator takes a model name as its argument. The model name may be given in CamelCase or under_score and + should not be suffixed with 'Model'. - The generator creates a model class in app/models, a test suite in - test/unit, test fixtures in test/fixtures/singular_name.yml, and a migration - in db/migrate. + As additional parameters, the generator will take attribute pairs described by name and type. These attributes will + be used to prepopulate the migration to create the table for the model and give you a set of predefined fixture. + You don't have to think up all attributes up front, but it's a good idea of adding just the baseline of what's + needed to start really working with the resource. -Example: - ./script/generate model Account + The generator creates a model class in app/models, a test suite in test/unit, test fixtures in + test/fixtures/singular_name.yml, and a migration in db/migrate. - This will create an Account model: - Model: app/models/account.rb - Test: test/unit/account_test.rb - Fixtures: test/fixtures/accounts.yml - Migration: db/migrate/XXX_add_accounts.rb +Examples: + ./script/generate model account + This will create an Account model: + Model: app/models/account.rb + Test: test/unit/account_test.rb + Fixtures: test/fixtures/accounts.yml + Migration: db/migrate/XXX_add_accounts.rb + + ./script/generate model post title:string created_on:date body:text published:boolean + + Creates post model with predefined attributes. diff --git a/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml index 8794d28ae4..9f5ae29a81 100644 --- a/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml +++ b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml @@ -1,5 +1,11 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -first: +one: id: 1 -another: +<% for attribute in attributes -%> + <%= attribute.name %>: <%= attribute.default %> +<% end -%> +two: id: 2 +<% for attribute in attributes -%> + <%= attribute.name %>: <%= attribute.default %> +<% end -%> \ No newline at end of file diff --git a/railties/lib/rails_generator/generators/components/model/templates/migration.rb b/railties/lib/rails_generator/generators/components/model/templates/migration.rb index a6954ebd85..2a305a6a9d 100644 --- a/railties/lib/rails_generator/generators/components/model/templates/migration.rb +++ b/railties/lib/rails_generator/generators/components/model/templates/migration.rb @@ -1,7 +1,9 @@ class <%= migration_name %> < ActiveRecord::Migration def self.up create_table :<%= table_name %> do |t| - # t.column :name, :string +<% for attribute in attributes -%> + t.column :<%= attribute.name %>, :<%= attribute.type %> +<% end -%> end end -- cgit v1.2.3