diff options
Diffstat (limited to 'railties/lib/generators')
9 files changed, 92 insertions, 22 deletions
diff --git a/railties/lib/generators/active_record/migration/migration_generator.rb b/railties/lib/generators/active_record/migration/migration_generator.rb index baf9650790..95cc34ba42 100644 --- a/railties/lib/generators/active_record/migration/migration_generator.rb +++ b/railties/lib/generators/active_record/migration/migration_generator.rb @@ -3,7 +3,7 @@ require 'generators/active_record' module ActiveRecord module Generators class MigrationGenerator < Base - argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type" def create_migration_file set_local_assigns! diff --git a/railties/lib/generators/active_record/model/model_generator.rb b/railties/lib/generators/active_record/model/model_generator.rb index 32a2ed15e4..59310f6c61 100644 --- a/railties/lib/generators/active_record/model/model_generator.rb +++ b/railties/lib/generators/active_record/model/model_generator.rb @@ -3,7 +3,7 @@ require 'generators/active_record' module ActiveRecord module Generators class ModelGenerator < Base - argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type" check_class_collision diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index 386013c272..d1543e8d94 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -7,6 +7,7 @@ module Rails :helper => true, :migration => true, :orm => 'active_record', + :resource_controller => 'controller', :test_framework => 'test_unit', :template_engine => 'erb', :timestamps => true @@ -14,8 +15,8 @@ module Rails ALIASES = { :fixture_replacement => '-r', - :helper => '-l', :orm => '-o', + :resource_controller => '-c', :test_framework => '-t', :template_engine => '-e' } @@ -91,24 +92,19 @@ module Rails # ruby script/generate controller Account --no-test-framework # def self.hook_for(*names) - default_options = names.extract_options! - verbose = default_options.key?(:verbose) ? default_options[:verbose] : :blue - invocations.concat(names) + default_class_options(*names) + options = names.extract_options! + verbose = options.fetch(:verbose, :blue) names.each do |name| - options = default_options.dup - options[:desc] ||= "#{name.to_s.humanize} to be invoked" - options[:banner] ||= "NAME" - options[:aliases] ||= ALIASES[name] - - class_option name, options.merge!(:type => :default, :default => DEFAULTS[name]) + invocations << [ name, base_name, generator_name ] class_eval <<-METHOD, __FILE__, __LINE__ def invoke_for_#{name} return unless options[#{name.inspect}] klass = Rails::Generators.find_by_namespace(options[#{name.inspect}], - self.class.base_name, self.class.generator_name) + #{base_name.inspect}, #{generator_name.inspect}) if klass say_status :invoke, options[#{name.inspect}], #{verbose.inspect} @@ -146,18 +142,18 @@ module Rails # def self.invoke_if(*names) conditional_class_options(*names) - options = names.extract_options! verbose = options.fetch(:verbose, :blue) - invocations.concat(names) names.each do |name| + invocations << [ name, base_name, generator_name ] + class_eval <<-METHOD, __FILE__, __LINE__ def invoke_if_#{name} return unless options[#{name.inspect}] klass = Rails::Generators.find_by_namespace(#{name.inspect}, - self.class.base_name, self.class.generator_name) + #{base_name.inspect}, #{generator_name.inspect}) if klass say_status :invoke, #{name.inspect}, #{verbose.inspect} @@ -246,6 +242,21 @@ module Rails end end + # Creates a class option with type default, banner, alias lookup and + # description. Used internally by hook_for (ie, not part of plugin API). + # + def self.default_class_options(*names) #:nodoc: + default_options = names.extract_options! + + names.each do |name| + options = default_options.dup + options[:desc] ||= "#{name.to_s.humanize} to be invoked" + options[:banner] ||= "NAME" + options[:aliases] ||= ALIASES[name] + class_option name, options.merge!(:type => :default, :default => DEFAULTS[name]) + end + end + # Overwrite class options help to allow invoked generators options to be # shown when invoking a generator. Only first level options and options # that belongs to the default group are shown. @@ -253,13 +264,14 @@ module Rails def self.class_options_help(shell, ungrouped_name=nil, extra_group=nil) klass_options = Thor::CoreExt::OrderedHash.new - invocations.each do |name| + invocations.each do |args| + name, base, generator = args option = class_options[name] klass_name = option.type == :boolean ? name : option.default next unless klass_name - klass = Rails::Generators.find_by_namespace(klass_name, base_name, generator_name) + klass = Rails::Generators.find_by_namespace(klass_name, base, generator) next unless klass human_name = klass_name.to_s.classify 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 diff --git a/railties/lib/generators/test_unit/model/model_generator.rb b/railties/lib/generators/test_unit/model/model_generator.rb index 74440d93d3..96883f592f 100644 --- a/railties/lib/generators/test_unit/model/model_generator.rb +++ b/railties/lib/generators/test_unit/model/model_generator.rb @@ -3,7 +3,7 @@ require 'generators/test_unit' module TestUnit module Generators class ModelGenerator < Base - argument :attributes, :type => :hash, :default => {}, :banner => "field:type, field:type" + argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type" check_class_collision :suffix => "Test" conditional_class_options :fixture |