Add rake task to export enterprises to CSV

This commit is contained in:
Rohan Mitchell
2014-07-09 14:04:51 +10:00
parent 7a3f68ea38
commit 3758a8ae2b
2 changed files with 36 additions and 4 deletions

View File

@@ -0,0 +1,32 @@
require 'csv'
namespace :openfoodnetwork do
namespace :dev do
desc 'export enterprises to CSV'
task :export_enterprises => :environment do
CSV.open('db/enterprises.csv', 'wb') do |csv|
csv << enterprise_header
enterprises.each do |enterprise|
csv << enterprise_row(enterprise)
end
end
end
private
def enterprises
Enterprise.by_name
end
def enterprise_header
['name', 'description', 'long_description', 'is_primary_producer', 'is_distributor', 'contact', 'phone', 'email', 'website', 'twitter', 'abn', 'acn', 'pickup_times', 'next_collection_at', 'distributor_info', 'visible', 'facebook', 'instagram', 'linkedin', 'address1', 'address2', 'city', 'zipcode', 'state', 'country']
end
def enterprise_row(enterprise)
[enterprise.name, enterprise.description, enterprise.long_description, enterprise.is_primary_producer, enterprise.is_distributor, enterprise.contact, enterprise.phone, enterprise.email, enterprise.website, enterprise.twitter, enterprise.abn, enterprise.acn, enterprise.pickup_times, enterprise.next_collection_at, enterprise.distributor_info, enterprise.visible, enterprise.facebook, enterprise.instagram, enterprise.linkedin, enterprise.address.address1, enterprise.address.address2, enterprise.address.city, enterprise.address.zipcode, enterprise.address.state_name, enterprise.address.country.andand.name]
end
end
end

View File

@@ -6,9 +6,9 @@ namespace :openfoodnetwork do
desc 'export users to CSV'
task export_users: :environment do
CSV.open('db/users.csv', 'wb') do |csv|
csv << header
csv << user_header
users.each do |user|
csv << row(user)
csv << user_row(user)
end
end
end
@@ -34,7 +34,7 @@ namespace :openfoodnetwork do
end
def header
def user_header
["encrypted_password", "password_salt", "email", "remember_token", "persistence_token", "reset_password_token", "perishable_token", "sign_in_count", "failed_attempts", "last_request_at", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "login", "created_at", "updated_at", "authentication_token", "unlock_token", "locked_at", "remember_created_at", "reset_password_sent_at",
"role_name",
@@ -45,7 +45,7 @@ namespace :openfoodnetwork do
end
def row(user)
def user_row(user)
sa = user.orders.last.andand.ship_address
ba = user.orders.last.andand.bill_address