Skip to content

Instantly share code, notes, and snippets.

@cantonic
Created September 4, 2012 09:24
Show Gist options
  • Save cantonic/3619074 to your computer and use it in GitHub Desktop.
Save cantonic/3619074 to your computer and use it in GitHub Desktop.
require "csv"
namespace :contact do
desc "Create CSV for all unsold Contacts"
task :create_csv => :environment do
@contact_count = []
cities = City.where(:is_active => true).each do |city|
CSV.open("tmp/csv/#{city.name}-#{Date.today}.csv", "w", {:force_quotes=>true}) do |csv|
csv << ["Anrede", "Name", "Email", "Telefon", "Geburtsdatum", "Anmeldedatum", "Ort"]
contacts = city.contacts.where(:status => 1)
@contact_count << [city.name, contacts.length]
contacts.each do |contact|
csv << ["#{contact.title}", "#{contact.name}", "#{contact.email}", "#{contact.phone}", "#{contact.birthday}", "#{contact.created_at.in_time_zone('MET')}", "#{city.name}"]
contact.update_attributes(:status => 2)
end
end
end
send_csv(@contact_count)
end
def send_csv(contact_count)
ContactMailer.send_csv(contact_count).deliver
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment