From e3d5364e41f2063f4ecf3be4a5f05622c045dabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 16 Jul 2009 00:17:28 +0200 Subject: Implemented generaators fallbacks. --- railties/lib/generators.rb | 46 ++++++++++++++++++++++++++++++---------- railties/test/generators_test.rb | 14 ++++++++++++ 2 files changed, 49 insertions(+), 11 deletions(-) (limited to 'railties') diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb index c59bb9776f..bb821c0896 100644 --- a/railties/lib/generators.rb +++ b/railties/lib/generators.rb @@ -75,14 +75,31 @@ module Rails } } - def self.aliases + def self.aliases #:nodoc: @@aliases ||= DEFAULT_ALIASES.dup end - def self.options + def self.options #:nodoc: @@options ||= DEFAULT_OPTIONS.dup end + # Hold configured generators fallbacks. If a plugin developer wants a + # generator group to fallback to another group in case of missing generators, + # they can add a fallback. + # + # For example, shoulda is considered a test_framework and is an extension + # of test_unit. However, most part of shoulda generators are similar to + # test_unit ones. + # + # Shoulda then can tell generators to search for test_unit generators when + # some of them are not available by adding a fallback: + # + # Rails::Generators.fallbacks[:shoulda] = :test_unit + # + def self.fallbacks + @@fallbacks ||= {} + end + # Remove the color from output. # def self.no_color! @@ -137,16 +154,23 @@ module Rails # # "test_unit:generators:model", "test_unit:model" # - def self.find_by_namespace(name, base=nil, context=nil) + def self.find_by_namespace(name, base=nil, context=nil) #:nodoc: name, attempts = name.to_s, [] - if name.count(':') == 0 - attempts << "#{base}:generators:#{name}" if base - attempts << "#{name}:generators:#{context}" if context + case name.count(':') + when 1 + base, name = name.split(':') + return find_by_namespace(name, base) + when 0 + if base + base = base.to_sym + attempts << "#{base}:generators:#{name}" + attempts << "#{fallbacks[base]}:generators:#{name}" if fallbacks[base] + end + attempts << "#{name}:generators:#{context}" if context end - attempts << name.sub(':', ':generators:') if name.count(':') == 1 - attempts << name + attempts << name unloaded = attempts - namespaces lookup(unloaded) @@ -206,13 +230,13 @@ module Rails # Return all defined namespaces. # - def self.namespaces + def self.namespaces #:nodoc: Thor::Base.subclasses.map{ |klass| klass.namespace } end # Keep builtin generators in an Array[Array[group, name]]. # - def self.builtin + def self.builtin #:nodoc: Dir[File.dirname(__FILE__) + '/generators/*/*'].collect do |file| file.split('/')[-2, 2] end @@ -230,7 +254,7 @@ module Rails # generators/rails/model_generator.rb # generators/model_generator.rb # - def self.lookup(attempts) + def self.lookup(attempts) #:nodoc: attempts.each do |attempt| generators_path = ['.'] diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 78c05f2eb8..56c95caa41 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -1,5 +1,6 @@ require File.join(File.dirname(__FILE__), 'generators', 'generators_test_helper') require 'generators/rails/model/model_generator' +require 'generators/test_unit/model/model_generator' require 'mocha' class GeneratorsTest < GeneratorsTestCase @@ -114,4 +115,17 @@ class GeneratorsTest < GeneratorsTestCase ensure rm_rf File.dirname(template) end + + def test_fallbacks_for_generators_on_invoke + Rails::Generators.fallbacks[:shoulda] = :test_unit + TestUnit::Generators::ModelGenerator.expects(:start).with(["Account"], {}) + Rails::Generators.invoke "shoulda:model", ["Account"] + end + + def test_fallbacks_for_generators_on_find_by_namespace + Rails::Generators.fallbacks[:remarkable] = :test_unit + klass = Rails::Generators.find_by_namespace(:plugin, :remarkable) + assert klass + assert_equal "test_unit:generators:plugin", klass.namespace + end end -- cgit v1.2.3