aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/generators.rb86
-rw-r--r--railties/lib/generators/base.rb36
-rw-r--r--railties/lib/generators/rails/app/templates/config/environment.rb26
-rw-r--r--railties/lib/generators/rails/plugin/plugin_generator.rb5
-rw-r--r--railties/lib/initializer.rb4
-rw-r--r--railties/lib/rails/configuration.rb53
-rw-r--r--railties/lib/vendor/thor-0.11.1/lib/thor/parser/option.rb13
-rw-r--r--railties/lib/vendor/thor-0.11.1/lib/thor/parser/options.rb13
-rw-r--r--railties/test/initializer_test.rb60
9 files changed, 204 insertions, 92 deletions
diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb
index ddb65c8579..6935cb8319 100644
--- a/railties/lib/generators.rb
+++ b/railties/lib/generators.rb
@@ -17,34 +17,64 @@ require 'generators/named_base'
module Rails
module Generators
- DEFAULT_ALIASES = {
- :actions => '-a',
- :fixture_replacement => '-r',
- :orm => '-o',
- :resource_controller => '-c',
- :scaffold_controller => '-c',
- :stylesheets => '-y',
- :test_framework => '-t',
- :template_engine => '-e'
- }
-
- DEFAULT_OPTIONS = {
- :fixture => true,
- :force_plural => false,
- :helper => true,
- :integration_tool => :test_unit,
- :layout => true,
- :migration => true,
- :orm => :active_record,
- :performance_tool => :test_unit,
- :resource_controller => :controller,
- :scaffold_controller => :scaffold_controller,
- :singleton => false,
- :stylesheets => true,
- :test_framework => :test_unit,
- :template_engine => :erb,
- :timestamps => true
- }
+ DEFAULT_ALIASES = Hash.new{ |h,k| h[k] = {} }
+ DEFAULT_ALIASES.merge!(
+ :rails => {
+ :actions => '-a',
+ :orm => '-o',
+ :resource_controller => '-c',
+ :scaffold_controller => '-c',
+ :stylesheets => '-y',
+ :template_engine => '-e',
+ :test_framework => '-t'
+ },
+
+ :test_unit => {
+ :fixture_replacement => '-r',
+ },
+
+ :plugin => {
+ :generator => '-g',
+ :tasks => '-r'
+ }
+ )
+
+ DEFAULT_OPTIONS = Hash.new{ |h,k| h[k] = {} }
+ DEFAULT_OPTIONS.merge!(
+ :active_record => {
+ :migration => true,
+ :timestamps => true
+ },
+
+ :erb => {
+ :layout => true
+ },
+
+ :rails => {
+ :force_plural => false,
+ :helper => true,
+ :layout => true,
+ :orm => :active_record,
+ :integration_tool => :test_unit,
+ :performance_tool => :test_unit,
+ :resource_controller => :controller,
+ :scaffold_controller => :scaffold_controller,
+ :singleton => false,
+ :stylesheets => true,
+ :template_engine => :erb,
+ :test_framework => :test_unit
+ },
+
+ :test_unit => {
+ :fixture => true,
+ :fixture_replacement => nil
+ },
+
+ :plugin => {
+ :generator => false,
+ :tasks => false
+ }
+ )
def self.aliases
@@aliases ||= DEFAULT_ALIASES.dup
diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb
index c1acd4c94f..1be0dd6ec2 100644
--- a/railties/lib/generators/base.rb
+++ b/railties/lib/generators/base.rb
@@ -110,7 +110,7 @@ module Rails
names.each do |name|
defaults = if options[:type] == :boolean
{ }
- elsif [true, false].include?(options.fetch(:default, Rails::Generators.options[name]))
+ elsif [true, false].include?(options.fetch(:default, default_value_for_option(name)))
{ :banner => "" }
else
{ :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" }
@@ -143,8 +143,8 @@ module Rails
#
def self.class_option(name, options={}) #:nodoc:
options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc)
- options[:aliases] = Rails::Generators.aliases[name] unless options.key?(:aliases)
- options[:default] = Rails::Generators.options[name] unless options.key?(:default)
+ options[:aliases] = default_aliases_for_option(name) unless options.key?(:aliases)
+ options[:default] = default_value_for_option(name) unless options.key?(:default)
super(name, options)
end
@@ -205,6 +205,36 @@ module Rails
end
end
+ # Return the default value for the option name given doing a lookup in
+ # Rails::Generators.options.
+ #
+ def self.default_value_for_option(option)
+ options = Rails::Generators.options
+
+ if options[generator_name.to_sym].key?(option)
+ options[generator_name.to_sym][option]
+ elsif options[base_name.to_sym].key?(option)
+ options[base_name.to_sym][option]
+ else
+ options[:rails][option]
+ end
+ end
+
+ # Return default aliases for the option name given doing a lookup in
+ # Rails::Generators.aliases.
+ #
+ def self.default_aliases_for_option(option)
+ aliases = Rails::Generators.aliases
+
+ if aliases[generator_name.to_sym].key?(option)
+ aliases[generator_name.to_sym][option]
+ elsif aliases[base_name.to_sym].key?(option)
+ aliases[base_name.to_sym][option]
+ else
+ aliases[:rails][option]
+ end
+ end
+
# Keep hooks configuration that are used on prepare_for_invocation.
#
def self.hooks #:nodoc:
diff --git a/railties/lib/generators/rails/app/templates/config/environment.rb b/railties/lib/generators/rails/app/templates/config/environment.rb
index 02fbb57b87..59c1f0bfb2 100644
--- a/railties/lib/generators/rails/app/templates/config/environment.rb
+++ b/railties/lib/generators/rails/app/templates/config/environment.rb
@@ -45,20 +45,22 @@ Rails::Initializer.run do |config|
# Configure generators default options.
config.generators do |g|
- # Scaffold configuration
- g.helper = true
- g.layout = true
- g.stylesheets = true
+ g.rails do |r|
+ r.helper = true
+ r.stylesheets = true
+ end
- # ORM configuration
- g.orm = :active_record
- g.timestamps = true
+ g.orm :active_record do |ar|
+ ar.migration = true
+ ar.timestamps = true
+ end
- # Template engine configuration
- g.template_engine = :erb
+ g.template_engine :erb do |erb|
+ erb.layout = true
+ end
- # Test framework configuration
- g.test_framework = :test_unit
- g.fixtures = true
+ g.test_framework :test_unit do |tu|
+ tu.fixtures = true
+ end
end
end
diff --git a/railties/lib/generators/rails/plugin/plugin_generator.rb b/railties/lib/generators/rails/plugin/plugin_generator.rb
index 49fe409ea8..a7417f28c2 100644
--- a/railties/lib/generators/rails/plugin/plugin_generator.rb
+++ b/railties/lib/generators/rails/plugin/plugin_generator.rb
@@ -3,8 +3,7 @@ require 'generators/rails/generator/generator_generator'
module Rails
module Generators
class PluginGenerator < NamedBase
- class_option :tasks, :type => :boolean, :aliases => "-t", :default => false,
- :desc => "When supplied creates tasks base files."
+ class_option :tasks, :desc => "When supplied creates tasks base files."
check_class_collision
@@ -21,7 +20,7 @@ module Rails
directory 'tasks', plugin_dir('tasks')
end
- hook_for :generator, :aliases => "-g", :type => :boolean do |instance, generator|
+ hook_for :generator do |instance, generator|
instance.inside_with_padding instance.send(:plugin_dir) do
instance.invoke generator, [ instance.name ], :namespace => false
end
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb
index d0ef9d0c2f..bb04dfa370 100644
--- a/railties/lib/initializer.rb
+++ b/railties/lib/initializer.rb
@@ -573,8 +573,8 @@ Run `rake gems:install` to install the missing gems.
Initializer.default.add :initialize_generators do
if defined?(Rails::Generators)
Rails::Generators.no_color! unless config.generators.colorize_logging
- Rails::Generators.aliases.merge! config.generators.aliases
- Rails::Generators.options.merge! config.generators.options
+ Rails::Generators.aliases.deep_merge! config.generators.aliases
+ Rails::Generators.options.deep_merge! config.generators.options
end
end
end
diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb
index 1a8f261e6f..edbec6ad70 100644
--- a/railties/lib/rails/configuration.rb
+++ b/railties/lib/rails/configuration.rb
@@ -252,23 +252,22 @@ module Rails
# Holds generators configuration:
#
- # config.generators.orm = :datamapper
- # config.generators.test_framework = :rspec
- # config.generators.template_engine = :haml
- #
- # A block can also be given for less verbose configuration:
- #
# config.generators do |g|
- # g.orm = :datamapper
- # g.test_framework = :datamapper
- # g.template_engine = :haml
- # end
+ # g.orm :datamapper do |dm|
+ # dm.migration = true
+ # dm.timestamps = false
+ # end
#
- # You can also configure/override aliases:
+ # g.template_engine = :haml
+ # g.test_framework = :datamapper
#
- # config.generators.aliases = :test_framework => "-w"
+ # g.plugin do |p|
+ # p.aliases :generator => "-g"
+ # p.generator = true
+ # end
+ # end
#
- # Finally, to disable color in console, do:
+ # If you want to disable color in console, do:
#
# config.generators.colorize_logging = false
#
@@ -282,18 +281,30 @@ module Rails
end
class Generators #:nodoc:
- attr_accessor :aliases, :options, :colorize_logging
+ attr_reader :aliases, :options
+ attr_accessor :colorize_logging
def initialize
- @aliases, @options, @colorize_logging = {}, {}, true
+ @namespace, @colorize_logging = :rails, true
+ @aliases = Hash.new { |h,k| h[k] = {} }
+ @options = Hash.new { |h,k| h[k] = {} }
end
- def method_missing(method, *args, &block)
- method = method.to_s
- if method.gsub!(/=$/, '')
- @options[method.to_sym] = args.first
- else
- super(method.to_sym, *args, &block)
+ def aliases(another=nil)
+ @aliases[@namespace].merge!(another) if another
+ @aliases
+ end
+ alias :aliases= :aliases
+
+ def method_missing(method, *args)
+ sanitized_method = method.to_s.sub(/=$/, '').to_sym
+ @options[@namespace][sanitized_method] = args.first if args.first
+
+ if block_given?
+ previous_namespace = @namespace
+ @namespace = (args.first || sanitized_method).to_sym
+ yield self
+ @namespace = previous_namespace
end
end
end
diff --git a/railties/lib/vendor/thor-0.11.1/lib/thor/parser/option.rb b/railties/lib/vendor/thor-0.11.1/lib/thor/parser/option.rb
index ab9be6fb9f..5c43f6b18f 100644
--- a/railties/lib/vendor/thor-0.11.1/lib/thor/parser/option.rb
+++ b/railties/lib/vendor/thor-0.11.1/lib/thor/parser/option.rb
@@ -95,14 +95,21 @@ class Thor
end
end
- def input_required?
- type != :boolean
+ # Allow some type predicates as: boolean?, string? and etc.
+ #
+ def method_missing(method, *args, &block)
+ given = method.to_s.sub(/\?$/, '').to_sym
+ if valid_type?(given)
+ self.type == given
+ else
+ super
+ end
end
protected
def validate!
- raise ArgumentError, "An option cannot be boolean and required." if type == :boolean && required?
+ raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
end
def valid_type?(type)
diff --git a/railties/lib/vendor/thor-0.11.1/lib/thor/parser/options.rb b/railties/lib/vendor/thor-0.11.1/lib/thor/parser/options.rb
index 29f6500d97..01c86b7b27 100644
--- a/railties/lib/vendor/thor-0.11.1/lib/thor/parser/options.rb
+++ b/railties/lib/vendor/thor-0.11.1/lib/thor/parser/options.rb
@@ -122,9 +122,16 @@ class Thor
# Parse the value at the peek analyzing if it requires an input or not.
#
def parse_peek(switch, option)
- if option.input_required?
- return nil if no_or_skip?(switch)
- raise MalformattedArgumentError, "no value provided for option '#{switch}'" unless current_is_value?
+ unless current_is_value?
+ if option.boolean?
+ # No problem for boolean types
+ elsif no_or_skip?(switch)
+ return nil # User set value to nil
+ elsif option.string? && !option.required?
+ return option.human_name # Return the option name
+ else
+ raise MalformattedArgumentError, "no value provided for option '#{switch}'"
+ end
end
@non_assigned_required.delete(option)
diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb
index 163785fee3..c9362dbe36 100644
--- a/railties/test/initializer_test.rb
+++ b/railties/test/initializer_test.rb
@@ -304,52 +304,78 @@ class InitializerGeneratorsTests < Test::Unit::TestCase
assert_equal({}, @configuration.generators.options)
end
- def test_generators_set_options
+ def test_generators_set_rails_options
@configuration.generators.orm = :datamapper
@configuration.generators.test_framework = :rspec
- assert_equal({ :orm => :datamapper, :test_framework => :rspec }, @configuration.generators.options)
+ expected = { :rails => { :orm => :datamapper, :test_framework => :rspec } }
+ assert_equal expected, @configuration.generators.options
end
- def test_generators_set_aliases
+ def test_generators_set_rails_aliases
@configuration.generators.aliases = { :test_framework => "-w" }
- assert_equal({ :test_framework => "-w" }, @configuration.generators.aliases)
+ expected = { :rails => { :test_framework => "-w" } }
+ assert_equal expected, @configuration.generators.aliases
end
- def test_generators_with_block
+ def test_generators_with_block_for_rails_configuration
@configuration.generators do |g|
g.orm = :datamapper
g.test_framework = :rspec
end
- assert_equal({ :orm => :datamapper, :test_framework => :rspec }, @configuration.generators.options)
+ expected = { :rails => { :orm => :datamapper, :test_framework => :rspec } }
+ assert_equal expected, @configuration.generators.options
end
def test_generators_aliases_and_options_on_initialization
- @configuration.generators.aliases = { :test_framework => "-w" }
- @configuration.generators.orm = :datamapper
- @configuration.generators.test_framework = :rspec
+ @configuration.generators.aliases :test_framework => "-w"
+ @configuration.generators.orm :datamapper
+ @configuration.generators.test_framework :rspec
- RAILS_ENV.replace "generators"
@initializer.run(:initialize_generators)
- assert_equal :rspec, Rails::Generators.options[:test_framework]
- assert_equal "-w", Rails::Generators.aliases[:test_framework]
+ assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
+ assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework]
end
def test_generators_no_color_on_initialization
@configuration.generators.colorize_logging = false
- RAILS_ENV.replace "generators"
@initializer.run(:initialize_generators)
assert_equal Thor::Base.shell, Thor::Shell::Basic
end
- def test_generators_raise_no_method_error_non_setters
- assert_raise NoMethodError do
- @configuration.generators.foo
- end
+ def test_generators_with_namespaced_blocks_for_options_and_aliases
+ namespaced_configuration!
+ expected = {
+ :rails => { :orm => :datamapper },
+ :plugin => { :generator => true },
+ :datamapper => { :migrations => true }
+ }
+ assert_equal expected, @configuration.generators.options
+ assert_equal({ :plugin => { :generator => "-g" } }, @configuration.generators.aliases)
+ end
+
+ def test_generators_with_namespaced_configuration_are_deep_merged
+ namespaced_configuration!
+ @initializer.run(:initialize_generators)
+ assert Rails::Generators.aliases.size >= 1
+ assert Rails::Generators.options.size >= 1
end
protected
+ def namespaced_configuration!
+ @configuration.generators do |g|
+ g.orm :datamapper do |dm|
+ dm.migrations = true
+ end
+
+ g.plugin do |p|
+ p.aliases = { :generator => "-g" }
+ p.generator = true
+ end
+ end
+ end
+
def teardown
Rails::Generators.clear_aliases!
Rails::Generators.clear_options!