Skip to content
This repository
Browse code

Improved how AppGenerator generates the application name. It now dete…

…cts the current app name whenever possible. This means that renaming the residing directory will not effect the app name generated by AppGenerator.

[#5225 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit cdad483dff4fef1b640dc3c750719c325b252f89 1 parent aeaab06
Fred Wu fredwu authored josevalim committed
6 railties/lib/rails/generators/rails/app/app_generator.rb
@@ -356,8 +356,12 @@ def app_name
356 356 @app_name ||= File.basename(destination_root)
357 357 end
358 358
  359 + def defined_app_const_base
  360 + Rails.application.class.name.sub(/::Application$/, "") if Rails.application.instance_of?(Rails::Application)
  361 + end
  362 +
359 363 def app_const_base
360   - @app_const_base ||= app_name.gsub(/\W/, '_').squeeze('_').camelize
  364 + @app_const_base ||= defined_app_const_base || app_name.gsub(/\W/, '_').squeeze('_').camelize
361 365 end
362 366
363 367 def app_const
24 railties/test/generators/app_generator_test.rb
@@ -106,6 +106,30 @@ def test_invalid_application_name_is_fixed
106 106 assert_file "things-43/config/application.rb", /^module Things43$/
107 107 end
108 108
  109 + def test_application_name_is_detected_if_it_exists_and_app_folder_renamed
  110 + app_root = File.join(destination_root, "myapp")
  111 + app_moved_root = File.join(destination_root, "myapp_moved")
  112 +
  113 + run_generator [app_root]
  114 +
  115 + Rails.application.config.root = app_moved_root
  116 + Rails.application.class.stubs(:name).returns("Myapp")
  117 + Rails.application.stubs(:instance_of?).returns(Rails::Application)
  118 +
  119 + FileUtils.mv(app_root, app_moved_root)
  120 +
  121 + # forces the shell to automatically overwrite all files
  122 + Thor::Base.shell.send(:attr_accessor, :always_force)
  123 + shell = Thor::Base.shell.new
  124 + shell.send(:always_force=, true)
  125 +
  126 + generator = Rails::Generators::AppGenerator.new ["rails"], { :with_dispatchers => true },
  127 + :destination_root => app_moved_root, :shell => shell
  128 + generator.send(:app_const)
  129 + silence(:stdout){ generator.send(:create_config_files) }
  130 + assert_file "myapp_moved/config/environment.rb", /Myapp::Application\.initialize!/
  131 + end
  132 +
109 133 def test_application_names_are_not_singularized
110 134 run_generator [File.join(destination_root, "hats")]
111 135 assert_file "hats/config/environment.rb", /Hats::Application\.initialize!/

0 comments on commit cdad483

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