Rake Tasks:
Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'make', and uses a 'Rakefile' and . rake files to build up a list of tasks. In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other.
It is most often used for administration level tasks that can be scripted. The benefit to using Rake over Make or similar, is that it is a Ruby tool and can interface with yourRoR app natively, so Models, data constraints and business rules are all available for use.
1.create a rake file
say lib/tasks/product. rake
another way to create a rake file:
$rails g task my_namespace my_task1 my_task2
$ create lib/tasks/my_namespace. rake
It will generate scaffold for our new rake task: >lib/tasks/my_namespace. rake
namespace : my_namespace do
desc "TODO"
task : my_task1 => : environment do
end
desc "TODO"
task : my_task2 => : environment do
end
end
It is awesome! And now you can write here your code for new rake tasks.
Let’s make sure these rake tasks are exist and we are able to use them:
$ rake -T |grep my_namespace
rake my_namespace: my_task1 # TODO
rake my_namespace: my_task2 # TODO
To upload products fromcsv using rake task:
require 'rubygems '
require 'csv '
namespace : product do
desc "To Upload products"
task : upload , [: file] => : environment do |task, args |
CSV. foreach ( args [ : file]) do |row|
puts row [ 0] + "creating.."
if row[ 0]. present?
Product. create ( row[ 0])
else
puts "Here is an empty row!"
end
end
end
end
$rake --tasks
$rake product: upload[ file] #To Upload products
rake product: upload[ '/home/saritha /Downloads/270815 New Registrations.csv']
To run rake tasks in production mode:
$rake product: upload[ '/home/saritha /Downloads/270815 New Registrations.csv'] RAILS_ENV=production
Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'make', and uses a 'Rakefile' and
It is most often used for administration level tasks that can be scripted. The benefit to using Rake over Make or similar, is that it is a Ruby tool and can interface with your
1.
$rails g task my_namespace my_task1 my_task2
$ create lib/tasks/my_namespace
It will generate scaffold for our new rake task: >lib/tasks/my_namespace
It is awesome! And now you can write here your code for new rake tasks.
Let’s make sure these rake tasks are exist and we are able to use them:
$ rake -T |
To upload products from
CSV
Product
$rake --tasks
$rake product
To run rake tasks in production mode:
$rake product
No comments:
Post a Comment