Skip to content
This repository
Browse code

Make the application name snake cased when it contains spaces

The application name is used to fill the `database.yml` and
`session_store.rb` files ; previously, if the provided name contained
whitespaces, it led to unexpected names in these files.

Since Shellwords.escape adds backslashes to escape spaces, the app_name
should remove them and replace any space with an underscore (just like
periods previously).

Also improve the assert_file helper to work with paths containing spaces
using String#shellescape.
  • Loading branch information...
commit 52b252614e275da799b6a15cebbfd067c59987d1 1 parent 99044be
Robin Dupret robin850 authored
8 railties/CHANGELOG.md
Source Rendered
... ... @@ -1,3 +1,11 @@
  1 +* Make the application name snake cased when it contains spaces
  2 +
  3 + The application name is used to fill the `database.yml` and
  4 + `session_store.rb` files ; previously, if the provided name
  5 + contained whitespaces, it led to unexpected names in these files.
  6 +
  7 + *Robin Dupret*
  8 +
1 9 * Added `--model-name` option to `ScaffoldControllerGenerator`.
2 10
3 11 *yalab*
2  railties/lib/rails/generators/rails/app/app_generator.rb
@@ -239,7 +239,7 @@ def file(*args, &block)
239 239 end
240 240
241 241 def app_name
242   - @app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root)).tr(".", "_")
  242 + @app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root)).tr('\\', '').tr(". ", "_")
243 243 end
244 244
245 245 def defined_app_name
2  railties/lib/rails/generators/testing/assertions.rb
@@ -21,7 +21,7 @@ module Assertions
21 21 # end
22 22 # end
23 23 def assert_file(relative, *contents)
24   - absolute = File.expand_path(relative, destination_root)
  24 + absolute = File.expand_path(relative, destination_root).shellescape
25 25 assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not"
26 26
27 27 read = File.read(absolute) if block_given? || !contents.empty?
10 railties/test/generators/app_generator_test.rb
@@ -367,6 +367,16 @@ def test_pretend_option
367 367 assert_no_match(/run bundle install/, output)
368 368 end
369 369
  370 + def test_application_name_with_spaces
  371 + path = File.join(destination_root, "foo bar".shellescape)
  372 +
  373 + # This also applies to MySQL apps but not with SQLite
  374 + run_generator [path, "-d", 'postgresql']
  375 +
  376 + assert_file "foo bar/config/database.yml", /database: foo_bar_development/
  377 + assert_file "foo bar/config/initializers/session_store.rb", /key: '_foo_bar/
  378 + end
  379 +
370 380 protected
371 381
372 382 def action(*args, &block)

0 comments on commit 52b2526

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