Custom rake task

Task to add Indian states to spree database.

1) Add a text file with extension .rake to the folder lib/tasks/
example:-lib/tasks/mytask.rake

2) add file content as follows

task :add_indian_states=>:environment do
CSV.foreach(“lib/tasks/states.csv”) do | row |
state, abbr = row
Spree::State.create(:name => state,:abbr=> abbr,:country_id=>3)

end
end

3) add file “states.csv” to lib/tasks/ with file content as follows

“Andhra Pradesh”,” AP”
“Arunachal Pradesh”,” AR”
“Assam”,” AS”
“Bihar”,” BR”
“Chhattisgarh”,” CT”
“Goa”,” GA”
“Gujarat”,” GJ”
“Haryana”,” HR”
“Himachal Pradesh”,” HP”
“Jammu & Kashmir”,” JK”
“Jharkhand”,” JH”
“Karnataka”,” KA”
“Kerala”,” KL”
“Madhya Pradesh”,” MP”
“Maharashtra”,” MH”
“Manipur”,” MN”
“Meghalaya”,” ML”
“Mizoram”,” MZ”
“Nagaland”,” NL”
“Odisha”,” OR”
“Punjab”,” PB”
“Rajasthan”,” RJ”
“Sikkim”,” SK”
“Tamil Nadu”,” TN”
“Tripura”,” TR”
“Uttarakhand”,” UT”
“Uttar Pradesh”,” UP”
“West Bengal”,” WB”
“Andaman & Nicobar”,” AN”
“Chandigarh”,” CH”
“Dadra and Nagar Haveli”,” DN”
“Daman & Diu”,” DD”
“Delhi”,” DL”
“Lakshadweep”,” LD”
“Puducherry”,” PY”
4) want to add the following to config/application.rb to work with CSV library
require ‘csv’

5) here “country_id=3” denotes INDIA as foreign key.

you can avoid  step 6 in later versions of spree and rails if this raise error

6) you want to make new file state_decorator.rb with the following content to the folder app/models to avoid rails Mass assignment error.

Spree::State.class_eval do
attr_accessible :country_id
end

7) rake add_indian_states will run the task. ( rails will detect the task name from the file which is the folder lib/tasks)
Rake Reference

railscasts 362
http://jasonseifer.com/2010/04/06/rake-tutorial
http://blog.jayfields.com/2006/11/ruby-testing-rake-tasks.html
http://lukaszwrobel.pl/blog/rake-tutorial
http://railsguides.net/2012/03/14/how-to-generate-rake-task/
csv reference

http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html

 

www.klairvoyant.in

Advertisements
This entry was posted in rails, ruby on rails, web development and tagged , , , , , . Bookmark the permalink.

5 Responses to Custom rake task

  1. Jeff says:

    Nice article. Got one problem. What is spree? Im using a Mysql server but i dont know how to reference it

  2. spree ( http://spreecommerce.com/ ) is open source ecommerce platform. using this rake task added data to this open source code.

  3. Johnf415 says:

    I like this post, enjoyed this one regards for putting up. The goal of revival is conformity to the image of Christ, not imitation of animals. by Richard F. Lovelace. eeccgaedeade

  4. Johne449 says:

    I just ought to tell you which you have written an exceptional and distinctive post that I really enjoyed reading. Im fascinated by how nicely you laid out your material and presented your views. Thank you. fdccafbacgde

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s