From 9068bc05b8c5a1f2a0ad6959cb897d5831046d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 28 Jun 2009 18:28:30 +0200 Subject: Added ResourceGenerator. --- .../rails/controller/controller_generator.rb | 2 +- .../rails/migration/migration_generator.rb | 2 +- .../lib/generators/rails/model/model_generator.rb | 2 +- railties/lib/generators/rails/resource/USAGE | 23 ++++++++++++++ .../rails/resource/resource_generator.rb | 35 ++++++++++++++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 railties/lib/generators/rails/resource/USAGE create mode 100644 railties/lib/generators/rails/resource/resource_generator.rb (limited to 'railties/lib/generators/rails') diff --git a/railties/lib/generators/rails/controller/controller_generator.rb b/railties/lib/generators/rails/controller/controller_generator.rb index bc2a53fc7d..3fed058c00 100644 --- a/railties/lib/generators/rails/controller/controller_generator.rb +++ b/railties/lib/generators/rails/controller/controller_generator.rb @@ -9,7 +9,7 @@ module Rails end hook_for :template_engine, :test_framework - invoke_if :helper, :aliases => "-v" + invoke_if :helper end end end diff --git a/railties/lib/generators/rails/migration/migration_generator.rb b/railties/lib/generators/rails/migration/migration_generator.rb index 6aefddd3fd..cfaa5f0ad2 100644 --- a/railties/lib/generators/rails/migration/migration_generator.rb +++ b/railties/lib/generators/rails/migration/migration_generator.rb @@ -1,7 +1,7 @@ module Rails module Generators class MigrationGenerator < NamedBase - argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type" hook_for :orm end end diff --git a/railties/lib/generators/rails/model/model_generator.rb b/railties/lib/generators/rails/model/model_generator.rb index 58c274c4d3..36354c5638 100644 --- a/railties/lib/generators/rails/model/model_generator.rb +++ b/railties/lib/generators/rails/model/model_generator.rb @@ -1,7 +1,7 @@ module Rails module Generators class ModelGenerator < NamedBase - argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type" hook_for :orm, :test_framework end end diff --git a/railties/lib/generators/rails/resource/USAGE b/railties/lib/generators/rails/resource/USAGE new file mode 100644 index 0000000000..936619b0db --- /dev/null +++ b/railties/lib/generators/rails/resource/USAGE @@ -0,0 +1,23 @@ +Description: + Stubs out a new resource including an empty model and controller suitable + for a restful, resource-oriented application. Pass the singular model name, + either CamelCased or under_scored, as the first argument, and an optional + list of attribute pairs. + + Attribute pairs are field:type arguments specifying the + model's attributes. Timestamps are added by default, so you don't have to + specify them by hand as 'created_at:datetime updated_at:datetime'. + + You don't have to think up every attribute up front, but it helps to + sketch out a few so you can start working with the model immediately. + + This generator invokes your configured ORM and test framework, besides + creating helpers and add routes to config/routes.rb. + + Unlike the scaffold generator, the resource generator does not create + views or add any methods to the generated controller. + +Examples: + `./script/generate resource post` # no attributes + `./script/generate resource post title:string body:text published:boolean` + `./script/generate resource purchase order_id:integer amount:decimal` diff --git a/railties/lib/generators/rails/resource/resource_generator.rb b/railties/lib/generators/rails/resource/resource_generator.rb new file mode 100644 index 0000000000..5fdf8c5b1b --- /dev/null +++ b/railties/lib/generators/rails/resource/resource_generator.rb @@ -0,0 +1,35 @@ +require 'generators/rails/model/model_generator' + +module Rails + module Generators + class ResourceGenerator < ModelGenerator + hook_for :resource_controller + + class_option :actions, :type => :array, :default => [], :banner => "ACTION ACTION", + :desc => "Actions for the resource controller", :aliases => "-a" + + def invoke_for_resource_controller + return unless options[:resource_controller] + + klass = Rails::Generators.find_by_namespace(options[:resource_controller], :rails, :controller) + + if klass + args = [] + args << class_name.pluralize + args.concat(options[:actions]) + + say_status :invoke, options[:resource_controller], :blue + klass.new(args, options.dup, _overrides_config).invoke(:all) + else + say "Could not find and invoke '#{options[:resource_controller]}'." + end + end + + # TODO Add singleton support + def add_resource_routes + route "map.resources :#{file_name.pluralize}" + end + + end + end +end -- cgit v1.2.3