From 2699e9c2ddff13c5e19e1c95e838fa6d9a965c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 8 Jul 2009 10:57:56 +0200 Subject: [PATCH] Added config.generators with tests. --- railties/lib/rails/configuration.rb | 49 ++++++++++++++++++++++++++++- railties/test/initializer_test.rb | 29 +++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 1a2f217d20..4a5fe4da7d 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -249,5 +249,52 @@ module Rails def reload_plugins? @reload_plugins end + + # 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 + # + # You can also configure/override aliases: + # + # config.generators.aliases :test_framework => "-w" + # + def generators + @generators ||= Generators.new + if block_given? + yield @generators + else + @generators + end + end + + class Generators #:nodoc: + def initialize + @aliases, @options = {}, {} + end + + def aliases(values=nil) + @aliases = values if values + @aliases + end + + def options(values=nil) + @options = values if values + @options + end + + def method_missing(method, *args, &block) + @options[method.to_sym] = args.first + end + end end -end \ No newline at end of file +end diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index 550cb7de76..da78647ab3 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -278,6 +278,35 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase end end +class InitializerGeneratorsTests < Test::Unit::TestCase + def test_generators_empty_aliases_and_options + assert_equal({}, Rails::Configuration.new.generators.aliases) + assert_equal({}, Rails::Configuration.new.generators.options) + end + + def test_generators_set_options + config = Rails::Configuration.new + config.generators.orm :datamapper + config.generators.test_framework :rspec + assert_equal({ :orm => :datamapper, :test_framework => :rspec }, config.generators.options) + end + + def test_generators_set_aliases + config = Rails::Configuration.new + config.generators.aliases :test_framework => "-w" + assert_equal({ :test_framework => "-w" }, config.generators.aliases) + end + + def test_generators_with_block + config = Rails::Configuration.new + config.generators do |g| + g.orm :datamapper + g.test_framework :rspec + end + assert_equal({ :orm => :datamapper, :test_framework => :rspec }, config.generators.options) + end +end + class InitializerSetupI18nTests < Test::Unit::TestCase def test_no_config_locales_dir_present_should_return_empty_load_path File.stubs(:exist?).returns(false)