aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-07-16 00:17:28 +0200
committerJosé Valim <jose.valim@gmail.com>2009-07-16 00:17:28 +0200
commite3d5364e41f2063f4ecf3be4a5f05622c045dabf (patch)
treea733e00d405d92025d87fa87aafedc08b23f0b0e /railties
parent9c4ba74b7cd74b0312425ecc8c5b57aa6cf8c0d5 (diff)
downloadrails-e3d5364e41f2063f4ecf3be4a5f05622c045dabf.tar.gz
rails-e3d5364e41f2063f4ecf3be4a5f05622c045dabf.tar.bz2
rails-e3d5364e41f2063f4ecf3be4a5f05622c045dabf.zip
Implemented generaators fallbacks.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/generators.rb46
-rw-r--r--railties/test/generators_test.rb14
2 files changed, 49 insertions, 11 deletions
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