In my application.rb file I have a few options to modify my apps URLs like so:
config.action_controller.default_url_options = { :trailing_slash => true }
But these do not seem to take effect in my Rake tasks, despite the fact that I am running them in the Rails environment via the :environment dependency.
I know that I can get this to work simply by calling the following in my rake task:
default_url_options[:trailing_slash] = true
... but I'd like to DRY this up. Is there a clean way to make a rake task use Rails' default_url_options from application.rb?
You can use the following in a rake task (and in Rails console):
Rails.application.config.action_controller.default_url_options
Related
Normally I run my tests with parallel_cucumber which runs different features in parallel using parallel_test gem. I want to setup a rake task that will run using different profiles and run the features within each task in parallel.
I have setup my Rakefile this way:
namespace :features do
Cucumber::Rake::Task.new(:basket) do |t|
t.profile = "basket"
end
Cucumber::Rake::Task.new(:fruits) do |t|
t.profile = "fruits"
end
Cucumber::Rake::Task.new(:veggies) do |t|
t.profile = "veggies"
end
task :all => [:basket, :fruits, :veggies]
end
When I run "rake features:all" it will run each task in sequence(as expected/desired) but will run the features within each task one at a time(not desired). I would like to keep each task running in sequence but would like the features within each task to run in parallel. Is this possible? If not is there a way this can be done?
As always your help is much appreciated.
Rake offers multitask. You could either
Changing task to multitask in the rakefile:
multitask :all => [:basket, :fruits, :veggies]
use -m option in the command line:
rake -m features:all
Have a look at the parallel_tests gem. It allows you to run your features in parallel. While the typical use case is to run all of your features, you are able to run specific rake tasks in parallel. See the documentation for more details.
Somehow Rspec and Cucumber are making it into my default rake task (which is fine because I want them there). But I have tried adding additional tasks to the default task and it has no effect.
What is the proper way to add tasks to the default rake task?
Typically your Rakefile will have something like this:
task :default => [:spec]
You just need to add more tasks into this list.
The answer from davogenes is correct for non-namespaced rake tasks.
If you want to have a default task in namespaces you need to do the following
namespace :mynamespace do
desc "This should be the default"
task :mydefault do
Do.something_by_default
end
desc "Another task in this namespace"
task :other do
Do.something
end
end
desc "This is the default task of mynamespace"
task mynamespace: 'mynamespace:mydefault'
Then you can run rake mynamespace as well as rake mynamespace:other or rake mynamespace:mydefault.
I'm trying to use CruiseControl.rb (ver. 2.0.0pre1) with RSpec for my Ruby on Rails 3 app. The cruise_config.rb for my project looks like this:
Project.configure do |project|
project.rake_task = 'db:migrate db:test:prepare spec'
project.scheduler.polling_interval = 1.hour
project.scheduler.always_build = false
end
But when I try to run a build with CruiseControl, it says:
rake aborted!
Custom rake task(s) 'spec' not defined
Tasks: TOP => cc:build
(See full trace by running task with --trace)
It can't find the spec rake task to run the RSpec tests. I also tried to define a custom rake task inside my Rakefile and removed the project.rake_task = 'db:migrate db:test:prepare spec' line inside the cruise_config.rb:
desc "Custom Task for CruiseControl.rb"
task :cruise do
puts "Custom Rake task"
Rake::Task['db:migrate'].execute
Rake::Task['db:test:prepare'].execute
Rake::Task['spec'].execute
end
If i do so, CruiseControl says
rake aborted!
ActiveRecord::ConnectionNotEstablished
Tasks: TOP => cruise
(See full trace by running task with --trace)
[CruiseControl] Invoking Rake task "cruise"
Custom Rake task
Does anybody have CruiseControl.rb working with RSpec?
Ensure that you have the :spec task defined in your Rakefile, for RSpec 2, it looks like this:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
I'm not sure the "right" way to do this, so I wanted to ask the community. Probably a simple question.
I have a file "dbutils.rb" that I want to automatically include to have available whenever I run a "rake db:migrate", without putting it in application.rb and without putting it in every single db migration.
Where would I put my require to make this happen?
Rails defines $rails_rake_task = true in the :environment task.
The :environment task again is always loaded when you run :migrate (it is loaded for other Rake tasks also). You could use this to add require "dbutils" to your environment.rb when $rails_rake_task is true. And skip loading otherwise.
The other option is a custom Rake task like fl00r suggested.
I have a set of rake tasks where I need to invoke capistrano at some point. Edwin Goei's blog suggests shelling out to capistrano via "sh".
Is there a simpler way? It would seem you should be able to call the appropriate tasks programmatically. Thanks in advance.
Yes, Capistrano has programmatic access to the command-line components. If you want to call them from a rake task, though, you need to do a little extra work.
task :deploy
require 'rubygems'
require 'capistrano'
require 'capistrano/cli'
parameters = ["deploy"] # this is an array of the strings that come after
# cap on the command line. e.g.,
# ["deploy", "-S", "revision=1024"] gives you local var
# revision in your deploy.rb.
# The following is required ONLY when you run Capistrano 2+ from Rake,
# because Rake adds the methods from FileUtils to Object. FileUtils includes
# a method called symlink which interferes with Capistrano's symlink task.
Capistrano::Configuration::Namespaces::Namespace.class_eval { undef :symlink }
Capistrano::CLI.parse(parameters).execute!
end
For capistrano 3:
http://capistranorb.com/documentation/advanced-features/capistrano-pure-ruby/
require 'capistrano/all'
stages = "production"
set :application, 'my_app_name'
set :repo_url, 'git#github.com:capistrano/capistrano.git'
set :deploy_to, '/var/www/'
set :stage, :production
role :app, %w{}
require 'capistrano/setup'
require 'capistrano/deploy'
Dir.glob('capistrano/tasks/*.cap').each { |r| import r }
Capistrano::Application.invoke("production")
Capistrano::Application.invoke("deploy")
Jonathan, your mileage may vary by doing something like set(:shell, false) to stop capistrano running tasks in a sub-sh-shell.
Just a thought, feel free to ping me if you need a hand though.