提交 d5d0b8ea 编写于 作者: J José Valim

Removing unecessary class methods.

上级 35c4df4a
......@@ -7,10 +7,9 @@ class ModelGenerator < Base
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")
......
......@@ -3,18 +3,22 @@
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 @@ def self.namespace(name=nil)
# 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 @@ def invoke_for_#{name}
# 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 @@ def self.remove_hook_for(*names)
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 @@ def self.invocation_blocks #:nodoc:
@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 @@ def self.class_options_help(shell, ungrouped_name=nil, extra_group=nil)
# 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 @@ def self.get_options_from_invocations(group_options, base_options)
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?
......
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
......
......@@ -2,19 +2,16 @@
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
......
......@@ -3,6 +3,8 @@
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
......
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
......
......@@ -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")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册