diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index 7af99797ea4a5b5ec9d615b18abd695038a445a7..e6baf2fc79c784157f0c29777dc54bef76e39d93 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -12,6 +12,16 @@ class Base < Thor::Group add_runtime_options! + # Always move to rails source root. + # + def initialize(*args) #:nodoc: + if !invoked?(args) && defined?(Rails.root) && Rails.root + self.destination_root = Rails.root + FileUtils.cd(destination_root) + end + super + end + # Automatically sets the source root based on the class name. # def self.source_root @@ -247,6 +257,13 @@ def class_collisions(*class_names) #:nodoc: end end + # Check if this generator was invoked from another one by inspecting + # parameters. + # + def invoked?(args) + args.last.is_a?(Hash) && args.last.key?(:invocations) + end + # Use Rails default banner. # def self.banner diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 199b5fa8b41802e7b0a88c0f3e1282c48521dbc7..f5cb26cf52effc79e21b5715efa41b959191531c 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -182,7 +182,7 @@ def run_generator end def generator(config={}) - @generator ||= Rails::Generators::Base.new([], {}, { :destination_root => destination_root }.merge!(config)) + @generator ||= Rails::Generators::Base.new([], {}, config) end def action(*args, &block) diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 20f2a24e6d9180c86e42cdb9bdb8fab6dff56a11..c44d25b72cf13c3d521f1e236a31b6b94d4a202e 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -126,7 +126,7 @@ def test_rails_is_frozen def test_template_from_dir_pwd FileUtils.cd(Rails.root) - assert_match /It works from file!/, run_generator(["-m", "lib/template.rb"]) + assert_match /It works from file!/, run_generator(["-m", "../lib/template.rb"]) end def test_template_raises_an_error_with_invalid_path @@ -170,7 +170,7 @@ def run_generator(args=[]) end def generator(options={}) - @generator ||= Rails::Generators::AppGenerator.new([destination_root], options, :destination_root => destination_root) + @generator ||= Rails::Generators::AppGenerator.new([destination_root], options) end def action(*args, &block) diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb index 56bc688ad0ef891290e4b9b4ce583fff929bdf48..3020e928dccc207f95e5b2fdfdb61aefcf8235d7 100644 --- a/railties/test/generators/controller_generator_test.rb +++ b/railties/test/generators/controller_generator_test.rb @@ -74,7 +74,7 @@ def test_actions_are_turned_into_methods protected def run_generator(args=["Account", "foo", "bar"]) - silence(:stdout) { Rails::Generators::ControllerGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::ControllerGenerator.start args } end end diff --git a/railties/test/generators/generator_generator_test.rb b/railties/test/generators/generator_generator_test.rb index aea3f4da516c83123a4fa24c1d25925d4536b73d..703aa20914f5ea14a4f2ee342d521bdceaf789d9 100644 --- a/railties/test/generators/generator_generator_test.rb +++ b/railties/test/generators/generator_generator_test.rb @@ -20,7 +20,7 @@ def test_generator_skeleton_is_created protected def run_generator(args=["awesome"], config={}) - silence(:stdout) { Rails::Generators::GeneratorGenerator.start args, config.merge(:destination_root => destination_root) } + silence(:stdout) { Rails::Generators::GeneratorGenerator.start args, config } end end diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index ccf08c347ccd22f513892e3e5067cdeb525a9452..829a38c10357ae8f94545e8792e6593cb59f233a 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -1,5 +1,5 @@ # TODO: Fix this RAILS_ENV stuff -RAILS_ENV = 'test' +RAILS_ENV = 'test' unless defined?(RAILS_ENV) require 'abstract_unit' Rails.application.config.root = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures')) @@ -11,12 +11,17 @@ CURRENT_PATH = File.expand_path(Dir.pwd) Rails::Generators.no_color! +module Rails + def self.root + @root ||= File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'tmp')) + end +end + class GeneratorsTestCase < Test::Unit::TestCase include FileUtils def destination_root - @destination_root ||= File.expand_path(File.join(File.dirname(__FILE__), - '..', 'fixtures', 'tmp')) + Rails.root end def setup diff --git a/railties/test/generators/helper_generator_test.rb b/railties/test/generators/helper_generator_test.rb index f8bfc517a22043dc1ce546f2084dd66300e5ebde..44f5a324af790c01ed00a333188837fa04b674dd 100644 --- a/railties/test/generators/helper_generator_test.rb +++ b/railties/test/generators/helper_generator_test.rb @@ -54,7 +54,7 @@ def test_namespaced_and_not_namespaced_helpers protected def run_generator(args=["admin"]) - silence(:stdout) { Rails::Generators::HelperGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::HelperGenerator.start args } end end diff --git a/railties/test/generators/integration_test_generator_test.rb b/railties/test/generators/integration_test_generator_test.rb index 6a504ceea2048cd4f782cde07f673facef9f3bcf..68b55a66f987645ce02e1599d4dc6d47c4ddeb73 100644 --- a/railties/test/generators/integration_test_generator_test.rb +++ b/railties/test/generators/integration_test_generator_test.rb @@ -12,7 +12,7 @@ def test_integration_test_skeleton_is_created protected def run_generator(args=["integration"]) - silence(:stdout) { Rails::Generators::IntegrationTestGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::IntegrationTestGenerator.start args } end end diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb index 251474ad167027cea031981212bf8b31fa9378e6..e33af25773d0305919dff2b6a4f1ebea108d500a 100644 --- a/railties/test/generators/mailer_generator_test.rb +++ b/railties/test/generators/mailer_generator_test.rb @@ -46,7 +46,7 @@ def test_actions_are_turned_into_methods protected def run_generator(args=["notifier", "foo", "bar"]) - silence(:stdout) { Rails::Generators::MailerGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::MailerGenerator.start args } end end diff --git a/railties/test/generators/metal_generator_test.rb b/railties/test/generators/metal_generator_test.rb index 80bf34289234548dbfc4257e556f4bd4b80a0a46..4f36e0f61247cbb2400b7052af0d588921eccb68 100644 --- a/railties/test/generators/metal_generator_test.rb +++ b/railties/test/generators/metal_generator_test.rb @@ -17,7 +17,7 @@ def test_check_class_collision protected def run_generator(args=["foo"]) - silence(:stdout) { Rails::Generators::MetalGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::MetalGenerator.start args } end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 35172a8be489c6241ebb781361c48bdc822ead55..b1fdbef42561c2494e751601cabdad69916ca536 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -53,7 +53,7 @@ def test_remove_migration_with_attributes protected def run_generator(args=[@migration]) - silence(:stdout) { Rails::Generators::MigrationGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::MigrationGenerator.start args } end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index e073b11e1eff220896b0b93f27a78511cea94b68..a0d4bed992ae3b0d16843e462f8de03ea6945cce 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -175,7 +175,7 @@ def test_check_class_collision protected def run_generator(args=["Account", "name:string", "age:integer"], config={}) - silence(:stdout) { Rails::Generators::ModelGenerator.start args, config.merge(:destination_root => destination_root) } + silence(:stdout) { Rails::Generators::ModelGenerator.start args, config } end end diff --git a/railties/test/generators/observer_generator_test.rb b/railties/test/generators/observer_generator_test.rb index 6fed2998dd983f5b48884c88b8a157409ffe32c4..becc217ac0edc26eb1df41f5a93d870488025bd7 100644 --- a/railties/test/generators/observer_generator_test.rb +++ b/railties/test/generators/observer_generator_test.rb @@ -27,7 +27,7 @@ def test_logs_if_the_test_framework_cannot_be_found protected def run_generator(args=["account"]) - silence(:stdout) { Rails::Generators::ObserverGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::ObserverGenerator.start args } end end diff --git a/railties/test/generators/performance_test_generator_test.rb b/railties/test/generators/performance_test_generator_test.rb index d19128f79ae9766c2209548d3c02505900b88219..00906a61e02374a334caddc93b6ebe1eecb09a85 100644 --- a/railties/test/generators/performance_test_generator_test.rb +++ b/railties/test/generators/performance_test_generator_test.rb @@ -12,7 +12,7 @@ def test_performance_test_skeleton_is_created protected def run_generator(args=["performance"]) - silence(:stdout) { Rails::Generators::PerformanceTestGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::PerformanceTestGenerator.start args } end end diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index f5b8b6ffb648e0a8f8850f33a1eab40d53f79b9f..c8bfaf3d97dc77dc51c0f671ee79143d51946f9a 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -50,7 +50,7 @@ def test_plugin_generator_on_revoke protected def run_generator(args=["plugin_fu"], config={}) - silence(:stdout) { Rails::Generators::PluginGenerator.start args, config.merge(:destination_root => destination_root) } + silence(:stdout){ Rails::Generators::PluginGenerator.start args, config } end end diff --git a/railties/test/generators/resource_generator_test.rb b/railties/test/generators/resource_generator_test.rb index dcae81c204cf1a4d4745f9d3ae01b32d790b9e4e..99811bc07b96c450acb07df7e19c0304f908da2c 100644 --- a/railties/test/generators/resource_generator_test.rb +++ b/railties/test/generators/resource_generator_test.rb @@ -100,7 +100,7 @@ def test_route_is_removed_on_revoke protected def run_generator(args=["account"], config={}) - silence(:stdout) { Rails::Generators::ResourceGenerator.start args, config.merge(:destination_root => destination_root) } + silence(:stdout) { Rails::Generators::ResourceGenerator.start args, config } end end diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 02155c295c813b1fc4ac5fdfd52cad24ada78f43..43647360d6e0690af38a850b0014ccc0ba2c9bd6 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -139,7 +139,7 @@ def self.all(klass) protected def run_generator(args=["User", "name:string", "age:integer"]) - silence(:stdout) { Rails::Generators::ScaffoldControllerGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::ScaffoldControllerGenerator.start args } end end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index c0652c034f36fcbdcf0dc3300af41a5404373580..09ab58e404ca4be147a5bd00ddfb77884d1d52cd 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -122,8 +122,7 @@ def test_scaffold_on_revoke def run_generator(config={}) silence(:stdout) do - Rails::Generators::ScaffoldGenerator.start ["product_line", "title:string", "price:integer"], - config.merge(:destination_root => destination_root) + Rails::Generators::ScaffoldGenerator.start ["product_line", "title:string", "price:integer"], config end end diff --git a/railties/test/generators/session_migration_generator_test.rb b/railties/test/generators/session_migration_generator_test.rb index 34fb996b7f25514d384e59b772f937200de9bb73..342b9a900ec4b6ddcdad41bf0c1c3290194d88d3 100644 --- a/railties/test/generators/session_migration_generator_test.rb +++ b/railties/test/generators/session_migration_generator_test.rb @@ -28,7 +28,7 @@ def test_session_migration_with_custom_table_name protected def run_generator(args=[]) - silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args, :destination_root => destination_root } + silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args } end end diff --git a/railties/test/generators/stylesheets_generator_test.rb b/railties/test/generators/stylesheets_generator_test.rb index 15263d4bb8767c76bc181def101d1cfe96c59584..6a07898c51c7a70c72e3b4a12d3d1bdaeb2d18da 100644 --- a/railties/test/generators/stylesheets_generator_test.rb +++ b/railties/test/generators/stylesheets_generator_test.rb @@ -18,7 +18,7 @@ def test_stylesheets_are_not_deleted_on_revoke protected def run_generator(config={}) - silence(:stdout) { Rails::Generators::StylesheetsGenerator.start [], config.merge(:destination_root => destination_root) } + silence(:stdout) { Rails::Generators::StylesheetsGenerator.start [], config } end end