Skip to content
Browse files

Allow railties to specify generators paths.

  • Loading branch information...
1 parent 9acf0af commit 4ca97650880a751901c4370a50c806a84fa529f4 @josevalim josevalim committed
View
6 railties/lib/rails/application.rb
@@ -8,7 +8,7 @@ class Application
class << self
attr_writer :config
alias configure class_eval
- delegate :initialize!, :load_tasks, :root, :to => :instance
+ delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance
private :new
def instance
@@ -82,6 +82,10 @@ def load_tasks
end
end
+ def load_generators
+ plugins.each { |p| p.load_generators }
+ end
+
def initializers
initializers = Bootstrap.new(self).initializers
plugins.each { |p| initializers += p.initializers }
View
38 railties/lib/rails/generators.rb
@@ -168,7 +168,7 @@ def self.invoke(namespace, args=ARGV, config={})
# Show help message with available generators.
def self.help
- traverse_load_paths!
+ lookup!
namespaces = subclasses.map{ |k| k.namespace }
namespaces.sort!
@@ -226,22 +226,10 @@ def self.invoke_fallbacks_for(name, base) #:nodoc:
nil
end
- # This will try to load any generator in the load path to show in help.
- def self.traverse_load_paths! #:nodoc:
- $LOAD_PATH.each do |base|
- Dir[File.join(base, "{generators,rails_generators}", "**", "*_generator.rb")].each do |path|
- begin
- require path
- rescue Exception => e
- # No problem
- end
- end
- end
- end
-
# Receives namespaces in an array and tries to find matching generators
# in the load path.
def self.lookup(namespaces) #:nodoc:
+ load_generators_from_railties!
paths = namespaces_to_paths(namespaces)
paths.each do |path|
@@ -261,6 +249,28 @@ def self.lookup(namespaces) #:nodoc:
end
end
+ # This will try to load any generator in the load path to show in help.
+ def self.lookup! #:nodoc:
+ load_generators_from_railties!
+
+ $LOAD_PATH.each do |base|
+ Dir[File.join(base, "{generators,rails_generators}", "**", "*_generator.rb")].each do |path|
+ begin
+ require path
+ rescue Exception => e
+ # No problem
+ end
+ end
+ end
+ end
+
+ # Allow generators to be loaded from custom paths.
+ def self.load_generators_from_railties! #:nodoc:
+ return if defined?(@generators_from_railties) || Rails.application.nil?
+ @generators_from_railties = true
+ Rails.application.load_generators
+ end
+
# Convert namespaces to paths by replacing ":" for "/" and adding
# an extra lookup. For example, "rails:model" should be searched
# in both: "rails/model/model_generator" and "rails/model_generator".
View
15 railties/lib/rails/railtie.rb
@@ -35,13 +35,28 @@ def self.rake_tasks(&blk)
@rake_tasks
end
+ def self.generators(&blk)
+ @generators ||= []
+ @generators << blk if blk
+ @generators
+ end
+
def rake_tasks
self.class.rake_tasks
end
+ def generators
+ self.class.generators
+ end
+
def load_tasks
return unless rake_tasks
rake_tasks.each { |blk| blk.call }
end
+
+ def load_generators
+ return unless generators
+ generators.each { |blk| blk.call }
+ end
end
end
View
7 railties/test/generators_test.rb
@@ -148,6 +148,13 @@ def self.name() 'NewGenerator' end
Rails::Generators.subclasses.delete(klass)
end
+ def test_load_generators_from_railties
+ Rails::Generators::ModelGenerator.expects(:start).with(["Account"], {})
+ Rails::Generators.send(:remove_instance_variable, :@generators_from_railties)
+ Rails.application.expects(:load_generators)
+ Rails::Generators.invoke("model", ["Account"])
+ end
+
def test_rails_root_templates
template = File.join(Rails.root, "lib", "templates", "active_record", "model", "model.rb")
View
16 railties/test/plugins/framework_extension_test.rb
@@ -30,6 +30,22 @@ class MyTie < Rails::Railtie
AppTemplate::Application.load_tasks
assert $ran_block
end
+
+ test "generators block is executed when MyApp.load_generators is called" do
+ $ran_block = false
+
+ class MyTie < Rails::Railtie
+ generators do
+ $ran_block = true
+ end
+ end
+
+ require "#{app_path}/config/environment"
+
+ assert !$ran_block
+ AppTemplate::Application.load_generators
+ assert $ran_block
+ end
end
class ActiveRecordExtensionTest < Test::Unit::TestCase

0 comments on commit 4ca9765

Please sign in to comment.
Something went wrong with that request. Please try again.