Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions guides/source/rails_application_templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ rails new blog -m ~/template.rb
$ rails new blog -m http://example.com/template.rb
```

You can use the rake task `rails:template` to apply templates to an existing Rails application. The location of the template needs to be passed in to an environment variable named LOCATION. Again, this can either be path to a file or a URL.
You can use the task `rails:template` to apply templates to an existing Rails application. The location of the template needs to be passed in to an environment variable named LOCATION. Again, this can either be path to a file or a URL.

```bash
$ bin/rails rails:template LOCATION=~/template.rb
Expand All @@ -38,7 +38,7 @@ The Rails templates API is easy to understand. Here's an example of a typical Ra
# template.rb
generate(:scaffold, "person name:string")
route "root to: 'people#index'"
rake("db:migrate")
rails_command("db:migrate")

after_bundle do
git :init
Expand Down Expand Up @@ -175,18 +175,24 @@ Executes an arbitrary command. Just like the backticks. Let's say you want to re
run "rm README.rdoc"
```

### rake(command, options = {})
### rails_command(command, options = {})

Runs the supplied rake tasks in the Rails application. Let's say you want to migrate the database:
Runs the supplied task in the Rails application. Let's say you want to migrate the database:

```ruby
rake "db:migrate"
rails_command "db:migrate"
```

You can also run rake tasks with a different Rails environment:
You can also run tasks with a different Rails environment:

```ruby
rake "db:migrate", env: 'production'
rails_command "db:migrate", env: 'production'
```

You can also run tasks as a super-user:

```ruby
rails_command "log:clear", sudo: true
```

### route(routing_code)
Expand Down Expand Up @@ -226,7 +232,7 @@ CODE
These methods let you ask questions from templates and decide the flow based on the user's answer. Let's say you want to Freeze Rails only if the user wants to:

```ruby
rake("rails:freeze:gems") if yes?("Freeze rails gems?")
rails_command("rails:freeze:gems") if yes?("Freeze rails gems?")
# no?(question) acts just the opposite.
```

Expand Down
5 changes: 5 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Alias `rake` with `rails_command` in the Rails Application Templates API
following Rails 5 convention of preferring "rails" to "rake" to run tasks.

*claudiob*

* Change fail fast of `bin/rails test` interrupts run on error.

*Yuji Yaginuma*
Expand Down
3 changes: 2 additions & 1 deletion railties/lib/rails/generators/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,9 @@ def rake(command, options={})
log :rake, command
env = options[:env] || ENV["RAILS_ENV"] || 'development'
sudo = options[:sudo] && RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : ''
in_root { run("#{sudo}#{extify(:rake)} #{command} RAILS_ENV=#{env}", verbose: false) }
in_root { run("#{sudo}#{extify(:rails)} #{command} RAILS_ENV=#{env}", verbose: false) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't sound right to me.. if someone asks us to run rake, we should really be running rake.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That make sense.

end
alias :rails_command :rake

# Just run the capify command in root
#
Expand Down
48 changes: 43 additions & 5 deletions railties/test/generators/actions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,43 +202,81 @@ def test_generate_should_run_script_generate_with_argument_and_options
end

def test_rake_should_run_rake_command_with_default_env
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=development", verbose: false]) do
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do
action :rake, 'log:clear'
end
end
end

def test_rake_with_env_option_should_run_rake_command_in_env
assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
action :rake, 'log:clear', env: 'production'
end
end

def test_rake_with_rails_env_variable_should_run_rake_command_in_env
assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
with_rails_env "production" do
action :rake, 'log:clear'
end
end
end

def test_env_option_should_win_over_rails_env_variable_when_running_rake
assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
with_rails_env "staging" do
action :rake, 'log:clear', env: 'production'
end
end
end

def test_rake_with_sudo_option_should_run_rake_command_with_sudo
assert_called_with(generator, :run, ["sudo rake log:clear RAILS_ENV=development", verbose: false]) do
assert_called_with(generator, :run, ["sudo rails log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do
action :rake, 'log:clear', sudo: true
end
end
end

def test_rails_command_should_run_rails_command_with_default_env
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do
action :rails_command, 'log:clear'
end
end
end

def test_rails_command_with_env_option_should_run_rails_command_in_env
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
action :rails_command, 'log:clear', env: 'production'
end
end

def test_rails_command_with_rails_env_variable_should_run_rails_command_in_env
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
with_rails_env "production" do
action :rails_command, 'log:clear'
end
end
end

def test_env_option_should_win_over_rails_env_variable_when_running_rails
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
with_rails_env "staging" do
action :rails_command, 'log:clear', env: 'production'
end
end
end

def test_rails_command_with_sudo_option_should_run_rails_command_with_sudo
assert_called_with(generator, :run, ["sudo rails log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do
action :rails_command, 'log:clear', sudo: true
end
end
end

def test_capify_should_run_the_capify_command
assert_called_with(generator, :run, ['capify .', verbose: false]) do
action :capify!
Expand Down