diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 9c8302a710..9685995afd 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -222,17 +222,13 @@ module Rails # the generator or an Array that is joined. # # generate(:authenticated, "user session") - def generate(*args) + def generate(what, *args) + log :generate, what + options = args.extract_options! - args = Shellwords.split(args.join(" ")) + options[:abort_on_failure] = !options[:inline] - log :generate, args.first - - in_root do - silence_warnings do - ::Rails::Command.invoke("generate", args, options) - end - end + rails_command "generate #{what} #{args.join(" ")}", options end # Runs the supplied rake task (invoked with 'rake ...') @@ -252,7 +248,17 @@ module Rails # rails_command("gems:install", sudo: true) # rails_command("gems:install", capture: true) def rails_command(command, options = {}) - execute_command :rails, command, options + if options[:inline] + log :rails, command + command, *args = Shellwords.split(command) + in_root do + silence_warnings do + ::Rails::Command.invoke(command, args, options) + end + end + else + execute_command :rails, command, options + end end # Make an entry in Rails routing file config/routes.rb diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 6159b9b8e9..8227a1310a 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -384,16 +384,28 @@ class ActionsTest < Rails::Generators::TestCase assert_file "app/models/my_model.rb", /MyModel/ end - test "generate with concatenated arguments" do + test "generate should raise on failure" do run_generator - action :generate, "model MyModel name:string" + message = capture(:stderr) do + assert_raises SystemExit do + action :generate, "model", "1234567890" + end + end + assert_match(/1234567890/, message) + end + + test "generate with inline option" do + run_generator + assert_not_called(generator, :run) do + action :generate, "model", "MyModel", inline: true + end assert_file "app/models/my_model.rb", /MyModel/ end - test "generate should raise on failure" do + test "generate with inline option should raise on failure" do run_generator error = assert_raises do - action :generate, "model", "1234567890" + action :generate, "model", "1234567890", inline: true end assert_match(/1234567890/, error.message) end @@ -503,6 +515,22 @@ class ActionsTest < Rails::Generators::TestCase end end + test "rails_command with inline option" do + run_generator + assert_not_called(generator, :run) do + action :rails_command, "generate model MyModel", inline: true + end + assert_file "app/models/my_model.rb", /MyModel/ + end + + test "rails_command with inline option should raise on failure" do + run_generator + error = assert_raises do + action :rails_command, "generate model 1234567890", inline: true + end + assert_match(/1234567890/, error.message) + end + test "route should add route" do run_generator route_commands = ["get 'foo'", "get 'bar'", "get 'baz'"]