diff options
Diffstat (limited to 'railties')
7 files changed, 34 insertions, 47 deletions
diff --git a/railties/lib/generators/active_record/model/model_generator.rb b/railties/lib/generators/active_record/model/model_generator.rb index 42ff48c75c..149ce281f3 100644 --- a/railties/lib/generators/active_record/model/model_generator.rb +++ b/railties/lib/generators/active_record/model/model_generator.rb @@ -7,10 +7,9 @@ module ActiveRecord check_class_collision - conditional_class_options :migration, :timestamps - - class_option :parent, :type => :string, - :desc => "The parent class for the generated model" + 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_model_file template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb") diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index 47c93636b4..519d8b84a8 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -3,18 +3,22 @@ require 'generators/actions' module Rails module Generators DEFAULTS = { + :actions => [], :fixture => true, + :force_plural => false, :helper => true, :migration => true, :orm => 'active_record', :resource_controller => 'controller', :scaffold_controller => 'scaffold_controller', + :singleton => false, :test_framework => 'test_unit', :template_engine => 'erb', :timestamps => true } ALIASES = { + :actions => '-a', :fixture_replacement => '-r', :orm => '-o', :resource_controller => '-c', @@ -109,12 +113,14 @@ module Rails # end # def self.hook_for(*names, &block) - default_class_options(*names) options = names.extract_options! as = options.fetch(:as, generator_name) verbose = options.fetch(:verbose, :blue) names.each do |name| + default = { :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" } + class_option name, default.merge!(options) + invocations << [ name, base_name, as ] invocation_blocks[name] = block if block_given? @@ -165,12 +171,13 @@ module Rails # for usage and examples. # def self.invoke_if(*names, &block) - conditional_class_options(*names) - options = names.extract_options! + options = names.extract_options!.merge(:type => :boolean) as = options.fetch(:as, generator_name) verbose = options.fetch(:verbose, :blue) names.each do |name| + class_option name, options + invocations << [ name, base_name, as ] invocation_blocks[name] = block if block_given? @@ -207,6 +214,15 @@ module Rails end end + # Make class option aware of DEFAULTS and ALIASES. + # + def self.class_option(name, options) #:nodoc: + options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc) + options[:aliases] = ALIASES[name] unless options.key?(:aliases) + options[:default] = DEFAULTS[name] unless options.key?(:default) + super(name, options) + end + protected # This is the common method that both hook_for and invoke_if use to @@ -288,34 +304,6 @@ module Rails @invocation_blocks ||= from_superclass(:invocation_blocks, {}) end - # Creates a conditional class option with type boolean, default value - # lookup and default description. - # - def self.conditional_class_options(*names) - default_options = names.extract_options! - - names.each do |name| - options = default_options.dup - options[:desc] ||= "Indicates when to generate #{name.to_s.humanize.downcase}" - class_option name, options.merge!(:type => :boolean, :default => DEFAULTS[name] || false) - 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 and second level options # are shown, for instance, if a generator invokes an ORM that invokes @@ -338,7 +326,7 @@ module Rails # in base_options are not added twice. # def self.get_options_from_invocations(group_options, base_options) - invocations.sort{ |a,b| a[0].to_s <=> b[0].to_s }.each do |args| + invocations.each do |args| name, base, generator = args option = class_options[name] @@ -353,7 +341,7 @@ module Rails group_options[human_name] += klass.class_options.values.select do |option| base_options[option.name.to_sym].nil? && option.group.nil? && - !group_options[human_name].any? { |i| i.name == option.name } + !group_options.values.flatten.any? { |i| i.name == option.name } end yield klass if block_given? diff --git a/railties/lib/generators/rails/controller/controller_generator.rb b/railties/lib/generators/rails/controller/controller_generator.rb index 3fed058c00..53e839d34d 100644 --- a/railties/lib/generators/rails/controller/controller_generator.rb +++ b/railties/lib/generators/rails/controller/controller_generator.rb @@ -1,7 +1,7 @@ module Rails module Generators class ControllerGenerator < NamedBase - argument :actions, :type => :array, :default => [], :banner => "action action" + argument :actions, :type => :array, :default => DEFAULTS[:actions], :banner => "action action" check_class_collision :suffix => "Controller" def create_controller_files diff --git a/railties/lib/generators/rails/resource/resource_generator.rb b/railties/lib/generators/rails/resource/resource_generator.rb index 9c57eda7de..37aca18758 100644 --- a/railties/lib/generators/rails/resource/resource_generator.rb +++ b/railties/lib/generators/rails/resource/resource_generator.rb @@ -2,19 +2,16 @@ require 'generators/rails/model/model_generator' module Rails module Generators - class ResourceGenerator < ModelGenerator + class ResourceGenerator < ModelGenerator #metagenerator hook_for :resource_controller do |base, controller| base.invoke controller, [ base.name.pluralize, base.options[:actions] ] end - class_option :actions, :type => :array, :default => [], :banner => "ACTION ACTION", - :desc => "Actions for the resource controller", :aliases => "-a" + class_option :actions, :type => :array, :banner => "ACTION ACTION", + :desc => "Actions for the resource controller" - class_option :singleton, :type => :boolean, :default => false, :aliases => "-i", - :desc => "Supply to create a singleton controller" - - class_option :force_plural, :type => :boolean, :default => false, :aliases => "-u", - :desc => "Forces the use of a plural ModelName" + class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller" + class_option :force_plural, :type => :boolean, :desc => "Forces the use of a plural ModelName" def initialize(args=[], options={}, config={}) super diff --git a/railties/lib/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/generators/rails/scaffold/scaffold_generator.rb index 73ec294cf0..c641165e3d 100644 --- a/railties/lib/generators/rails/scaffold/scaffold_generator.rb +++ b/railties/lib/generators/rails/scaffold/scaffold_generator.rb @@ -3,6 +3,8 @@ require 'generators/rails/resource/resource_generator' module Rails module Generators class ScaffoldGenerator < ResourceGenerator #metagenerator + class_option :test_framework, :banner => "NAME", :desc => "Test framework to be invoked" + remove_hook_for :actions, :resource_controller hook_for :scaffold_controller end diff --git a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb index 4d36b6603b..3a6620ae70 100644 --- a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -1,6 +1,7 @@ module Rails module Generators class ScaffoldControllerGenerator < NamedBase + class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller" check_class_collision :suffix => "Controller" def create_controller_files diff --git a/railties/lib/generators/test_unit/model/model_generator.rb b/railties/lib/generators/test_unit/model/model_generator.rb index 96883f592f..92fd75a2d5 100644 --- a/railties/lib/generators/test_unit/model/model_generator.rb +++ b/railties/lib/generators/test_unit/model/model_generator.rb @@ -4,9 +4,9 @@ module TestUnit module Generators class ModelGenerator < Base argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type" + class_option :fixture, :type => :boolean check_class_collision :suffix => "Test" - conditional_class_options :fixture def create_test_file template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb") |