Does Rails run initializers for rake task? - ruby-on-rails

Are the scripts from config/initializers executed when I run rake task?

It does if your rake task depends on :environment.
i.e, you declare your task like so:
task :my_task => :environment do
...
end

Mostly yes. rake loads a complete rails environment including initializers, when your task depends on :environment.

Related

Rails - Rake test and rubocop in one task

I'm trying to setup my rails project so that all the verification required by a contributor is in one command, currently we have been running:
rake test
But now we also want to use rubocop for static analysis:
rubocop -R -a
I want this to be executable in one simple rake task. It would be nice to override 'rake test' to run rubocop then the standard rake test stuff for a rails project, as then no-one will have to remember to change the command. But if I have to create a separate rake task, that's probably fine too.
I've seen the rubocop rake integration here, at the bottom, but I'm not sure how to bundle that with 'rake test' into one task... Any thoughts?
I prefer to set my default task to run rubocop then the tests. Either way, it is a good idea to have those tasks separate rather than have one task do two things.
require 'rubocop/rake_task'
task :default => [:rubocop, :test]
desc 'Run tests'
task(:test) do
# run your specs here
end
desc 'Run rubocop'
task :rubocop do
RuboCop::RakeTask.new
end
Your tasks:
> rake -T
rake rubocop # Run rubocop
rake test # Run tests
This is the .rake file that I ended up with in the end.
desc 'Run tests and rubocop'
task :validate do
Rake::Task['rubocop'].invoke
Rake::Task['test'].invoke
end
task :rubocop do
require 'rubocop'
cli = Rubocop::CLI.new
cli.run(%w(--rails --auto-correct))
end
You could easily define your own rake task which first invokes Rails' test rake task and then the code snippet you mentioned for rubocop.
For example, in a .rake file you could have something like that:
require 'rubocop/rake_task'
desc 'Run tests and rubocop'
task :my_test do
Rake::Task['test'].invoke
RuboCop::RakeTask.new
end
If you feel the need to customize the call to Rubocop and that involves more code, you could create another custom task, say :rubocop, which you then invoke from :my_test as well.
Finally, an alternative to creating your own rake task and sticking with rake test would be to modify your test_helper to invoke whatever you need invoked after testing is completed.
It appears to have changed from:
Rubocop::RakeTask.new
to:
RuboCop::RakeTask.new
See ma? I know how to CamelCase!
I used following .rake file to run the test and rubocop tasks at once:
task default: %w[rubocop test]
RuboCop::RakeTask.new(:rubocop) do |task|
task.patterns = ['**/*.rb']
task.fail_on_error = false
task.options = ["--auto-correct-all"]
end
task :test do
ruby 'test/program_test.rb'
end
The first line allows both tasks to be run by calling rake.
Command line arguments can also be added in the options array.

how to run this rake task with 2 args and environment in rails 4?

Here are the first few lines of my rake task:
desc "Import National Data into DB\n Usage:
rake import_regional[/Users/am/0925/xyz.csv, some_market]"
task :import_regional, [:file_path, :market] => [:environment] do |t, args|
.......
Here is how Im trying to run this task on the command line:
rake import_regional ["/Users/xyz.csv", "northeast"]
when I try this, I get this error
rake aborted!
Don't know how to build task
'import_regional[/Users/xyz.csv,'
I suspect it's the way in which you're running the task on the command-line; the [] doesn't have its Ruby meaning, there. Instead, maybe see if this works for you:
rake "import_regional[/Users/xyz.csv, northeast]"
This should still allow for spaces, etc. in :file_path, if required.
Peace,
tiredpixel
Command line isn't a Ruby interpreter, it's mangling things before it gets to Rake.
Try this:
rake "import_regional['/Users/xyz.csv', 'northeast']"

Figuring out how rake works and how command line arguments can be handled in a rake tasks in rails

How can I pass arguments to a rake task so that it executes the rake task on a different schema? For example I have rake code such as the one below:
namespace :update_persons_table do
task :import => :environment do
config = Rails.configuration.database_configuration
ActiveRecord::Base.connection.schema_search_path = "my, public, data_master_reports"
# do stuff make updates to table....
end
end
I call this rake task from the command line like this:
RAILS_ENV='production' rake update_persons_table:import
BTW, does the above RAILS_ENV call I am using have to do with the :environment do statement I am using in the second line? Because in my database.yml file i do have a production: database entry. Im trying to figure out how the whole plumbing for this works. This rake task updates a table in a database. But I want to be able to call it on another clone table in a different database. How can I do that with passing parameters in the command line?
How can I pass arguments to a rake task so that it executes the rake task on a different schema?
I think what you are asking is
How can I pass arguments to a rake task so that it executes the rake task on a different environment?
The schema is the same for all environments of your app
does the above RAILS_ENV call I am using have to do with the :environment do statement I am using in the second line?
Yes :environment do will take whatever environment(:development, :test, :production) you are specifying. In your example, its RAILS_ENV='production'
Now to run the rake task in a different environment, like say the development environment
`RAILS_ENV='development' rake update_persons_table:import'
Now the same code that was executed on the production environment db when you ran `RAILS_ENV='production' rake update_persons_table:import' has been run in the development environment db
Hope this is clear enough to get you started

Rake - Adding to default task

I am using a third party gem that gives me the ability to execute some tests using the rake command:
rake jasminerice:run
Is it possible to include the task as part of default rake task? (i.e. when I run rake then rake jasminerice:run is added to the rest of my tests). This is required for CI integration.
Any help would be greatly appreciated!
In Rake, tasks can have dependencies. Generally, you can do this:
task :hello do
puts "hello"
end
task :default => :hello
For your specific problem, you should be able to do this:
task :default => "jasminerice:run"

How do I code a rake task that runs the Rails db:migrate task?

I would like to run db:migrate VERSION=0 and then db:migrate inside of my own rake task. I am confused about how to do this. Do I need a special require statement? My rake task will reside in the lib/tasks directory of a Rails app. Thanks.
Is your task just dependent on having a clean db? If that's the case then you can do:
task :my_task => [:environment, 'db:reset']
EDIT: Rake::Task[] won't accept parameters, you have to set it in ENV. In addition, you have to reenable the task to run it multiple times.
ENV['VERSION']= '0'
Rake::Task['db:migrate'].invoke
Rake::Task['db:migrate'].reenable
ENV.delete 'VERSION'
Rake::Task["db:migrate"].invoke
NOTE: Rake::Task.reenable requires Rake 0.8.2 or higher.
Check out rake db:reset as that will accomplish what you are trying to do.
To see what all of your rake tasks do, run rake -T

Resources