diff options
author | José Valim <jose.valim@gmail.com> | 2010-01-19 18:43:09 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-19 19:59:08 +0100 |
commit | 4ca97650880a751901c4370a50c806a84fa529f4 (patch) | |
tree | fedf6fc1dfc022015bc4f9c53004035a58e7a544 | |
parent | 9acf0af544f2f5dcaf257bdc25047017c972ffce (diff) | |
download | rails-4ca97650880a751901c4370a50c806a84fa529f4.tar.gz rails-4ca97650880a751901c4370a50c806a84fa529f4.tar.bz2 rails-4ca97650880a751901c4370a50c806a84fa529f4.zip |
Allow railties to specify generators paths.
-rw-r--r-- | railties/lib/rails/application.rb | 6 | ||||
-rw-r--r-- | railties/lib/rails/generators.rb | 38 | ||||
-rw-r--r-- | railties/lib/rails/railtie.rb | 15 | ||||
-rw-r--r-- | railties/test/generators_test.rb | 7 | ||||
-rw-r--r-- | railties/test/plugins/framework_extension_test.rb | 16 |
5 files changed, 67 insertions, 15 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 4d05f8115c..b92a7ff129 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -8,7 +8,7 @@ module Rails 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 @@ module Rails 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 } diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 736c36c0dc..d3175e6a9d 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -168,7 +168,7 @@ module Rails # 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 @@ module Rails 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 @@ module Rails 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". diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 43a0303c5b..e3297148e5 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -35,13 +35,28 @@ module Rails @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 diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 60c81a813f..664d1e5670 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -148,6 +148,13 @@ class GeneratorsTest < Rails::Generators::TestCase 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") diff --git a/railties/test/plugins/framework_extension_test.rb b/railties/test/plugins/framework_extension_test.rb index c920db77aa..d57fd4e635 100644 --- a/railties/test/plugins/framework_extension_test.rb +++ b/railties/test/plugins/framework_extension_test.rb @@ -30,6 +30,22 @@ module PluginsTest 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 |